Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Don't overwrite custom etag #3348

Merged
merged 6 commits into from
Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/clever-readers-turn.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

Preserve explicit ETag header
5 changes: 3 additions & 2 deletions packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -122,8 +122,9 @@ export async function respond(incoming, options, state = {}) {
: await render_page(request, route, match, options, state, ssr);

if (response) {
// inject ETags for 200 responses
if (response.status === 200) {
// inject ETags for 200 responses, if the endpoint
// doesn't have its own ETag handling
if (response.status === 200 && !response.headers.etag) {
const cache_control = get_single_valued_header(response.headers, 'cache-control');
if (!cache_control || !/(no-store|immutable)/.test(cache_control)) {
let if_none_match_value = request.headers['if-none-match'];
Expand Down
10 changes: 10 additions & 0 deletions packages/kit/test/apps/basics/src/routes/etag/custom.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/** @type {import('@sveltejs/kit').RequestHandler} */
export function get({ headers }) {
if (headers['if-none-match'] === '@1234@') return { status: 304 };
return {
body: `${Math.random()}`,
headers: {
etag: '@1234@'
}
};
}
14 changes: 14 additions & 0 deletions packages/kit/test/apps/basics/test/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -732,6 +732,20 @@ test.describe.parallel('ETags', () => {

expect(r2.status()).toBe(304);
});

test('custom etag', async ({ request }) => {
const r1 = await request.get('/etag/custom');
const etag = r1.headers()['etag'];
expect(etag).toBe('@1234@');

const r2 = await request.get('/etag/custom', {
headers: {
'if-none-match': '@1234@'
}
});

expect(r2.status()).toBe(304);
});
});

test.describe.parallel('Headers', () => {
Expand Down