-
Notifications
You must be signed in to change notification settings - Fork 27.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Reading request.cookies
in a GET app route does not make the route dynamic
#49006
Comments
Here is an example GET app route that remains static despite reading from Note that uncommenting use of other request attributes in this app route will make it dynamic: Here is a nearly identical GET app route that is correctly treated as dynamic because it reads cookie data via |
request.cookies
in an GET app route does not make the route dynamicrequest.cookies
in an app route with just GET does not make the route dynamic
request.cookies
in an app route with just GET does not make the route dynamicrequest.cookies
in a GET app route does not make the route dynamic
All Next.js versions that support app routes in the app/ directory have the same behaviour. I tried versions 13.2.4 & 13.2.0 |
Give this subtle bug, and others that might be lurking, can I suggest that you rethink the current approach where GET function app routes are automatically determined to be dynamic or static based on vague criteria and a sprinkling of black magic? It would be much simpler if app routes were always dynamic by default, but could be made static with the explicit and obvious addition of Here is the current documentation about this: Based on my experiments tracking down this bug, I think these seemingly simple guidelines need to have the following caveats spelled out clearly. (However I'm still not sure I'm right about my additions below, it's not at all clear exactly what is going on under the hood)
Provided you call one of a specially-blessed subset of attributes on the Request object. Just passing in the Request object to the function is not enough, you must also access the request appropriately within your code.
Here "using" doesn't mean making e.g. POST requests from a client, which is what I assumed at first. It really means adding code for any non-GET functions to the app route file. If you have a static GET function working as intended, you might be surprised when you later add a POST function to that app route and suddenly find the old GET function has become dynamic. Just adding the following triggers dynamic behaviour for the entire app route file:
"Using" here means calling a blessed attribute of the |
This issue is fixed for me in version 14.0.4 most likely by the PR #58769. Thanks! |
This closed issue has been automatically locked because it had no new activity for 2 weeks. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you. |
Verify 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: 16.16.0 npm: 8.11.0 Yarn: 1.22.17 pnpm: N/A Relevant packages: next: 13.4.2-canary.4 eslint-config-next: N/A react: 18.2.0 react-dom: 18.2.0 typescript: 4.9.5
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/jmurty/next-issue-non-dynamic-cookies
To Reproduce
npm run build && npm run start
/api/non-dynamic-cookies
builds as Static, not Servercurl http://localhost:3000/api/non-dynamic-cookies -H 'Cookie:a=b'
and see the timestamp remains unchanged, and cookies provided are not returned/api/dynamic-cookies
builds as Server, and if hit with e.g.curl http://localhost:3000/api/dynamic-cookies -H 'Cookie:a=b'
returns a new timestamp each time and the provided cookie valuesrequest.url
. Upon rebuild and restart, the route becomes dynamicOld steps
Reproduce bug when reading from
request.cookies
:request.cookies
yarn build && yarn start
) or deployed to VercelCounter-example, no bug when reading
cookies
imported from"next/headers"
:cookies
imported from "next/headers"yarn build && yarn start
) or deployed to VercelDescribe the Bug
In 13.4.1 reading cookie data in a GET app route from the
request.cookies
attribute provided byNextRequest
does not trigger Next.js to make the app route dynamic. Instead the app route is mis-categorised as a static route at build time, and will return the cached static content forever.Workarounds for now are to either:
cookies
viaimport { cookies } from "next/headers"
instead of fromrequest.cookies
export const revalidate = 0
or add aPOST()
function or…Expected Behavior
Accessing the
request.cookies
attribute within a GET app route should mark it as dynamic, like what happens if you access other attributes of therequest
likeurl
orheaders
, or when you access cookies viaimport { cookies } from "next/headers"
Which browser are you using? (if relevant)
No response
How are you deploying your application? (if relevant)
next start & Vercel
The text was updated successfully, but these errors were encountered: