Move version into payload to avoid rotating client chunks#16271
Move version into payload to avoid rotating client chunks#16271Axeldeblen wants to merge 10 commits into
Conversation
creating the test harness to validate client chunk rotation issue by testing chunk file names as well as chunk content
In the client we now read via the global version property rather than changing the name of the global object itself which was the source of bundle rotations
|
Install the latest version of pnpm add https://pkg.svelte.dev/@sveltejs/kit/c/37b34b0afcf35dc139ec60ae351a4eb2e4699fd9Open in Note This PR is from a fork. A maintainer must approve approve each commit before it can be built and installed. |
|
|
We might be able to solve this differently - moving the global access outside the modules that rarely change -; working on something locally. Then we can compare solutions. |
|
Going to close this in favour of #16324 which solves this without the need for hashing the app version |
…#16324) The global `__sveltekit__abc123` is removed from the JS code in the client runtime, it now only exists in the on-the-fly-generated rendered HTML for app initialization and streaming chunks. That way the SvelteKit runtime stays stable between redeploys as long as it doesn't change, resulting in more cache hits. It's achieve by creating a new payload file, which the global value is passed to, and the other files then use the exported variable to get it. Because some eagerly require it we have to split up the dynamic imports. This should not matter because preloading loads and parses them in parallel anyway. Fixes #12260 Alternative to #16271 --------- Co-authored-by: Tee Ming Chew <chewteeming01@gmail.com>
closes
#12260
#9576
Somewhat adjacent to #14176
Context
For context we're running a svelte kit app and deploying many times per day. Currently svelte kit places the version into the client bundle, causing the chunks to rotate their hashes invalidating the caching whenever we deploy as described in #12260. This contributes to a degraded user experience in our case .e.g. a user will open a modal but the chunk will 404's the client chunks because the server version has rotated. We're using our git SHA as our version.
Description
This PR aims to solve the bundle rotating by refactoring the mechanism of how svelte kit defines and deliver the version within each client / server context.
Here is a simple visual flow of how the current
versionleaks into the client bundle via two seperate sources.[see](https://vite.dev/config/shared-options#define) for how
__SVELTEKIT_PAYLOAD__is replaced by viteBecause the client uses the version hash as part of the global definition we need to keep it stable if we want to avoid the cascade effect.
For that reason we are using a combination of config values to avoid any global collisions in embedded sveltekit version like in #14176
(Open to other suggestions on other combinations here)
Now the
version_hashis stable on globalThis between deploys we pass the actual version through the payload.Then we pass the version as a prop within the payload
N.B Perhaps
version_hashneeds to be renamed now as our definitions of "version" diverge a bit. I'd like to get a bit more feedback on the general approach here before continuing further refactoring.Tests
I've added some tests to demonstrate the current issue. In these tests we build multiple versions of the app with different version inputs and compare the content of the built chunks to avoid running into situations similar to #12779 (even though we are not using post build plugins).
Production testing
Our app is currently pinned to a slightly older version of kit but we're happy to test the core of these changes out on a subset of production traffic to help verify.
Please don't delete this checklist! Before submitting the PR, please make sure you do the following:
Tests
pnpm testand lint the project withpnpm lintandpnpm checkChangesets
pnpm changesetand following the prompts. Changesets that add features should beminorand those that fix bugs should bepatch. Please prefix changeset messages withfeat:,fix:, orchore:.I haven't made a change set yet but I will on confirmation the maintainers are happy with the direction of this PR.
Edits