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

/_app/version.json is marked immutable and cached forever #4837

Closed
georgecrawford opened this issue May 6, 2022 · 6 comments · Fixed by #5051
Closed

/_app/version.json is marked immutable and cached forever #4837

georgecrawford opened this issue May 6, 2022 · 6 comments · Fixed by #5051
Labels
bug Something isn't working p1-important SvelteKit cannot be used by a large number of people, basic functionality is missing, etc.
Milestone

Comments

@georgecrawford
Copy link
Contributor

georgecrawford commented May 6, 2022

Describe the bug

As mentioned in #4188 (comment), I suspect recent changes to the way that /_app/version.json is served have caused it to be treated as a static asset.

Reproduction

See this simple repro: https://stackblitz.com/edit/sveltejs-kit-template-default-nwjzok?file=package.json&terminal=dev

The offending network requests are visible every 10 seconds:

Example request
curl 'https://sveltejs-kit-template-default-nwjzok--3000.local.webcontainer.io/_app/version.json' \
  -H 'sec-ch-ua: " Not A;Brand";v="99", "Chromium";v="100", "Google Chrome";v="100"' \
  -H 'pragma: no-cache' \
  -H 'cache-control: no-cache' \
  -H 'Referer: https://sveltejs-kit-template-default-nwjzok--3000.local.webcontainer.io/' \
  -H 'sec-ch-ua-mobile: ?0' \
  -H 'User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36' \
  -H 'sec-ch-ua-platform: "macOS"' \
  --compressed
Example response headers
cache-control: public,max-age=31536000,immutable
connection: keep-alive
content-length: 24
content-type: application/json
date: Fri, 06 May 2022 08:28:08 GMT
etag: W/"24-1651825319850"
keep-alive: timeout=5
last-modified: Fri, 06 May 2022 08:21:59 GMT
vary: Accept-Encoding

A immutable cached response for version.json means my app will always believe there's an upgrade pending, as the browser has cached the initial version response.

Logs

No response

System Info

See StackBlitz container - the npx envinfo command didn't run to completion.

	"devDependencies": {
		"@sveltejs/adapter-node": "^1.0.0-next.73",
		"@sveltejs/kit": "next",
		"@types/cookie": "^0.4.1",
		"prettier": "^2.5.1",
		"prettier-plugin-svelte": "^2.5.0",
		"svelte": "^3.46.0",
		"svelte-check": "^2.2.6",
		"typescript": "~4.6.2"
	},

Severity

blocking an upgrade

Additional Information

No response

@benmccann benmccann added bug Something isn't working p1-important SvelteKit cannot be used by a large number of people, basic functionality is missing, etc. labels May 6, 2022
@benmccann benmccann added this to the 1.0 milestone May 6, 2022
@geoffrich
Copy link
Member

Looks like #3222 assumed that everything in _app/ would be a hashed asset, but version.json is not.

@georgecrawford
Copy link
Contributor Author

georgecrawford commented May 6, 2022

Thanks @geoffrich, that’s the problem for sure. I don’t know what the team’s definition of ‘_app’ is, so I’ll wait for someone to advise rather than open a speculative PR.

@Rich-Harris
Copy link
Member

Are you experiencing buggy behaviour, or is it just that you don't expect the immutable header? Everything in _app is indeed marked as immutable, but it should work anyway because version.json is fetched with no-cache:

const file = import.meta.env.VITE_SVELTEKIT_APP_VERSION_FILE;
const res = await fetch(`${assets}/${file}`, {
headers: {
pragma: 'no-cache',
'cache-control': 'no-cache'
}
});

@bjon
Copy link
Contributor

bjon commented May 12, 2022

@Rich-Harris Can confirm it doesn't work behind a CDN. File never updates (tested with Google Cloud CDN).

@georgecrawford
Copy link
Contributor Author

Are you experiencing buggy behaviour, or is it just that you don't expect the immutable header? Everything in _app is indeed marked as immutable, but it should work anyway because version.json is fetched with no-cache:

const file = import.meta.env.VITE_SVELTEKIT_APP_VERSION_FILE;
const res = await fetch(`${assets}/${file}`, {
headers: {
pragma: 'no-cache',
'cache-control': 'no-cache'
}
});

Thanks for this. I think I might be wrong - I wasn't aware the request could cache-bust an immutable resource like this. TBH, I can't find this trick explicitly documented anywhere online, and the version is immutable statement seems all wrong to me - almost by definition, the version will change over time.

I did however see some buggy behaviour, but it could be with userland code. I'll try and get a reduced test case.

@Rich-Harris
Copy link
Member

Ah, interesting — I thought if a CDN received a no-cache request it would go back to the origin server. That's... surprising. I guess we'll need to do something like move everything inside _app that isn't version.json into something like _app/build, and only apply immutable cache headers to that directory.

Rich-Harris added a commit that referenced this issue May 24, 2022
Rich-Harris added a commit that referenced this issue May 24, 2022
* put build inside _app/build directory

* use free variable for native fetch

* update test

* only serve _app/build with immutable cache header, not _app/version.json - fixes #4837

* Update .changeset/wise-berries-flash.md

* fix is_static_file

* rename /build/ to /immutable/

* Update packages/kit/src/core/preview/index.js

* Update packages/kit/src/core/build/build_client.js

* Update packages/adapter-netlify/src/edge.js

* Update .changeset/wise-berries-flash.md

* update test

* fix cloudflare adapters
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working p1-important SvelteKit cannot be used by a large number of people, basic functionality is missing, etc.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants