Persist migrations
persist serializes { state, version } to browser storage. The version is the length of the migration array.
Storage types
{ local: key }useslocalStorage.{ session: key }usessessionStorage.{ cookie: key }usesdocument.cookie.
Migrations
Migration functions run from the stored version index until the latest migration. The result is written back with the new version.
persist({ theme: 'light', count: 0 }, {
local: 'settings',
migrate: [
(old) => ({ theme: String(old), count: 0 }),
(old) => ({ ...old, count: Number(old.count ?? 0) }),
],
});Caveats
- Migrations are only attempted for
localandcookiestorage. sessionstorage does not acceptmigratein the public type.- Values are JSON parsed and stringified; non-JSON state should be adapted before persistence.
- Browser storage failures are caught and logged only when
windowexists.