Each site storage bucket has a mode that describes the data retention policy for that bucket. There are two modes:
"best-effort"
-
The user agent will try to retain the data contained in the bucket for as long as it can, but will not warn users if storage space runs low and it becomes necessary to clear the bucket in order to relieve the storage pressure.
"persistent"
-
The user agent will retain the data as long as possible, clearing all
"best-effort"
buckets before considering clearing a bucket marked"persistent"
. If it becomes necessary to consider clearing persistent buckets, the user agent will notify the user and provide a way to clear one or more persistent buckets as needed.
You can change an origin's storage bucket mode by using the user permission.
if (navigator.storage && navigator.storage.persist) {
navigator.storage.persist().then((persistent) => {
if (persistent) {
console.log("Storage will not be cleared except by explicit user action");
} else {
console.log("Storage may be cleared by the UA under storage pressure.");
}
});
}
You can also use the navigator.storage.persisted()
method to know whether an origin's storage is persistent or not:
if (navigator.storage && navigator.storage.persist) {
navigator.storage.persisted().then((persistent) => {
if (persistent) {
console.log("Storage will not be cleared except by explicit user action");
} else {
console.log("Storage may be cleared by the UA under storage pressure.");
}
});
}
To learn more, see Does browser-stored data persist?.