-
Notifications
You must be signed in to change notification settings - Fork 72
Closed
Labels
Description
Describe the bug
When I try to set multiple cookies inside middleware, they are getting folded into one Set-Cookie
header, and therefore browser doesn't set them properly.
According to cloudflare docs and the IETF RFC, the set-cookie header should not fold.
I narrowed down issue to opennext because it worked for me on other environments:
- next-on-pages
next dev
- Just using cloudflare workers, e.g. this code:
export default {
async fetch(request, env, ctx) {
const headers = new Headers();
headers.append("Set-Cookie", "test1=hello; Path=/; HttpOnly");
headers.append("Set-Cookie", "test2=world; Path=/; HttpOnly");
return new Response('Hello World!', { headers });
},
};
Steps to reproduce
repro: https://github.com/blitss/opennext-set-cookies-issue
- Create a perfectly normal nextjs project, can just use
npm create cloudflare@latest -- my-next-app --framework=next --experimental
- Create middleware.ts
- Use multiple
set-cookie
header inside middleware:
import { NextRequest, NextResponse, NextFetchEvent } from "next/server";
export function middleware(request: NextRequest, event: NextFetchEvent) {
// Create a response instance
const response = new NextResponse('testing cookies');
// Append multiple cookies using the Set-Cookie header
response.headers.append(
"Set-Cookie",
"test1=value1; Path=/; HttpOnly; Secure; SameSite=Strict"
);
response.headers.append(
"Set-Cookie",
"test2=value2; Path=/; HttpOnly; Secure; SameSite=Strict"
);
return response;
}
export const config = {
matcher: ["/test"],
};
A result:

Expected behavior
The set cookie should not fold
@opennextjs/cloudflare version
0.5.12
Wrangler version
4.3.0
next info output
% bunx next info
Operating System:
Platform: darwin
Arch: arm64
Version: Darwin Kernel Version 24.3.0: Thu Jan 2 20:24:23 PST 2025; root:xnu-11215.81.4~3/RELEASE_ARM64_T6020
Available memory (MB): 16384
Available CPU cores: 12
Binaries:
Node: 20.10.0
npm: 10.2.3
Yarn: 1.22.19
pnpm: 8.14.0
Relevant Packages:
next: 15.2.3 // There is a newer version (15.2.4) available, upgrade recommended!
eslint-config-next: 15.1.6
react: 19.0.0
react-dom: 19.0.0
typescript: 5.7.3
Next.js Config:
output: N/A
⚠ There is a newer version (15.2.4) 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
Additional context
No response