diff --git a/.changeset/little-countries-cough.md b/.changeset/little-countries-cough.md new file mode 100644 index 000000000000..7649ca44df4e --- /dev/null +++ b/.changeset/little-countries-cough.md @@ -0,0 +1,5 @@ +--- +'@sveltejs/kit': minor +--- + +feat: add snapshot mechanism for preserving ephemeral DOM state diff --git a/documentation/docs/30-advanced/65-snapshots.md b/documentation/docs/30-advanced/65-snapshots.md new file mode 100644 index 000000000000..41971b98e941 --- /dev/null +++ b/documentation/docs/30-advanced/65-snapshots.md @@ -0,0 +1,33 @@ +--- +title: Snapshots +--- + +Ephemeral DOM state — like scroll positions on sidebars, the content of `` 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 + + +
+