-
-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* add failing test * make it possible to read snapshot props * refactor sessionStorage stuff a bit * snapshots * working * beef up test * tidy up * fix type * run all tests * err.... * lint * account for missing layouts etc * thank you, past us, for creating accessors for export const automatically * don't restore if navigation was blocked * update scroll position at same time as snapshot * DRY * types * docs * changeset * add a comment * only capture snapshot when appropriate * Update packages/kit/src/runtime/client/client.js Co-authored-by: Simon H <[email protected]> * document caveat around large snapshots * destroy alternate timelines * fix --------- Co-authored-by: Simon H <[email protected]>
- Loading branch information
1 parent
b108439
commit 63613bf
Showing
14 changed files
with
218 additions
and
40 deletions.
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.