diff --git a/.changeset/proud-wombats-mate.md b/.changeset/proud-wombats-mate.md new file mode 100644 index 000000000000..2b80d3a7821e --- /dev/null +++ b/.changeset/proud-wombats-mate.md @@ -0,0 +1,5 @@ +--- +'astro': patch +--- + +Fixes an issue where SSR error pages would return duplicated custom headers. diff --git a/packages/astro/src/core/app/index.ts b/packages/astro/src/core/app/index.ts index 0e776ae9f2c6..26a6523d8ec9 100644 --- a/packages/astro/src/core/app/index.ts +++ b/packages/astro/src/core/app/index.ts @@ -439,6 +439,15 @@ export class App { // this function could throw an error... originalResponse.headers.delete('Content-type'); } catch {} + // we use a map to remove duplicates + const mergedHeaders = new Map([ + ...Array.from(newResponse.headers), + ...Array.from(originalResponse.headers), + ]); + const newHeaders = new Headers(); + for (const [name, value] of mergedHeaders) { + newHeaders.set(name, value); + } return new Response(newResponse.body, { status, statusText: status === 200 ? newResponse.statusText : originalResponse.statusText, @@ -447,10 +456,7 @@ export class App { // If users see something weird, it's because they are setting some headers they should not. // // Although, we don't want it to replace the content-type, because the error page must return `text/html` - headers: new Headers([ - ...Array.from(newResponse.headers), - ...Array.from(originalResponse.headers), - ]), + headers: newHeaders, }); } diff --git a/packages/astro/test/fixtures/ssr-error/package.json b/packages/astro/test/fixtures/ssr-error/package.json new file mode 100644 index 000000000000..ee13c03790c2 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-error/package.json @@ -0,0 +1,8 @@ +{ + "name": "@test/ssr", + "version": "0.0.0", + "private": true, + "dependencies": { + "astro": "workspace:*" + } +} diff --git a/packages/astro/test/fixtures/ssr-error/src/pages/[...slug].astro b/packages/astro/test/fixtures/ssr-error/src/pages/[...slug].astro new file mode 100644 index 000000000000..1fbb67e0c776 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-error/src/pages/[...slug].astro @@ -0,0 +1,8 @@ +--- +return new Response("oops", { + status: 500, + headers: new Headers({ + "X-Debug": "1234", + }), +}); +--- diff --git a/packages/astro/test/fixtures/ssr-error/src/pages/index.astro b/packages/astro/test/fixtures/ssr-error/src/pages/index.astro new file mode 100644 index 000000000000..9509e01e50f3 --- /dev/null +++ b/packages/astro/test/fixtures/ssr-error/src/pages/index.astro @@ -0,0 +1,6 @@ + +