Can't cache a page load (setting "cache-control" with `setHeaders` doesn't seem to work)
See original GitHub issueDescribe the bug
For example, setting the cache here has no effect.
export const load: PageLoad = async ({ fetch, setHeaders }) => {
const res = await fetch(`/api/users`);
if (!res.ok) throw error(res.status);
const users: UserV2[] = await res.json();
setHeaders({
'cache-control': 'max-age=86400',
});
return {
users,
};
};
Maybe it’s because the initial request to the page contains “Cache-Control: max-age=0”?
So the current behavior is that even if I set the “cache-control” header, the request is always processed by the server (instead of being served by disk/memory cache).
The expected behaviour is that the request is served from cache after being cached (obviously)
Reproduction
Unfortunately, I can’t really debug “cache-control” using node.new 🤔 The network panel doesn’t give me access to the “sandboxed” requests.
Logs
Raw response headers
HTTP/1.1 304 Not Modified
Access-Control-Allow-Origin: *
cache-control: max-age=86400
etag: "1ons99h"
Date: Sun, 02 Oct 2022 23:00:59 GMT
Connection: keep-alive
Keep-Alive: timeout=5
Raw request headers
GET /top-secret HTTP/1.1
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cache-Control: max-age=0
Connection: keep-alive
Cookie: ...
Host: localhost:3000
If-None-Match: "1ons99h"
Referer: http://localhost:3000/top-secret
Sec-Fetch-Dest: document
Sec-Fetch-Mode: navigate
Sec-Fetch-Site: same-origin
Sec-GPC: 1
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/106.0.0.0 Safari/537.36
System Info
System:
OS: macOS 12.6
CPU: (8) arm64 Apple M2
Memory: 2.30 GB / 24.00 GB
Shell: 5.8.1 - /bin/zsh
Binaries:
Node: 18.7.0 - ~/.nvm/versions/node/v18.7.0/bin/node
npm: 8.16.0 - ~/.nvm/versions/node/v18.7.0/bin/npm
Browsers:
Brave Browser: 106.1.44.105
Safari: 16.0
npmPackages:
@sveltejs/adapter-node: ^1.0.0-next.96 => 1.0.0-next.96
@sveltejs/kit: ^1.0.0-next.504 => 1.0.0-next.504
svelte: ^3.50.1 => 3.50.1 `
Severity
serious, but I can work around it
Additional Information
I guess a temporary workaround is to do in-memory caching?
Issue Analytics
- State:
- Created a year ago
- Reactions:1
- Comments:6 (4 by maintainers)
Top Results From Across the Web
How to set HTTP headers (for cache-control)? - Stack Overflow
To use cache-control in HTML, you use the meta tag, e.g. <meta http-equiv="Cache-control" content="public">. The value in the content field ...
Read more >+page.server.js no longer uses/respects setHeaders #6477
This js file response no longer uses the headers set with the setHeaders function. This means we can't use cache-control etc.
Read more >Caching headers: A practical guide for frontend developers
For a single page application, I'm interested in caching JavaScript, CSS, fonts, and image files indefinitely and preventing caching HTML files ...
Read more >Cache-Control - How to Properly Configure It - KeyCDN Support
Cache -Control is an HTTP cache header comprised of directives that allow you to define when and how a response should be cached....
Read more >Cache-Control - HTTP - MDN Web Docs
The Cache-Control HTTP header field holds directives (instructions) — in both ... This usually means the response can't be reused as-is.
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Yes, it’s all explained in https://github.com/sveltejs/kit/issues/6477#issuecomment-1255168104.
I’ve just added
stringify
andparse
functions todevalue
: https://github.com/Rich-Harris/devalue#stringify-and-parse. This would allow us to use a more conventional approach to serializing non-JSON-stringifiable values if we adopted it in SvelteKit. Payloads would get slightly larger, and we’d have to add 0.5kb of parsing logic, but the benefits probably outweigh the costs.Yeah, I think we fixed it a while back but didn’t close this issue. Closing now