diff --git a/deno_dist/context.ts b/deno_dist/context.ts index ce647e4cf..ac86abada 100644 --- a/deno_dist/context.ts +++ b/deno_dist/context.ts @@ -2,7 +2,7 @@ import type { HonoRequest } from './request.ts' import type { Env, FetchEventLike, Input, NotFoundHandler, TypedResponse } from './types.ts' import { HtmlEscapedCallbackPhase, resolveCallback } from './utils/html.ts' import type { RedirectStatusCode, StatusCode } from './utils/http-status.ts' -import type { JSONValue, JSONParsed, IsAny, Simplify } from './utils/types.ts' +import type { IsAny, JSONParsed, JSONValue, Simplify } from './utils/types.ts' type HeaderRecord = Record export type Data = string | ArrayBuffer | ReadableStream @@ -380,7 +380,11 @@ export class Context< if (this.#headers) { // If the header is set by c.header() and arg.headers, c.header() will be prioritized. this.#headers.forEach((v, k) => { - header.set(k, v) + if (k === 'set-cookie') { + header.append(k, v) + } else { + header.set(k, v) + } }) } const headers = setHeaders(header, this.#preparedHeaders) diff --git a/src/context.test.ts b/src/context.test.ts index 2453433dd..827247f5c 100644 --- a/src/context.test.ts +++ b/src/context.test.ts @@ -308,6 +308,22 @@ describe('Pass a ResponseInit to respond methods', () => { expect(await res.text()).toBe('

Hello

') }) + it('c.body() should retain context cookies from context and original response', async () => { + setCookie(c, 'context', '1') + setCookie(c, 'context', '2') + + const originalResponse = new Response('', { + headers: { + 'set-cookie': 'response=1; Path=/', + }, + }) + const res = c.body('', originalResponse) + const cookies = res.headers.getSetCookie() + expect(cookies.includes('context=1; Path=/')).toBe(true) + expect(cookies.includes('context=2; Path=/')).toBe(true) + expect(cookies.includes('response=1; Path=/')).toBe(true) + }) + it('c.text()', async () => { const originalResponse = new Response(JSON.stringify({ foo: 'bar' })) const res = c.text('foo', originalResponse) diff --git a/src/context.ts b/src/context.ts index 405c841b2..709196f98 100644 --- a/src/context.ts +++ b/src/context.ts @@ -2,7 +2,7 @@ import type { HonoRequest } from './request' import type { Env, FetchEventLike, Input, NotFoundHandler, TypedResponse } from './types' import { HtmlEscapedCallbackPhase, resolveCallback } from './utils/html' import type { RedirectStatusCode, StatusCode } from './utils/http-status' -import type { JSONValue, JSONParsed, IsAny, Simplify } from './utils/types' +import type { IsAny, JSONParsed, JSONValue, Simplify } from './utils/types' type HeaderRecord = Record export type Data = string | ArrayBuffer | ReadableStream @@ -380,7 +380,11 @@ export class Context< if (this.#headers) { // If the header is set by c.header() and arg.headers, c.header() will be prioritized. this.#headers.forEach((v, k) => { - header.set(k, v) + if (k === 'set-cookie') { + header.append(k, v) + } else { + header.set(k, v) + } }) } const headers = setHeaders(header, this.#preparedHeaders)