Skip to content

Commit

Permalink
preserve headers per the spec
Browse files Browse the repository at this point in the history
  • Loading branch information
Rich-Harris committed Jan 13, 2022
1 parent 57963c3 commit d749576
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 2 deletions.
19 changes: 18 additions & 1 deletion packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -129,11 +129,28 @@ export async function respond(incoming, options, state = {}) {
const etag = `"${hash(response.body || '')}"`;

if (if_none_match_value === etag) {
/** @type {import('types/helper').ResponseHeaders} */
const headers = { etag };

// https://datatracker.ietf.org/doc/html/rfc7232#section-4.1
for (const key of [
'cache-control',
'content-location',
'date',
'expires',
'vary'
]) {
if (key in response.headers) {
headers[key] = /** @type {string} */ (response.headers[key]);
}
}

return {
status: 304,
headers: { etag }
headers
};
}

response.headers['etag'] = etag;
}
}
Expand Down
5 changes: 4 additions & 1 deletion packages/kit/test/apps/basics/src/routes/etag/text.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
/** @type {import('@sveltejs/kit').RequestHandler} */
export function get() {
return {
body: 'some text'
body: 'some text',
headers: {
expires: 'yesterday'
}
};
}
1 change: 1 addition & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,7 @@ test.describe.parallel('ETags', () => {
});

expect(r2.status()).toBe(304);
expect(r2.headers()['expires']).toBe('yesterday');
});

test('generates etag/304 for binary body', async ({ request }) => {
Expand Down

0 comments on commit d749576

Please sign in to comment.