When a browser runs low on storage space, I understand that it will start purging the caches of sites (PWAs) starting with the least-recently visited. Per, Using the Cache API in the service worker:
You are responsible for implementing how your script (service worker) handles updates to the cache. All updates to items in the cache must be explicitly requested; items will not expire and must be deleted. However, if the amount of cached data exceeds the browser's storage limit, the browser will begin evicting all data associated with an origin, one origin at a time, until the storage amount goes under the limit again. See Browser storage limits and eviction criteria for more information.
The linked eviction criteria includes a section on LRU policy:
When the available disk space is filled up, the quota manager will start clearing out data based on an LRU policy — the least recently used origin will be deleted first, then the next one, until the browser is no longer over the limit.
We track the "last access time" for each origin using temporary storage. Once the global limit for temporary storage is reached (more on the limit later), we try to find all currently unused origins (i.e., ones with no tabs/apps open that are keeping open datastores). These are then sorted according to "last access time." The least recently used origins are then deleted until there's enough space to fulfill the request that triggered this origin eviction.
Now the question: do browsers use whether a site has been added to the homescreen (A2H) as a signal to defer clearing the cache storage for a given site? To me it would seem logical for a browser to prioritize purging the caches of least-visited non-A2H site caches before starting to purge the caches of sites that a user has explicitly given the special A2H designation.
Question originally asked in the context of the PWA feature plugin for WordPress.
I'm not aware of "add to home screen" being used as a signal when determining whether or not to clear an origin's storage when a device is running out of space. I wouldn't rely on it one way or another.
Instead, there's a web platform feature known as "Persistent Storage" that you can use to explicitly request that your origin's storage not be purged due to space constraints. That's something you could rely on with greater certainty. From that article:
Beginning with Chrome 55, Chrome will automatically grant the persistence permission if any of the following are true:
- The site is bookmarked (and the user has 5 or less bookmarks)
- The site has high site engagement
- The site has been added to home screen
- The site has push notifications enabled
The permission is automatically denied in all other cases. The goal is to ensure that users can rely on their favorite web apps and not find they have suddenly been cleared.
You'd use it like:
if (navigator.storage && navigator.storage.persist) {
navigator.storage.persist().then((granted) => {
// Optionally update your UI based on the granted state.
});
}