-
-
Notifications
You must be signed in to change notification settings - Fork 2k
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
feat: snapshots #8710
Merged
Merged
feat: snapshots #8710
Changes from all 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 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 @@ | ||
--- | ||
'@sveltejs/kit': minor | ||
--- | ||
|
||
feat: add snapshot mechanism for preserving ephemeral DOM state |
This file contains 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,33 @@ | ||
--- | ||
title: Snapshots | ||
--- | ||
|
||
Ephemeral DOM state — like scroll positions on sidebars, the content of `<input>` elements and so on — is discarded when you navigate from one page to another. | ||
|
||
For example, if the user fills out a form but clicks a link before submitting, then hits the browser's back button, the values they filled in will be lost. In cases where it's valuable to preserve that input, you can take a _snapshot_ of DOM state, which can then be restored if the user navigates back. | ||
|
||
To do this, export a `snapshot` object with `capture` and `restore` methods from a `+page.svelte` or `+layout.svelte`: | ||
|
||
```svelte | ||
/// file: +page.svelte | ||
<script> | ||
let comment = ''; | ||
|
||
/** @type {import('./$types').Snapshot<string>} */ | ||
export const snapshot = { | ||
capture: () => comment, | ||
restore: (value) => comment = value | ||
}; | ||
</script> | ||
|
||
<form method="POST"> | ||
<textarea bind:value={comment} /> | ||
<button>Post comment</button> | ||
</form> | ||
``` | ||
|
||
When you navigate away from this page, the `capture` function is called immediately before the page updates, and the returned value is associated with the current entry in the browser's history stack. If you navigate back, the `restore` function is called with the stored value as soon as the page is updated. | ||
|
||
The data must be serializable as JSON so that it can be persisted to `sessionStorage`. This allows the state to be restored when the page is reloaded, or when the user navigates back from a different site. | ||
|
||
> Avoid returning very large objects from `capture` — once captured, objects will be retained in memory for the duration of the session, and in extreme cases may be too large to persist to `sessionStorage`. |
This file contains 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 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 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 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 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 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 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 /> |
11 changes: 11 additions & 0 deletions
11
packages/kit/test/apps/basics/src/routes/snapshot/a/+page.svelte
This file contains 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 @@ | ||
<script> | ||
let message = ''; | ||
|
||
/** @type {import('./$types').Snapshot<string>} */ | ||
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 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 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> |
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.
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.
I see
initial_history_index
andcurrent_history_index
which both seem to benumber
s to be used as an index forsnapshots
.