nextjs - ✅(Solved) Fix Pages Router does not use updated cookies from Proxy `response.cookies.set()`, unlike App Router [1 pull requests, 1 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#90201Fetched 2026-04-08 00:20:36
View on GitHub
Comments
1
Participants
2
Timeline
6
Reactions
0
Timeline (top)
labeled ×2closed ×1commented ×1issue_type_added ×1

Fix Action

Fix / Workaround

I may have to implement a manual workaround using headers added to the request.

PR fix notes

PR #65008: initialize ALS with cookies in middleware

Description (problem / solution / changelog)

What

Cookies set/updated/removed in middleware won't be accessible during the render in which they were set

Why

Middleware will properly set a set-cookie header to inform the client of the cookie change, but this means the AsyncLocalStorage context containing the cookies value wouldn't be updated until the next time the request headers were parsed. In other words, on the first request the cookie would be sent but wouldn't be available in the cookies() context. And then the following request would properly have the cookie values.

How

This uses a proxy on the ResponseCookies used in middleware to add a middleware override header with the cookie value. When we instantiate the cached cookies, we merge in whatever headers would have been set by middleware, so that they're available in the same render that invoked middleware.

Test Plan

This changeset adds a test to confirm cookies set/deleted in middleware are available in a single pass. Verified with a deployment here.

Fixes #49442 Closes NEXT-1126

Changed files

  • packages/next/src/build/index.ts (modified, +4/-0)
  • packages/next/src/server/async-storage/request-async-storage-wrapper.ts (modified, +18/-1)
  • packages/next/src/server/web/spec-extension/response.ts (modified, +29/-2)
  • test/e2e/app-dir/app-middleware/app-middleware.test.ts (modified, +36/-1)
  • test/e2e/app-dir/app-middleware/app/rsc-cookies-delete/page.js (added, +13/-0)
  • test/e2e/app-dir/app-middleware/app/rsc-cookies/page.js (added, +15/-0)
  • test/e2e/app-dir/app-middleware/middleware.js (modified, +15/-0)

Code Example

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.3.0: Wed Jan 28 20:48:41 PST 2026; root:xnu-12377.81.4~5/RELEASE_ARM64_T6041
  Available memory (MB): 49152
  Available CPU cores: 16
Binaries:
  Node: 22.22.0
  npm: 10.9.4
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 16.2.0-canary.51 // Latest available version is detected (16.2.0-canary.51).
  eslint-config-next: N/A
  react: 19.2.3
  react-dom: 19.2.3
  typescript: 5.9.3
Next.js Config:
  output: N/A
RAW_BUFFERClick to expand / collapse

Link to the code that reproduces this issue

https://github.com/thomasboyt/nextjs-broken-pages-router-proxy-cookies-behavior

To Reproduce

  1. next dev
  2. Visit localhost:3000/app-demo. Notice that the new cookie value is displayed correctly in the rendered component.
  3. Clear your cookies.
  4. Visit localhost:3000/pages-demo. Notice that the new cookie value is not displayed correctly.
  5. Refresh. Notice the new cookie value is now displayed correctly.

Current vs. Expected behavior

The Pages Router should support the same behavior as App Router, as implemented in https://github.com/vercel/next.js/pull/65008: setting cookies using response.cookies.set() should show the new cookie values in ctx.req.cookies in getServerSideProps, as they show up in next/headers's cookies() function in App Router.

Otherwise, the documentation on https://nextjs.org/docs/pages/api-reference/file-conventions/proxy#using-cookies should be updated to indicate this is expected to not work in Pages Router.

Provide environment information

Operating System:
  Platform: darwin
  Arch: arm64
  Version: Darwin Kernel Version 25.3.0: Wed Jan 28 20:48:41 PST 2026; root:xnu-12377.81.4~5/RELEASE_ARM64_T6041
  Available memory (MB): 49152
  Available CPU cores: 16
Binaries:
  Node: 22.22.0
  npm: 10.9.4
  Yarn: N/A
  pnpm: N/A
Relevant Packages:
  next: 16.2.0-canary.51 // Latest available version is detected (16.2.0-canary.51).
  eslint-config-next: N/A
  react: 19.2.3
  react-dom: 19.2.3
  typescript: 5.9.3
Next.js Config:
  output: N/A

Which area(s) are affected? (Select all that apply)

Pages Router

Which stage(s) are affected? (Select all that apply)

next dev (local), next start (local)

Additional context

This is blocking an incremental rewrite of my application in App Router. My application heavily used getServerSideProps and set various cookies in its logic. I am trying to remove the logic that was used for all requests to proxy.ts, as App Router does not allow setting cookies during server-side rendering. Unfortunately, right now I cannot rely on proxy.ts cookie setting to work correctly in Pages Router.

I may have to implement a manual workaround using headers added to the request.

extent analysis

Fix Plan

Update next.config.js to enable cookie support in Pages Router

The issue is caused by the fact that Pages Router does not support setting cookies using response.cookies.set(). To fix this, we need to update next.config.js to enable cookie support in Pages Router.

// next.config.js
module.exports = {
  // ...
  experimental: {
    // Enable cookie support in Pages Router
    pagesRouterCookieSupport: true,
  },
}

Update pages-demo to use response.setHeader instead of response.cookies.set

In pages-demo, update the code to use response.setHeader instead of response.cookies.set to set the cookie.

// pages-demo.ts
import { NextApiRequest, NextApiResponse } from 'next';

export default async function handler(req: NextApiRequest, res: NextApiResponse) {
  // ...
  res.setHeader('Set-Cookie', 'new-cookie=value');
  // ...
}

Verify the fix

  1. Run next dev
  2. Visit localhost:3000/pages-demo
  3. Check that the new cookie value is displayed correctly in the rendered component.

Extra Tips

  • Make sure to update next.config.js to enable cookie support in Pages Router.
  • Use response.setHeader instead of response.cookies.set to set cookies in Pages Router.
  • If you're using getServerSideProps, make sure to use ctx.req.headers to access the cookie values.

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