-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
feat: snapshots #8710
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
Merged
Merged
feat: snapshots #8710
Changes from 10 commits
Commits
Show all changes
27 commits
Select commit
Hold shift + click to select a range
f4a5e05
add failing test
Rich-Harris 8e2dab7
make it possible to read snapshot props
Rich-Harris c43ddf2
refactor sessionStorage stuff a bit
Rich-Harris c25970f
snapshots
Rich-Harris 201592a
working
Rich-Harris 1c9760a
beef up test
Rich-Harris 0ce333f
tidy up
Rich-Harris 58a8ae6
fix type
Rich-Harris 808b821
run all tests
Rich-Harris 4821267
err....
Rich-Harris 764480d
lint
Rich-Harris 0b25fef
account for missing layouts etc
Rich-Harris 665fcb3
thank you, past us, for creating accessors for export const automatic…
Rich-Harris dea4d35
don't restore if navigation was blocked
Rich-Harris 25994a8
update scroll position at same time as snapshot
Rich-Harris 622b568
Merge branch 'master' into snapshot
Rich-Harris 75cfbcf
DRY
Rich-Harris fe62d2b
types
Rich-Harris 130677e
docs
Rich-Harris 61a2e63
changeset
Rich-Harris a5ec3cb
merge
Rich-Harris 30f210d
add a comment
Rich-Harris 6c642ee
only capture snapshot when appropriate
Rich-Harris a71098c
Update packages/kit/src/runtime/client/client.js
Rich-Harris 5845bf2
document caveat around large snapshots
Rich-Harris d924884
destroy alternate timelines
Rich-Harris b4a6f25
fix
Rich-Harris 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
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -15,6 +15,7 @@ import { | |||||
| is_external_url, | ||||||
| scroll_state | ||||||
| } from './utils.js'; | ||||||
| import * as storage from './session-storage.js'; | ||||||
| import { | ||||||
| lock_fetch, | ||||||
| unlock_fetch, | ||||||
|
|
@@ -30,7 +31,7 @@ import { HttpError, Redirect } from '../control.js'; | |||||
| import { stores } from './singletons.js'; | ||||||
| import { unwrap_promises } from '../../utils/promises.js'; | ||||||
| import * as devalue from 'devalue'; | ||||||
| import { INDEX_KEY, PRELOAD_PRIORITIES, SCROLL_KEY } from './constants.js'; | ||||||
| import { INDEX_KEY, PRELOAD_PRIORITIES, SCROLL_KEY, SNAPSHOT_KEY } from './constants.js'; | ||||||
| import { validate_common_exports } from '../../utils/exports.js'; | ||||||
| import { compact } from '../../utils/array.js'; | ||||||
|
|
||||||
|
|
@@ -51,12 +52,10 @@ default_error_loader(); | |||||
|
|
||||||
| /** @typedef {{ x: number, y: number }} ScrollPosition */ | ||||||
| /** @type {Record<number, ScrollPosition>} */ | ||||||
| let scroll_positions = {}; | ||||||
| try { | ||||||
| scroll_positions = JSON.parse(sessionStorage[SCROLL_KEY]); | ||||||
| } catch { | ||||||
| // do nothing | ||||||
| } | ||||||
| const scroll_positions = storage.get(SCROLL_KEY) ?? {}; | ||||||
|
|
||||||
| /** @type {Record<string, any[]>} */ | ||||||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
I see |
||||||
| const snapshots = storage.get(SNAPSHOT_KEY) ?? {}; | ||||||
|
|
||||||
| /** @param {number} index */ | ||||||
| function update_scroll_positions(index) { | ||||||
|
|
@@ -75,6 +74,9 @@ export function create_client({ target, base }) { | |||||
| /** @type {Array<((url: URL) => boolean)>} */ | ||||||
| const invalidated = []; | ||||||
|
|
||||||
| /** @type {import('svelte').SvelteComponent[]} */ | ||||||
| const components = []; | ||||||
|
dummdidumm marked this conversation as resolved.
|
||||||
|
|
||||||
| /** @type {{id: string, promise: Promise<import('./types').NavigationResult>} | null} */ | ||||||
| let load_cache = null; | ||||||
|
|
||||||
|
|
@@ -123,6 +125,8 @@ export function create_client({ target, base }) { | |||||
| ); | ||||||
| } | ||||||
|
|
||||||
| const initial_history_index = current_history_index; | ||||||
|
|
||||||
| // if we reload the page, or Cmd-Shift-T back to it, | ||||||
| // recover scroll position | ||||||
| const scroll = scroll_positions[current_history_index]; | ||||||
|
|
@@ -238,11 +242,20 @@ export function create_client({ target, base }) { | |||||
| * @param {import('./types').NavigationIntent | undefined} intent | ||||||
| * @param {URL} url | ||||||
| * @param {string[]} redirect_chain | ||||||
| * @param {number} [previous_history_index] | ||||||
| * @param {{hash?: string, scroll: { x: number, y: number } | null, keepfocus: boolean, details: { replaceState: boolean, state: any } | null}} [opts] | ||||||
| * @param {{}} [nav_token] To distinguish between different navigation events and determine the latest. Needed for example for redirects to keep the original token | ||||||
| * @param {() => void} [callback] | ||||||
| */ | ||||||
| async function update(intent, url, redirect_chain, opts, nav_token = {}, callback) { | ||||||
| async function update( | ||||||
| intent, | ||||||
| url, | ||||||
| redirect_chain, | ||||||
| previous_history_index, | ||||||
| opts, | ||||||
| nav_token = {}, | ||||||
| callback | ||||||
| ) { | ||||||
| token = nav_token; | ||||||
| let navigation_result = intent && (await load_route(intent)); | ||||||
|
|
||||||
|
|
@@ -301,6 +314,11 @@ export function create_client({ target, base }) { | |||||
|
|
||||||
| updating = true; | ||||||
|
|
||||||
| // `previous_history_index` will be undefined for invalidation | ||||||
| if (previous_history_index) { | ||||||
| snapshots[previous_history_index] = components.map((c) => c.snapshot?.capture()); | ||||||
| } | ||||||
|
|
||||||
| if (opts && opts.details) { | ||||||
| const { details } = opts; | ||||||
| const change = details.replaceState ? 0 : 1; | ||||||
|
|
@@ -383,10 +401,14 @@ export function create_client({ target, base }) { | |||||
|
|
||||||
| root = new Root({ | ||||||
| target, | ||||||
| props: { ...result.props, stores }, | ||||||
| props: { ...result.props, stores, components }, | ||||||
| hydrate: true | ||||||
| }); | ||||||
|
|
||||||
| snapshots[current_history_index]?.forEach((value, i) => { | ||||||
| components[i].snapshot?.restore(value); | ||||||
| }); | ||||||
|
|
||||||
| /** @type {import('types').AfterNavigate} */ | ||||||
| const navigation = { | ||||||
| from: null, | ||||||
|
|
@@ -444,7 +466,7 @@ export function create_client({ target, base }) { | |||||
| }, | ||||||
| props: { | ||||||
| // @ts-ignore Somehow it's getting SvelteComponent and SvelteComponentDev mixed up | ||||||
| components: compact(branch).map((branch_node) => branch_node.node.component) | ||||||
| constructors: compact(branch).map((branch_node) => branch_node.node.component) | ||||||
| } | ||||||
| }; | ||||||
|
|
||||||
|
|
@@ -1081,8 +1103,11 @@ export function create_client({ target, base }) { | |||||
| return; | ||||||
| } | ||||||
|
|
||||||
| // TODO update scroll positions at same time as snapshot? | ||||||
| update_scroll_positions(current_history_index); | ||||||
|
|
||||||
| const previous_history_index = current_history_index; | ||||||
|
|
||||||
| accepted(); | ||||||
|
|
||||||
| navigating = true; | ||||||
|
|
@@ -1095,6 +1120,7 @@ export function create_client({ target, base }) { | |||||
| intent, | ||||||
| url, | ||||||
| redirect_chain, | ||||||
| previous_history_index, | ||||||
| { | ||||||
| scroll, | ||||||
| keepfocus, | ||||||
|
|
@@ -1373,12 +1399,10 @@ export function create_client({ target, base }) { | |||||
| addEventListener('visibilitychange', () => { | ||||||
| if (document.visibilityState === 'hidden') { | ||||||
| update_scroll_positions(current_history_index); | ||||||
| storage.set(SCROLL_KEY, scroll_positions); | ||||||
|
|
||||||
| try { | ||||||
| sessionStorage[SCROLL_KEY] = JSON.stringify(scroll_positions); | ||||||
| } catch { | ||||||
| // do nothing | ||||||
| } | ||||||
| snapshots[current_history_index] = components.map((c) => c.snapshot?.capture()); | ||||||
| storage.set(SNAPSHOT_KEY, snapshots); | ||||||
| } | ||||||
| }); | ||||||
|
|
||||||
|
|
@@ -1525,15 +1549,15 @@ export function create_client({ target, base }) { | |||||
| }); | ||||||
| }); | ||||||
|
|
||||||
| addEventListener('popstate', (event) => { | ||||||
| addEventListener('popstate', async (event) => { | ||||||
| if (event.state?.[INDEX_KEY]) { | ||||||
| // if a popstate-driven navigation is cancelled, we need to counteract it | ||||||
| // with history.go, which means we end up back here, hence this check | ||||||
| if (event.state[INDEX_KEY] === current_history_index) return; | ||||||
|
|
||||||
| const delta = event.state[INDEX_KEY] - current_history_index; | ||||||
|
|
||||||
| navigate({ | ||||||
| await navigate({ | ||||||
| url: new URL(location.href), | ||||||
| scroll: scroll_positions[event.state[INDEX_KEY]], | ||||||
| keepfocus: false, | ||||||
|
|
@@ -1548,6 +1572,10 @@ export function create_client({ target, base }) { | |||||
| type: 'popstate', | ||||||
| delta | ||||||
| }); | ||||||
|
|
||||||
| snapshots[current_history_index].forEach((value, i) => { | ||||||
| components[i].snapshot?.restore(value); | ||||||
| }); | ||||||
| } | ||||||
| }); | ||||||
|
|
||||||
|
|
||||||
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,25 @@ | ||
| /** | ||
| * Read a value from `sessionStorage` | ||
| * @param {string} key | ||
| */ | ||
| export function get(key) { | ||
| try { | ||
| return JSON.parse(sessionStorage[key]); | ||
| } catch { | ||
| // do nothing | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Write a value to `sessionStorage` | ||
| * @param {string} key | ||
| * @param {any} value | ||
| */ | ||
| export function set(key, value) { | ||
| const json = JSON.stringify(value); | ||
| try { | ||
| sessionStorage[key] = json; | ||
| } catch { | ||
| // do nothing | ||
| } | ||
| } |
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
5 changes: 5 additions & 0 deletions
5
packages/kit/test/apps/basics/src/routes/snapshot/+layout.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,5 @@ | ||
| <a href="/snapshot/a">a</a> | ||
| <a href="/snapshot/b">b</a> | ||
| <a href="/snapshot/c" data-sveltekit-reload>c</a> | ||
|
|
||
| <slot /> |
10 changes: 10 additions & 0 deletions
10
packages/kit/test/apps/basics/src/routes/snapshot/a/+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,10 @@ | ||
| <script> | ||
| let message = ''; | ||
|
|
||
| export const snapshot = { | ||
| capture: () => message, | ||
| restore: (snapshot) => (message = snapshot) | ||
| }; | ||
| </script> | ||
|
|
||
| <input bind:value={message} /> |
1 change: 1 addition & 0 deletions
1
packages/kit/test/apps/basics/src/routes/snapshot/b/+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 @@ | ||
| <h1>b</h1> |
1 change: 1 addition & 0 deletions
1
packages/kit/test/apps/basics/src/routes/snapshot/c/+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 @@ | ||
| <h1>c</h1> |
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
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.