-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
Move version into payload to avoid rotating client chunks #16271
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
Open
Axeldeblen
wants to merge
9
commits into
sveltejs:version-3
Choose a base branch
from
Axeldeblen:version-chunk-rotation-fix
base: version-3
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
53082a7
scaffold app to test chunk rotation bug
Axeldeblen 407c963
add classifiers and tests in prepartion for fix
Axeldeblen 0f179a7
rewrite payload to be package name + config base path + config appDir
Axeldeblen 5cc6d5f
seperate how we read the version in different client/server contexts
Axeldeblen 8caf74f
fallback to default
Axeldeblen 471be87
add edge case handling for embedded apps and name resolution
Axeldeblen b99a7e0
Merge branch 'version-3' into version-chunk-rotation-fix
Axeldeblen be25498
followup fixes from merge for targetting version-3
Axeldeblen db64f31
Merge branch 'version-3' into version-chunk-rotation-fix
Axeldeblen File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const version = /** @type {string} */ (__SVELTEKIT_PAYLOAD__?.version); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const version = __SVELTEKIT_APP_VERSION__; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| { | ||
| "name": "test-version-chunk-rotation-app", | ||
| "private": true, | ||
| "version": "0.0.0", | ||
| "type": "module", | ||
| "scripts": { | ||
| "dev": "vite dev", | ||
| "build": "svelte-kit sync && vite build", | ||
| "preview": "vite preview", | ||
| "prepare": "svelte-kit sync", | ||
| "check": "svelte-kit sync && tsc && svelte-check" | ||
| }, | ||
| "devDependencies": { | ||
| "@sveltejs/adapter-auto": "workspace:^", | ||
| "@sveltejs/kit": "workspace:^", | ||
| "@sveltejs/vite-plugin-svelte": "catalog:", | ||
| "svelte": "catalog:", | ||
| "svelte-check": "catalog:", | ||
| "typescript": "catalog:", | ||
| "vite": "catalog:" | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| declare global { | ||
| namespace App {} | ||
| } | ||
|
|
||
| export {}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| <!doctype html> | ||
| <html lang="en"> | ||
| <head> | ||
| <meta charset="utf-8" /> | ||
| <meta name="viewport" content="width=device-width, initial-scale=1" /> | ||
| %sveltekit.head% | ||
| </head> | ||
| <body> | ||
| <div>%sveltekit.body%</div> | ||
| </body> | ||
| </html> |
6 changes: 6 additions & 0 deletions
6
packages/kit/test/version-chunk-rotation/app/src/lib/shared.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| export const sharedMessage = 'shared between routes'; | ||
|
|
||
| /** @param {number} n */ | ||
| export function double(n) { | ||
| return n * 2; | ||
| } |
7 changes: 7 additions & 0 deletions
7
packages/kit/test/version-chunk-rotation/app/src/routes/+page.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <script> | ||
| import { double, sharedMessage } from '$lib/shared.js'; | ||
| </script> | ||
|
|
||
| <h1>home</h1> | ||
| <p>{sharedMessage}: {double(21)}</p> | ||
| <a href="/about">about</a> |
7 changes: 7 additions & 0 deletions
7
packages/kit/test/version-chunk-rotation/app/src/routes/about/+page.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| <script> | ||
| import { double, sharedMessage } from '$lib/shared.js'; | ||
| </script> | ||
|
|
||
| <h1>about</h1> | ||
| <p>{sharedMessage}: {double(50)}</p> | ||
| <a href="/">home</a> |
1 change: 1 addition & 0 deletions
1
packages/kit/test/version-chunk-rotation/app/src/routes/prerendered/+page.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| export const prerender = true; |
6 changes: 6 additions & 0 deletions
6
packages/kit/test/version-chunk-rotation/app/src/routes/prerendered/+page.svelte
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| <script> | ||
| import { sharedMessage } from '$lib/shared.js'; | ||
| </script> | ||
|
|
||
| <h1>prerendered</h1> | ||
| <p>{sharedMessage}</p> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| { | ||
| "compilerOptions": { | ||
| "allowJs": true, | ||
| "checkJs": true, | ||
| "noEmit": true | ||
| }, | ||
| "extends": "./.svelte-kit/tsconfig.json" | ||
| } |
32 changes: 32 additions & 0 deletions
32
packages/kit/test/version-chunk-rotation/app/vite.config.js
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| import * as path from 'node:path'; | ||
| import process from 'node:process'; | ||
| import { sveltekit } from '@sveltejs/kit/vite'; | ||
| import adapter from '../../../../adapter-auto/index.js'; | ||
|
|
||
| /** @type {import('vite').UserConfig} */ | ||
| const config = { | ||
| build: { | ||
| minify: false | ||
| }, | ||
|
|
||
| clearScreen: false, | ||
|
|
||
| logLevel: 'silent', | ||
|
|
||
| plugins: [ | ||
| sveltekit({ | ||
| adapter: adapter(), | ||
| version: { | ||
| name: process.env.SK_VERSION || Date.now().toString() | ||
| } | ||
| }) | ||
| ], | ||
|
|
||
| server: { | ||
| fs: { | ||
| allow: [path.resolve('../../../src')] | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| export default config; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| import { createHash } from 'node:crypto'; | ||
| import fs from 'node:fs'; | ||
| import path from 'node:path'; | ||
|
|
||
| /** | ||
| * @typedef {object} Chunk | ||
| * @property {string} bytes_hash | ||
| * @property {boolean} contains_version | ||
| */ | ||
|
|
||
| const VERSION_DERIVED_PAYLOAD_KEY = /__sveltekit_(?!dev\b)[A-Za-z0-9]+/; | ||
|
|
||
| /** @param {string} code */ | ||
| const bytes_hash = (code) => createHash('sha256').update(code).digest('hex').slice(0, 12); | ||
|
|
||
| /** | ||
| * @param {string} dir | ||
| * @param {string} [base] | ||
| * @param {string[]} [found] | ||
| * @returns {string[]} | ||
| */ | ||
| function js_files(dir, base = dir, found = []) { | ||
| for (const entry of fs.readdirSync(dir, { withFileTypes: true })) { | ||
| const abs = path.join(dir, entry.name); | ||
| if (entry.isDirectory()) js_files(abs, base, found); | ||
| else if (entry.name.endsWith('.js')) found.push(path.relative(base, abs)); | ||
| } | ||
| return found.sort(); | ||
| } | ||
|
|
||
| /** | ||
| * @param {string} dir | ||
| * @param {string} version | ||
| * @returns {Map<string, Chunk>} | ||
| */ | ||
| export function snapshot(dir, version) { | ||
| /** @type {Map<string, Chunk>} */ | ||
| const chunks = new Map(); | ||
|
|
||
| for (const filename of js_files(dir)) { | ||
| const code = fs.readFileSync(path.join(dir, filename), 'utf-8'); | ||
|
|
||
| chunks.set(filename, { | ||
| bytes_hash: bytes_hash(code), | ||
| contains_version: code.includes(version) || VERSION_DERIVED_PAYLOAD_KEY.test(code) | ||
| }); | ||
| } | ||
|
|
||
| return chunks; | ||
| } | ||
|
|
||
| /** | ||
| * @param {Map<string, Chunk>} before | ||
| * @param {Map<string, Chunk>} after | ||
| */ | ||
| export function compare(before, after) { | ||
| /** @type {Array<{ name: string, contains_version: boolean }>} */ | ||
| const rotated = []; | ||
| /** @type {Array<{ name: string, contains_version: boolean }>} */ | ||
| const mutable = []; | ||
|
|
||
| for (const [name, chunk] of before) { | ||
| const replacement = after.get(name); | ||
| if (!replacement) { | ||
| rotated.push({ name, contains_version: chunk.contains_version }); | ||
| } else if (replacement.bytes_hash !== chunk.bytes_hash) { | ||
| mutable.push({ | ||
| name, | ||
| contains_version: chunk.contains_version || replacement.contains_version | ||
| }); | ||
| } | ||
| } | ||
|
|
||
| return { total: before.size, rotated, mutable }; | ||
| } | ||
|
|
||
| /** @param {ReturnType<typeof compare>} result */ | ||
| export function format_report(result) { | ||
| /** @param {{ name: string, contains_version: boolean }} finding */ | ||
| const line = ({ name, contains_version }) => | ||
| `${name}${contains_version ? ' ← contains the version' : ''}`; | ||
|
|
||
| return [ | ||
| `${result.total} chunks total · ${result.rotated.length} rotated · ${result.mutable.length} mutable (same name, new bytes)`, | ||
| ...result.mutable.map((finding) => ` MUTABLE ${line(finding)}`), | ||
| ...result.rotated.map((finding) => ` ROTATED ${line(finding)}`) | ||
| ].join('\n'); | ||
| } |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unresolved git merge conflict markers were committed in
packages/kit/src/core/utils.js, producing invalid JavaScript that breaks parsing/build of the entire kit package.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This needs to be resolved
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. There's a suggested fix on this thread that removes the three conflict markers while keeping the new
payload_hashfunction and using theversion-3wording for theruntime_directoryJSDoc — applying it resolves the conflict cleanly.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed. There's a committed fix suggestion on this comment (
merge-conflict-markers-utils) that removes all three conflict markers — it keeps the newpayload_hashfunction and uses theversion-3wording (Resolved path of theruntimedirectory posix-ified) for theruntime_directoryJSDoc. Applying it resolves the conflict and restores valid JS.