K1|ller
K1|ller13mo ago

Page couldn't be rendered statically because it used `cookies`

Has anyone had this issue? I'm using trpc in a server component and in localhost it works fine but it won't work when deployed toVercel. Any help will be appreciated
No description
No description
4 Replies
K1|ller
K1|llerOP13mo ago
solved adding force-dynamic, not sure if it's the best solution
iDarkLightning
iDarkLightning13mo ago
Well you haven't solved it, force dynamic forces it to not be static But yes, you cannot access cookies on static pages
LittleLily
LittleLily13mo ago
It seems like the most likely cause for getting the error is the failure of the nextjs auto-detection of dynamic routes based on use of cookies/etc due to it not being able to follow the chain of calls that results in cookies being used. It's happened to me a few times where I had a page that used cookies, then I moved the cookie usage to an external file that the page imported instead, and that must break the auto-detection and so nextjs thinks the page can be statically rendered even though it can't in reality. If that's the situation you're in then setting force-dynamic is the correct solution since it likely already needed to be dynamic, it just wasn't able to be detected. Even though the detection of dynamic routes/pages is mentioned in the docs somewhere, I think it would be better if it was made a little more obvious and upfront in the docs that the detection of dynamic vs static routes isn't always going to work, and I think it should recommend that you should probably just explicitly set force dynamic on routes that you know will use cookies, because otherwise the build will just decide to fail randomly one day when you refactor code that is functionally identical but stumps the detection in nextjs
K1|ller
K1|llerOP13mo ago
but i just need ssr, i guess it solves the needs? yeah makes sense I think it's more a vercel/next thing

Did you find this page helpful?