Skip to content

[BUG] Multiple cookies are not set properly #498

@blitss

Description

@blitss

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

  1. Create a perfectly normal nextjs project, can just use npm create cloudflare@latest -- my-next-app --framework=next --experimental
  2. Create middleware.ts
  3. 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:

Image

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't workingtriage

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions