Skip to content

Respect Cache-Control of serialized SSR fetch responses during subsequent client-side navigation #4625

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

Closed
Conduitry opened this issue Apr 15, 2022 · 2 comments · Fixed by #6461
Labels
breaking change feature / enhancement New feature or request p3-edge-case SvelteKit cannot be used in an uncommon way
Milestone

Comments

@Conduitry
Copy link
Member

Describe the problem

The fetch responses that are serialized as part of the SSR response are currently only used during the initial hydration of the app. If the same URLs are requested during subsequent client-side navigation, these requests are performed (at least) once more, regardless of the Cache-Control headers in the responses.

Describe the proposed solution

It would be nice if, even after the initial view is hydrated, the fetch function that's passed to load still checked with the serialized responses to see whether any of them are still valid. We should already have the information we need to make this decision, because the response headers for all of these requests are also serialized into the server-rendered HTML.

Alternatives considered

No response

Importance

would make my life easier

Additional Information

I have a fairly large chunk of data I want to expose throughout my app. When I request it in the root __layout load and expose it through stuff, this results in the data being requested again after navigating, regardless of the Cache-Control header in the response.

Currently, my workaround is to fetch the data during the handle hook and expose it through session. From here, the root __layout can then still expose it to the rest of the app with stuff without requiring widespread code changes.

@Conduitry Conduitry added feature / enhancement New feature or request p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc. labels Apr 15, 2022
@Conduitry
Copy link
Member Author

If possible, we should also ensure that any etag returned in these responses is manually added as if-none-match in the first request to a given URL, so we can also take advantage of that type of caching. I'm not sure whether browsers or the Fetch API let you control that.

@Rich-Harris Rich-Harris added this to the 1.0 milestone May 17, 2022
@Rich-Harris Rich-Harris removed the p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc. label May 17, 2022
@benmccann benmccann added the p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc. label Jul 19, 2022
@dummdidumm dummdidumm added p3-edge-case SvelteKit cannot be used in an uncommon way and removed p2-nice-to-have SvelteKit cannot be used by a small number of people, quality of life improvements, etc. labels Aug 26, 2022
@dummdidumm
Copy link
Member

I'm not sure if the added complexity of implementing this is worth it

dummdidumm added a commit that referenced this issue Aug 31, 2022
...for initially fetched responses
Closes #4625
The proposed etag logic is not implemented because it sounds too complex and would add needless code for 99% of users

This is a breaking change insofar that people could have relied on the old behavior somehow
Rich-Harris added a commit that referenced this issue Sep 1, 2022
* [breaking] respect cache-control max-age on the client

...for initially fetched responses
Closes #4625
The proposed etag logic is not implemented because it sounds too complex and would add needless code for 99% of users

This is a breaking change insofar that people could have relied on the old behavior somehow

* calculate ttl on the server

* oops

* oops

* no

* invalidate cache on POST

* fix

* simplify

* deflake, hopefully

* try to make test less flaky

* disable when prerendering

Co-authored-by: Rich Harris <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
breaking change feature / enhancement New feature or request p3-edge-case SvelteKit cannot be used in an uncommon way
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants