Course outline

Svelte / Intermediate: Kit routes and server data policy / Lesson 14

History snapshots and draft lifetime

Restore small ephemeral drafts across history navigation while keeping persistence, privacy and submission separate.

About 3 min readContent reviewed
On this page

Lesson goals

  • Capture and restore a small history snapshot.
  • Explain snapshot scope and storage.
  • Test navigation, reset and privacy.

Choose ephemeral state

A draft can disappear when a user navigates away and returns. A SvelteKit snapshot associates selected component state with a browser history entry so it can be restored on traversal. Decide whether that behavior matches the product: it is a convenience for small ephemeral state, not a durable server save, an authorization decision or a guarantee that a form was submitted.

Capture and restore a draft

Call the documented snapshot helper during component initialization. The example captures a short note, restores it on a matching history entry and resets it when the helper reports a navigation without captured state. The explicit ID identifies this example; instances need distinct IDs when their snapshots must remain independent. This fragment requires a compatible SvelteKit version that supplies the helper.

svelte SvelteKit page fragment
<script>
  import { snapshot } from "$app/navigation";
  let note = $state("");
  snapshot({
    id: "workshop-note",
    capture: () => note,
    restore: (value) => { note = value; },
    reset: () => { note = ""; }
  });
</script>
<label for="note">Draft note</label>
<textarea id="note" bind:value={note}></textarea>
Expected output
Returning to a captured history entry restores its draft note.

The helper associates the note with history and restores or resets it through the provided callbacks.

Render the current draft

A separate preview can receive the current value as a prop. It has no independent writable copy and therefore cannot silently diverge from the editor. This fragment illustrates the display contract; a parent must import it and pass note for it to render. Keep submitted or saved state distinct from the local draft and label both clearly.

svelte Svelte component fragment
<script>
  let { note } = $props();
</script>
<section aria-label="Draft preview">
  <h2>Preview</h2>
  <p>{note || "No draft yet."}</p>
</section>
Expected output
An empty note displays No draft yet.

The preview displays the supplied note or a clear empty message.

Understand privacy and limits

Snapshot data is serialized and persisted in session storage for restoration. Capture only small values appropriate for that browser session; do not treat it as a secret store or put credentials and sensitive drafts there without a deliberate security decision. Storage may be unavailable or cleared, so important work needs a separate durable saving strategy and a clear user-facing state.

Verify history behavior

Enter a synthetic note, navigate away and use Back to check restoration. Navigate to a new history entry and verify the intended reset, then test reload where the configured snapshot ID is stable. Confirm that no server write occurred merely from capture. Test a fresh browser session and storage failure separately rather than promising restoration in every environment.

Common errors and fixes

A draft is not restored on return.

Why it happens
The snapshot was not registered during initialization or the history entry differs.
How to fix it
Register the helper in the owning component and trace the history path.
Prevent it next time
Test navigation away and back with a synthetic draft.

Two instances restore each other’s draft.

Why it happens
Their snapshot IDs collide.
How to fix it
Give independently owned snapshots distinct stable IDs.
Prevent it next time
Test multiple instances and history entries.

A user assumes capture saved a submission.

Why it happens
Ephemeral history state was presented as durable storage.
How to fix it
Show a separate saved or submitted state from the server.
Prevent it next time
Document the lifetime and test a fresh session.

2 practice exercises

1. Does capturing a note submit it to the server?

Show worked solution

No. Snapshot capture preserves selected client history state. A durable save needs its own authenticated operation and confirmation.

2. Why avoid putting credentials in a snapshot?

Show worked solution

Snapshot data is persisted in browser session storage and can be unavailable or exposed to scripts in the application context. Use the application’s reviewed credential handling instead.

Key takeaways

  • Snapshots restore selected history state.
  • Use unique IDs and capture small appropriate values.
  • Keep durable submission and authorization separate.

Official references