-
Notifications
You must be signed in to change notification settings - Fork 30.1k
Description
Link to the code that reproduces this issue
https://github.com/Heet-Bhalodiya/mui-next-test
To Reproduce
Link to reproduction
Repository: https://github.com/Heet-Bhalodiya/mui-next-test
Deployed URL: https://mui-next-test-ten.vercel.app/test/en
Describe the Bug
After upgrading to Next.js 16.0.1, using cookies() in layout.tsx causes RSC (React Server Components) errors in production when a basePath is configured. This creates an impossible dilemma for implementing server-side theme persistence with system preference support.
The Dilemma
There are two approaches to implement theme persistence, but both have critical issues:
Approach 1: Using cookies() in layout (Current Implementation)
✅ Pros:
- Theme persists correctly across page refreshes
- Server knows the user's theme preference
- System mode can be stored and retrieved
❌ Cons:
- Causes RSC server errors in production when
basePathis configured - Forces all routes to be dynamic (removes static optimization)
- Cannot use
generateStaticParams()becausecookies()makes the entire layout dynamic
Approach 2: Using generateStaticParams() without cookies
✅ Pros:
- No RSC errors
- Routes can be statically generated
❌ Cons:
- Server has no knowledge of user's theme preference
- If server defaults to 'light' but user's system is 'dark', the page renders light first, then switches to dark
Would appreciate guidance on:
- Is this a bug or expected behavior?
- What's the recommended way to implement server-side theme persistence in Next.js 16 with
basePath? - Is there a way to use
cookies()without triggering RSC errors in this configuration?
Current vs. Expected behavior
Current Issue
When deploying with basePath configured and using cookies() in the root layout, the application throws RSC errors in production (see screenshot below).
Error Context:
- Happens in production deployment
- Only when
basePathis configured - Started happening after Next.js 16 update (it worked as expected till next v15)
Expected Behavior
- Should be able to use
cookies()in root layout without RSC errors whenbasePathis configured - Should be able to read server-side cookies to determine theme preference and avoid FOUC
- Should support system preference detection on the server side
- Static generation should be optional when dynamic features (like cookies) are required
Provide environment information
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 25.1.0: Mon Oct 20 19:32:56 PDT 2025; root:xnu-12377.41.6~2/RELEASE_ARM64_T8132
Available memory (MB): 16384
Available CPU cores: 10
Binaries:
Node: 22.12.0
npm: 11.4.2
Yarn: 1.22.22
pnpm: 10.11.0
Relevant Packages:
next: 16.0.1 // There is a newer version (16.0.3) available, upgrade recommended!
eslint-config-next: N/A
react: 19.2.0
react-dom: 19.2.0
typescript: 5.9.3
Next.js Config:
output: N/A
⚠ There is a newer version (16.0.3) available, upgrade recommended!
Please try the latest canary version (`npm install next@canary`) to confirm the issue still exists before creating a new issue.
Read more - https://nextjs.org/docs/messages/opening-an-issueWhich area(s) are affected? (Select all that apply)
Cookies, Dynamic Routes
Which stage(s) are affected? (Select all that apply)
Vercel (Deployed), next build (local)
Additional context
No response