Skip to content

Better snapshot API #16670

Description

@Rich-Harris

Describe the problem

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.

It has some other limitations:

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

Metadata

Metadata

Assignees

Labels

No labels
No labels

Projects

No projects

Relationships

None yet

Development

No branches or pull requests

Issue actions