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

[fix] support etag W/ prefix #2709

Merged
merged 5 commits into from
Nov 23, 2021
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/giant-ghosts-give.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@sveltejs/kit': patch
---

[fix] support etag W/ prefix
8 changes: 7 additions & 1 deletion packages/kit/src/runtime/server/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,15 @@ export async function respond(incoming, options, state = {}) {
if (response.status === 200) {
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'];
// ignore W/ prefix https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/If-None-Match#directives
if (if_none_match_value?.startsWith('W/"')) {
if_none_match_value = if_none_match_value.substring(2);
}

const etag = `"${hash(response.body || '')}"`;

if (request.headers['if-none-match'] === etag) {
if (if_none_match_value === etag) {
return {
status: 304,
headers: {},
Expand Down
23 changes: 23 additions & 0 deletions packages/kit/test/apps/basics/src/routes/etag/_tests.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,27 @@ export default function (test) {
js: false
}
);

test(
'support W/ etag prefix',
null,
async ({ fetch }) => {
const r1 = await fetch('/etag/text');
const etag = r1.headers.get('etag');
assert.ok(!!etag);

const r2 = await fetch('/etag/text', {
headers: etag
? {
'if-none-match': `W/${etag}`
}
: {}
});

assert.equal(r2.status, 304);
},
{
js: false
}
);
}