Help with Fetching searchParams in Dynamic Route ([[...slug]]) on Server Side in Next.js
Answered
American Chinchilla posted this in #help-forum
American ChinchillaOP
Hi everyone! π
I'm new to Next.js (currently using v15) and learning the ropes. I'm struggling with fetching searchParams on the server side in a dynamic route using a catch-all segment structure.
Here's my folder structure:
Code:
When I visit the following URL:
http://localhost:3000/category/health-and-beauty/hair-care?brand=ors
Console output:
'm confused because searchParams is always empty ({}), even though the URL includes a query string (?brand=ors). I've gone through the Next.js documentation but still can't figure out what I'm missing. π΅βπ«
Can anyone point me in the right direction? Your help would mean a lot! π
I'm new to Next.js (currently using v15) and learning the ropes. I'm struggling with fetching searchParams on the server side in a dynamic route using a catch-all segment structure.
Here's my folder structure:
βββ app
βββ category
βββ [[...slug]]
βββ page.tsxCode:
interface CategoryViewProps {
params: { page?: string; slug: string[] };
searchParams?: SearchParamsType;
}
export default async function CategoryPage({
params,
searchParams = {},
}: CategoryPageProps) {
console.log("Category page params:", params);
console.log("Category page search param:", searchParams);
return <CategoryView params={params} />;
}When I visit the following URL:
http://localhost:3000/category/health-and-beauty/hair-care?brand=ors
Console output:
Category page params: slug: [ 'health-and-beauty', 'hair-care' ]
Category page search param: {}'m confused because searchParams is always empty ({}), even though the URL includes a query string (?brand=ors). I've gone through the Next.js documentation but still can't figure out what I'm missing. π΅βπ«
Can anyone point me in the right direction? Your help would mean a lot! π
Answered by American Chinchilla
@LuisLl Thanks you so much for the reply.
I figured it out I have the dynamic = "force-static" on top of the page. After removing this all working fine now. Thanks again. Such a small mistake costed me 3 hours π but I'm glad its working now.
I figured it out I have the dynamic = "force-static" on top of the page. After removing this all working fine now. Thanks again. Such a small mistake costed me 3 hours π but I'm glad its working now.
3 Replies
Mmmh weird, first of all itβs weird youβre not getting the warning about params and searchParams being async now.
They should be typed as a Promise<{β¦}> and need to be awaited when consuming them.
Also, can you read the search params via useSearchParams in a client component? Just to make sure theyβre actually there
They should be typed as a Promise<{β¦}> and need to be awaited when consuming them.
Also, can you read the search params via useSearchParams in a client component? Just to make sure theyβre actually there
American ChinchillaOP
@LuisLl Thanks you so much for the reply.
I figured it out I have the dynamic = "force-static" on top of the page. After removing this all working fine now. Thanks again. Such a small mistake costed me 3 hours π but I'm glad its working now.
I figured it out I have the dynamic = "force-static" on top of the page. After removing this all working fine now. Thanks again. Such a small mistake costed me 3 hours π but I'm glad its working now.
Answer
Oh lol makes sense, a static page canβt have searchParams since itβs generated at built time π€£