0
Fork 0
mirror of https://github.com/withastro/astro.git synced 2024-12-30 22:03:56 -05:00

fix(ssr): duplicate custom headers (#12518)

* fix(ssr): duplicate custom headers

* rebase
This commit is contained in:
Emanuele Stoppa 2024-12-05 13:18:00 +00:00 committed by GitHub
parent d14d967b5f
commit e216250146
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 71 additions and 4 deletions

View file

@ -0,0 +1,5 @@
---
'astro': patch
---
Fixes an issue where SSR error pages would return duplicated custom headers.

View file

@ -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,
});
}

View file

@ -0,0 +1,8 @@
{
"name": "@test/ssr",
"version": "0.0.0",
"private": true,
"dependencies": {
"astro": "workspace:*"
}
}

View file

@ -0,0 +1,8 @@
---
return new Response("oops", {
status: 500,
headers: new Headers({
"X-Debug": "1234",
}),
});
---

View file

@ -0,0 +1,6 @@
<html>
<head><title>Hello world</title></head>
<body>
<h1>Hello world</h1>
</body>
</html>

View file

@ -4,6 +4,34 @@ import * as cheerio from 'cheerio';
import testAdapter from './test-adapter.js';
import { loadFixture } from './test-utils.js';
describe('Default 500 page', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;
/** @type {import('./test-utils.js').App} */
let app;
before(async () => {
fixture = await loadFixture({
root: './fixtures/ssr-error/',
output: 'server',
adapter: testAdapter(),
// test suite was authored when inlineStylesheets defaulted to never
build: { inlineStylesheets: 'never' },
});
await fixture.build({});
app = await fixture.loadTestAdapterApp();
});
it('should correctly merge headers coming from the original response and the 500 response, when calling a catch-all route', async () => {
const request = new Request('http://example.com/any');
const response = await app.render(request);
assert.equal(response.status, 500);
assert.equal(response.headers.get('x-debug'), '1234');
const html = await response.text();
assert.match(html, /oops/);
});
});
describe('404 and 500 pages', () => {
/** @type {import('./test-utils.js').Fixture} */
let fixture;

View file

@ -3825,6 +3825,12 @@ importers:
specifier: ^10.25.0
version: 10.25.0
packages/astro/test/fixtures/ssr-error:
dependencies:
astro:
specifier: workspace:*
version: link:../../..
packages/astro/test/fixtures/ssr-error-pages:
dependencies:
astro: