Skip to content

RSC Error with cookies() in layout.tsx when using basePath in Next.js 16 #86284

@Heet-Bhalodiya

Description

@Heet-Bhalodiya

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 basePath is configured
  • Forces all routes to be dynamic (removes static optimization)
  • Cannot use generateStaticParams() because cookies() 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:

  1. Is this a bug or expected behavior?
  2. What's the recommended way to implement server-side theme persistence in Next.js 16 with basePath?
  3. 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).

Image

Error Context:

  • Happens in production deployment
  • Only when basePath is configured
  • Started happening after Next.js 16 update (it worked as expected till next v15)

Expected Behavior

  1. Should be able to use cookies() in root layout without RSC errors when basePath is configured
  2. Should be able to read server-side cookies to determine theme preference and avoid FOUC
  3. Should support system preference detection on the server side
  4. 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-issue

Which 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

Metadata

Metadata

Assignees

No one assigned

    Labels

    CookiesRelated to the async cookies() function.Dynamic RoutesRelated to dynamic routes.

    Type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions