next.js: Using generateStaticParams causes issues with searchParams

Verify canary release

  • I verified that the issue exists in the latest Next.js canary release

Provide environment information

Operating System:
      Platform: darwin
      Arch: arm64
      Version: Darwin Kernel Version 22.4.0: Mon Mar  6 20:59:28 PST 2023; root:xnu-8796.101.5~3/RELEASE_ARM64_T6000
    Binaries:
      Node: 18.16.0
      npm: 9.5.1
      Yarn: 1.22.19
      pnpm: N/A
    Relevant packages:
      next: 13.4.3-canary.0
      eslint-config-next: 13.4.2
      react: 18.2.0
      react-dom: 18.2.0
      typescript: 5.0.4

Which area(s) of Next.js are affected? (leave empty if unsure)

App directory (appDir: true)

Link to the code that reproduces this issue

https://github.com/JamesSingleton/searchparams-issue-next-13

To Reproduce

  1. Clone the repo
  2. Run npm run dev
  3. Go to localhost:3000
  4. Click on the link that says Go to FCS Category
  5. You will be met with the bug

Describe the Bug

For some reason due to using generateStaticParams, if the page you are going to uses the searchParams param available to the page and generateMetadata functions you will get a Dynamic server usage: searchParams.page

Screenshot 2023-05-12 at 9 09 04 PM

Expected Behavior

Ideally there is no error. I have gone through https://nextjs.org/docs/app/api-reference/functions/generate-static-params#generate-params-from-the-top-down but there is no mention of searchParams.

Which browser are you using? (if relevant)

No response

How are you deploying your application? (if relevant)

No response

About this issue

  • Original URL
  • State: open
  • Created 4 years ago
  • Reactions: 11
  • Comments: 16 (4 by maintainers)

Most upvoted comments

If you are trying to dynamically read URL search params, this requires the page being dynamic. A statically generate page would not have access to the incoming request where the search parameters are read.

But you could render it with default params… (for example pagination)

@JamesSingleton This is the same with cookies and generateStaticParams. As an example: I’m trying to generateStaticParams all my product pages for an e-commerce shop like so

export function generateStaticParams() {
    return [];
}

This will cause our pages to be statically generated by users visiting the product pages which is great! But now we’d like the price on those PDP’s to be dynamic per customer and basically stream them in via a <Suspense > component. After trying multiple different ways of achieving this I came to the conclusion that this is currently not possible.

Reading trough multiple issue on GH like https://github.com/vercel/next.js/issues/49373, I noticed that everyone expects to be able to mix static and dynamic components which is not the case, your page either needs to be fully static or will be dynamic.

I noted the following “Good to know” section on https://nextjs.org/docs/app/building-your-application/rendering/static-and-dynamic-rendering stating the following:

“In the future, Next.js will introduce hybrid server-side rendering where layouts and pages in a route can be independently statically or dynamically rendered, instead of the whole route.”

@leerob please feel free correct me where I’m wrong.

If you are trying to dynamically read URL search params, this requires the page being dynamic. A statically generate page would not have access to the incoming request where the search parameters are read.

@filipkowal yea that would be nice of the linter to inform you of that because technically nothing breaks in production and if you refresh locally it goes away.

As for you suggestion, Google recommends ?page=n in their documentation about pagination best practices.

Also in my case I have /news/[category]/page.tsx and /news/[category]/[subcategory]/page.tsx so not sure that adding /news/[category]/[pageIndex]/page.tsx and /news/[category]/[subcategory]/[pageIndex].tsx would play nice?

Screenshot 2023-06-28 at 8 29 07 AM

In the production version of this website I have the categories hardcoded as /fcs/page/[page].tsx… But with the switch over to the app directory I took it as an opportunity to redo the logic so that I didn’t have to hardcode any of the categories and could pull them all from Sanity. Maybe there is a better way?

I did do some research into competition and

I just didn’t want their to be a collision of [subcategory]/page.tsx and [pageIndex]/page.tsx as I believe those would conflict as they are at the same level.