Summary
The persist() extension currently writes the collection state to localStorage in an unload event listener:
https://github.com/mswjs/data/blob/main/src/extensions/persist.ts
window.addEventListener('unload', () => {
localStorage.setItem(/* ... */)
})
The unload event is deprecated and is being gradually disabled in Chrome:
https://developer.chrome.com/docs/web-platform/deprecating-unload
In Chrome 150, registering this listener produces the following console error:
[Violation] Permissions policy violation: unload is not allowed in this document.
This prevents persist() from reliably saving the latest collection state when a page is closed, navigated away from, or reloaded.
Expected behavior
The latest collection state is stored reliably and restored on the next page load, as described in the persist() documentation.
Actual behavior
Persistence depends on the unload event being dispatched. When Chrome blocks the event listener, recent changes are not written to localStorage, and the next page load may restore stale or empty data.
Steps to reproduce
-
Open an application using the persist() extension in Chrome 150.
-
Create or update a record in the collection.
-
Observe the following error in the browser console:
[Violation] Permissions policy violation: unload is not allowed in this document.
-
Close, reload, or navigate away from the page.
-
Open the page again.
-
Observe that the latest collection state was not persisted.
Environment
Possible directions
Potential alternatives may include persisting changes when the collection is modified, or using lifecycle events such as visibilitychange or pagehide. I am not sure which approach best matches the intended semantics of the extension.
Related issue
#343 also discusses persistence and hydration behavior, but it does not appear to cover the dependency on the deprecated unload event itself.
Summary
The
persist()extension currently writes the collection state tolocalStoragein anunloadevent listener:https://github.com/mswjs/data/blob/main/src/extensions/persist.ts
The
unloadevent is deprecated and is being gradually disabled in Chrome:https://developer.chrome.com/docs/web-platform/deprecating-unload
In Chrome 150, registering this listener produces the following console error:
This prevents
persist()from reliably saving the latest collection state when a page is closed, navigated away from, or reloaded.Expected behavior
The latest collection state is stored reliably and restored on the next page load, as described in the
persist()documentation.Actual behavior
Persistence depends on the
unloadevent being dispatched. When Chrome blocks the event listener, recent changes are not written tolocalStorage, and the next page load may restore stale or empty data.Steps to reproduce
Open an application using the
persist()extension in Chrome 150.Create or update a record in the collection.
Observe the following error in the browser console:
Close, reload, or navigate away from the page.
Open the page again.
Observe that the latest collection state was not persisted.
Environment
@msw/data: 1.1.7Chrome: 150.0.7871.115 (Official Build) (64-bit)
Console error:
Possible directions
Potential alternatives may include persisting changes when the collection is modified, or using lifecycle events such as
visibilitychangeorpagehide. I am not sure which approach best matches the intended semantics of the extension.Related issue
#343 also discusses persistence and hydration behavior, but it does not appear to cover the dependency on the deprecated
unloadevent itself.