nextjs - ✅(Solved) Fix RSC Error with cookies() in layout.tsx when using basePath in Next.js 16 [1 pull requests, 6 comments, 2 participants]

Official PRs (…)
ON THIS PAGE

Recommended Tools

×6

Utilities matched from this issue’s tags and category — try them while you read without losing context.

GitHub issue graph ai analysis

Paste a GitHub issue URL. We fetch that issue, discover linked issues from bodies/comments/timeline, collect linked pull requests, and produce a structured English report.

The report is written in English Markdown for sharing and archival.

Helpful · Quick feedback

Loading…
GitHub stats
vercel/next.js#86284Fetched 2026-04-08 02:11:41
View on GitHub
Comments
6
Participants
2
Timeline
22
Reactions
0
Timeline (top)
commented ×6subscribed ×5mentioned ×4referenced ×3

Error Message

Error Context:

Root Cause

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

Fix Action

Fixed

PR fix notes

PR #16: Fix RSC error with cookies() in layout when using basePath

Description (problem / solution / changelog)

What?

Removes the basePath from the pathname in normalized RSC requests to resolve an issue where the cookies() API in layout.tsx failed to find the request context with basePath configured.

Why?

This fixes a bug (#86284) that caused cookies() to malfunction when basePath was applied, ensuring consistent behavior in applications utilizing this setup.

How?

  • Removed the basePath from pathname after normalizing RSC requests in handleRSCRequest.
  • Added a test case to verify that cookies() works correctly with basePath configured in layout.tsx.

Fixes #86284

Changed files

  • packages/next/src/server/base-server.ts (modified, +16/-0)
  • test/e2e/app-dir/app-routes/app-custom-routes.test.ts (modified, +14/-0)
  • test/e2e/app-dir/app-routes/app/layout.tsx (modified, +2/-0)
  • test/e2e/app-dir/app-routes/app/test-layout-cookies/layout.tsx (added, +20/-0)
  • test/e2e/app-dir/app-routes/app/test-layout-cookies/page.tsx (added, +5/-0)

Code Example

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
RAW_BUFFERClick to expand / collapse

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).

<img width="686" height="197" alt="Image" src="https://github.com/user-attachments/assets/ed6cb1ab-f54e-43c5-bb2a-b352ab004bd5" />

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

extent analysis

TL;DR

Upgrading to Next.js 16.0.3 may resolve the RSC errors caused by using cookies() in layout.tsx with a configured basePath.

Guidance

  • Verify if the issue persists with the latest version of Next.js (16.0.3) as recommended in the environment information.
  • Consider using generateStaticParams() without cookies as a temporary workaround, although this approach has its own limitations.
  • Review the Next.js documentation on React Server Components and cookies to ensure correct usage and configuration.
  • If the issue remains after upgrading, try to isolate the problem by testing with a minimal reproduction case.

Example

No specific code example is provided due to the complexity of the issue and the need for more information on the exact implementation.

Notes

The provided information suggests that the issue might be related to the version of Next.js used (16.0.1), and upgrading to a newer version could potentially resolve the problem. However, without further details on the implementation, it's challenging to provide a definitive solution.

Recommendation

Apply the workaround of upgrading to Next.js 16.0.3, as this version may include fixes for the reported issue, and the environment information explicitly recommends trying the latest version.

Vote matrix · Quick signals

Works
Did the solution work? Tap to confirm.
Easy Fix
Was it a quick fix?
Time Saver
Did it save you time?
Blocking
Was it severely blocking?
Common Issue
Are others likely hitting this too?
Flaky / Intermittent
Is it intermittent?
Verified / Reproducible
Can you reproduce it reliably?
Loading…

Still need to ship something?

×6

Another batch ranked right after the header list — different links, same matching logic.

Back to top recommendations

TRENDING

nextjs - ✅(Solved) Fix RSC Error with cookies() in layout.tsx when using basePath in Next.js 16 [1 pull requests, 6 comments, 2 participants]