Search code examples
cachingbrowser-cacheservice-workerweb-storage

Will `navigator.storage.persist` grandfather regular cache entries into persistent ones?


If I make a call to navigator.storage.persist, will entries already added to (and not yet expunged from) a regular cache automatically become persistent, or must a new cache be created (e.g., by somehow copying the old non-persistent cache into a newly created (and now, given user permissions, persistent) one)?

I'd like to get my my app started doing regular caching of critical files early on and then requesting persistence and registering a service worker to install these files ideally without making any new requests (unless the user already somehow cleared the cache before the worker installs).


Solution

  • The following excerpts from sections 3 & 3.1 of the specification are relevant:

    ...Each origin has an associated site storage unit. A site storage unit contains a single box.

    A box has mode which is either "best-effort" or "persistent"...

    A box is considered to be an atomic unit. Whenever a box is cleared by the user agent, it must be cleared in its entirety.

    So there's a single conceptual "box" for your origin's storage, and that origin is cleared in an all-or-nothing fashion. That would apply to items that were stored either before or after the "box" transitioned from being "best-effort" to being "persistent".

    That's all to say that you can cache things early, and then request persistence later on.