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

304 responses should contain etag #3286

Closed
Maggi64 opened this issue Jan 11, 2022 · 4 comments · Fixed by #3313
Closed

304 responses should contain etag #3286

Maggi64 opened this issue Jan 11, 2022 · 4 comments · Fixed by #3313
Labels
bug Something isn't working help wanted PRs welcomed. The implementation details are unlikely to cause debate p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc.

Comments

@Maggi64
Copy link

Maggi64 commented Jan 11, 2022

Describe the bug

Currently the correct headers are missing on 304 responses.

The IETF standards i founds say this:
The server generating a 304 response MUST generate any of the following header fields that would have been sent in a 200 (OK) response to the same request: Cache-Control, Content-Location, Date, ETag, Expires, and Vary.
https://datatracker.ietf.org/doc/html/rfc7232#section-4.1

if (response) {
// inject ETags for 200 responses
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 (if_none_match_value === etag) {
return {
status: 304,
headers: {}
};
}
response.headers['etag'] = etag;
}
}

For me this causes cloudflare caching to not revalidate my api responses correctly when a cache-control: max-age=60 header was set. Cloudflare support was so kind to point me to this issue.

Reproduction

Can create a repo if requested

Logs

No response

System Info

"@sveltejs/adapter-node": "1.0.0-next.61",
"@sveltejs/kit": "1.0.0-next.222",

Severity

serious, but I can work around it

Additional Information

I would suggest the following change to fix this issue.

const etag = `"${hash(response.body || '')}"`;
response.headers['etag'] = etag
if (if_none_match_value === etag) {
  return {
    status: 304,
    headers: response.headers
  };
}								
@Conduitry
Copy link
Member

Interesting, I wasn't aware of this. It does indeed sound in the spec that we should be returning an ETag here. Thanks for the report!

@Conduitry Conduitry added bug Something isn't working p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc. help wanted PRs welcomed. The implementation details are unlikely to cause debate labels Jan 11, 2022
@vicky1999
Copy link
Contributor

vicky1999 commented Jan 11, 2022

Can I work on this issue?

@bluwy
Copy link
Member

bluwy commented Jan 12, 2022

@vicky1999 feel free if no one created a PR yet.

@bluwy bluwy linked a pull request Jan 13, 2022 that will close this issue
5 tasks
@bluwy
Copy link
Member

bluwy commented Jan 13, 2022

#3133 fixed this

@bluwy bluwy closed this as completed Jan 13, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working help wanted PRs welcomed. The implementation details are unlikely to cause debate p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants