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: cache mechanism for request with different headers #8754

Merged

Conversation

alejogschwind
Copy link
Contributor

closes #8752

Please don't delete this checklist! Before submitting the PR, please make sure you do the following:

  • It's really useful if your PR references an issue where it is discussed ahead of time. In many cases, features are absent for a reason. For large changes, please create an RFC: https://github.com/sveltejs/rfcs
  • This message body should clearly illustrate what problems it solves.
  • Ideally, include a test that fails without this PR but passes with it.

Tests

  • Run the tests with pnpm test and lint the project with pnpm lint and pnpm check

Changesets

  • If your PR makes a change that should be noted in one or more packages' changelogs, generate a changeset by running pnpm changeset and following the prompts. Changesets that add features should be minor and those that fix bugs should be patch. Please prefix changeset messages with feat:, fix:, or chore:.

@changeset-bot
Copy link

changeset-bot bot commented Jan 27, 2023

🦋 Changeset detected

Latest commit: a885a67

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 1 package
Name Type
@sveltejs/kit Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

@benmccann
Copy link
Member

benmccann commented Jan 27, 2023

You would need to update packages/kit/src/runtime/server/page/serialize_data.js as well

Also, I think we could probably have just a single hash computed from a combination of the headers and body

@Rich-Harris
Copy link
Member

Thanks! Note that opts.headers can be any HeadersInit object, so if someone does this...

const headers = new Headers();
headers.append('Authorization', `Bearer ${token}`);
fetch('/blah', { headers });

...the hash will be all wrong because JSON.stringify(headers) === '{}'. The easiest solution would be along these lines (except combined with body, as @benmccann suggests):

if (opts?.headers) {
-  const headers = JSON.stringify(opts.headers);
+  const headers = [...new Headers(opts.headers)].join(',');
  selector += `[data-headers-hash="${hash(headers)}"]`;
}

@@ -262,10 +262,10 @@ test.describe('Load', () => {
const payload_b = '{"status":200,"statusText":"","headers":{},"body":"Y"}';
// by the time JS has run, hydration will have nuked these scripts
const script_contents_a = await page.innerHTML(
'script[data-sveltekit-fetched][data-url="/load/serialization-post.json"][data-hash="3t25"]'
'script[data-sveltekit-fetched][data-url="/load/serialization-post.json"][data-hash="4jrjph"]'
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I still didn't figure out why this test is failing. I updated the hash to consider headers but it seems that the hash now it's different than the test above so maybe there is some header that I'm not considering when I hash manually.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I found the issue, I was concatenating an ArrayBufferView + string.

To achieve this single hash computation I will need to update the hash function, I don't know if you want to do that. The other option is to concatenate the two hashes hash(headers) + hash(body)

@Rich-Harris Rich-Harris merged commit c14f3ae into sveltejs:master Jan 30, 2023
@Rich-Harris
Copy link
Member

thank you!

@github-actions github-actions bot mentioned this pull request Jan 30, 2023
dummdidumm pushed a commit that referenced this pull request Jul 4, 2023
- support caching of responses with `Vary` header (possible without any changes on the client because since #8754 we're taking headers into account for the cache key)
- fix browser caching of adjacent pages/endpoints

fixes #9780

---------

Co-authored-by: S. Elliott Johnson <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Caching issue with fetch response: Incorrect behavior observed
3 participants