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
- Clone the repo
- Run
npm run dev - Go to localhost:3000
- Click on the link that says
Go to FCS Category - 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
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)
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
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=nin their documentation about pagination best practices.Also in my case I have
/news/[category]/page.tsxand/news/[category]/[subcategory]/page.tsxso not sure that adding/news/[category]/[pageIndex]/page.tsxand/news/[category]/[subcategory]/[pageIndex].tsxwould play nice?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.tsxand[pageIndex]/page.tsxas I believe those would conflict as they are at the same level.