Next.js Discord

Discord Forum

Help with Fetching searchParams in Dynamic Route ([[...slug]]) on Server Side in Next.js

Answered
American Chinchilla posted this in #help-forum
Open in Discord
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:
└── app
    └── category
        └── [[...slug]]
            └── page.tsx


Code:
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.
View full answer

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
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.
Answer
Oh lol makes sense, a static page can’t have searchParams since it’s generated at built time 🀣