Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What is the storage limit for a service worker?

Most of the browsers provide localStorage with the storage limit of 5MB per domain. Are there such memory limits/constraints with respect to service workers?

I know that web workers (on which service workers are based) don't have such limitations. But Web Workers are not exactly used for assets caching, instead they're used more for processing (so CPU is the main concern there).

If there's no limit on the memory size, could a badly designed website crash the browser?

like image 496
Nachiketha Avatar asked Feb 06 '16 15:02

Nachiketha


People also ask

Can service workers access local storage?

Note: localStorage works in a similar way to service worker cache, but it is synchronous, so not allowed in service workers.

How much data can PWA store?

The group limit — this is defined as 20% of the global limit, but it has a minimum of 10 MB and a maximum of 2 GB. Each origin is part of a group (group of origins).

How much data we can store in cache?

The global limit is calculated as 50% of free disk space. In Firefox, an internal browser tool called the Quota Manager keeps track of how much disk space each origin is using up, and deletes data if necessary. So if the free space on your hard drive is 500 GB, then the total storage for a browser is 250 GB.

What is the IndexedDB size limit?

The only limits on the size of the IndexedDB database will be the user's disk space and operating system. The localStorage quota is 5000KB, and there is no way for a web site to ask the browser for permission to store more than that amount in localStorage.


2 Answers

Update Jan 15 2018

The StorageManager interface of Storage API is becoming a standard for all storage related api queries. As mentioned by @miguel-lattuada, the estimate API would provide an estimate of the storage used a web app the available storage. Also, note the QuotaExceededError exception which would help us in handling error scenarios.

eg code:

if ('storage' in navigator && 'estimate' in navigator.storage) {    navigator.storage.estimate().then(({usage, quota}) => {      console.log(`Using ${usage} out of ${quota} bytes.`);    }).catch(error => {      console.error('Loading storage estimate failed:');      console.log(error.stack);    });  } else {    console.error('navigator.storage.estimate API unavailable.');  }

For more info, refer the following 2 great articles:

  • MDN Web docs - Storage API
  • Google Developers: Estimating Available Storage Space

March 16 2017 (keeping it just for reference/history)

Recently I came across this article: offline-cookbook which states as below:

Your origin is given a certain amount of free space to do what it wants with. That free space is shared between all origin storage: LocalStorage, IndexedDB, Filesystem, and of course Caches.

The amount you get isn't spec'd, it will differ depending on device and storage conditions. You can find out how much you've got via:

navigator.storageQuota.queryInfo("temporary").then(function(info) {    console.log(info.quota);    // Result: <quota in bytes>    console.log(info.usage);    // Result: <used data in bytes> }); 

The above code might not work in all the browsers. (for eg: in chrome<48 one might have to look for webkitPersistentStorage etc)

Other useful info/resources

  1. As per Offline Storage for Progressive Web Apps by Addy Osmani

    In Chrome and Opera: Your storage is per origin (rather than per API). Both storage mechanisms will store data until the browser quota is reached. Apps can check how much quota they’re using with the Quota Management API (as described above).

    Firefox no limits, but will prompt after 50MB data stored

    Mobile Safari 50MB max

    Desktop Safari unlimited (prompts after 5MB)

    IE10+ maxes at 250MB and prompts at 10MB

  2. A more detailed guide on Working with quota on mobile browsers by Eiji Kitamura.

For now these are the most relevant articles/solutions found for my problem. If anyone knows some better article or specifications please share.

like image 137
Nachiketha Avatar answered Oct 19 '22 02:10

Nachiketha


There's no explicit limit. All modern browsers are multi-process or similar, so a badly-designed page (or SW) won't do anything worse than crash itself.

Note that the SW spec is very explicit about the browser being able to kill and restart the SW at any time, for any reason. (If DevTools is open on a page, Chrome purposely kills SWs for the page constantly, to encourage you to adopt good practices.)

like image 41
Xanthir Avatar answered Oct 19 '22 04:10

Xanthir