You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
I alluded to this in #16346 but it deserves its own issue.
The snapshot API is neat but cumbersome. The constraint that you have to export it from a +layout.svelte or +page.svelte component exists for a good reason (it makes it easy to associate a snapshot with a particular history entry, even across page reloads) but it's restrictive and involves magic types.
if you want to clear out state after a lateral navigation, you have to pair it with an afterNavigate — would be nice if this was part of the snapshot API directly
Describe the proposed solution
A new snapshot helper exported from $app/navigation with the following characteristics:
you can use it anywhere. by default it will generate an id (used for restoration) from a stack trace, but you can also provide one if you want to a) preserve state across new deployments that change the stack trace or b) use a snapshot via a shared helper, or in a component that can be rendered multiple times
uses devalue for serialization, respecting your custom transport hook
fires on shallow navigations
adds a reset method that does the job of afterNavigate, i.e. it is fired when creating a new history entry (whereas restore is fired when traversing)
I'm tagging this with the 3.0 milestone so that we can deprecate export const snapshot
Alternatives considered
No response
Importance
nice to have
Additional Information
No response
Reacted by Nic Polumeyv, Shivan Sivakumaran, Jona van der Vlist, Hyunbin Seo and gterras
Activity
feat: add `snapshot` helper to `$app/navigation` (#16685)