Javascript Service Worker 的存储限制是多少?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35242869/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 17:28:17  来源:igfitidea点击:

What is the storage limit for a service worker?

javascriptservice-workerprogressive-web-apps

提问by Nachiketha

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?

大多数浏览器为 localStorage 提供每个域 5MB 的存储限制。服务工作者是否有这样的内存限制/约束?

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).

我知道网络工作者(服务工作者所基于的)没有这样的限制。但是 Web Workers 并不完全用于资产缓存,而是更多地用于处理(因此 CPU 是那里的主要问题)。

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

如果对内存大小没有限制,设计糟糕的网站会导致浏览器崩溃吗?

回答by Nachiketha

Update Jan 15 2018

2018 年 1 月 15 日更新

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

Storage API的StorageManager接口正在成为所有与存储相关的 API 查询的标准。正如@miguel-lattuada所提到的,估计 API将提供对使用 Web 应用程序可用存储的存储的估计。另外,请注意QuotaExceededError异常,它可以帮助我们处理错误场景。

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:

有关更多信息,请参阅以下 2 篇精彩文章:



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

2017 年 3 月 16 日(仅供参考/历史记录)

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

最近我看到了这篇文章:offline-cookbook,其中说明如下:

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

您的原点有一定数量的可用空间来做它想做的事情。该可用空间在所有原始存储之间共享:LocalStorage、IndexedDB、Filesystem,当然还有缓存。

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)

上面的代码可能不适用于所有浏览器。(例如:在 chrome<48 中,可能需要查找 webkitPersistentStorage 等)

Other useful info/resources

其他有用的信息/资源

  1. As per Offline Storage for Progressive Web Appsby 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).

    Firefoxno limits, but will prompt after 50MB data stored

    Mobile Safari50MB max

    Desktop Safariunlimited (prompts after 5MB)

    IE10+maxes at 250MB and prompts at 10MB

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

  1. 根据Addy Osmani 的渐进式 Web 应用程序离线存储

    In Chrome and Opera:您的存储是每个源(而不是每个 API)。两种存储机制都将存储数据,直到达到浏览器配额。应用程序可以通过配额管理 API(如上所述)检查他们使用了多少配额。

    Firefox没有限制,但存储50MB数据后会提示

    Mobile Safari最大 50MB

    Desktop Safari无限制(5MB后提示)

    IE10+最大为 250MB,提示为 10MB

  2. 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.

目前,这些是为我的问题找到的最相关的文章/解决方案。如果有人知道一些更好的文章或规格,请分享。

回答by Xanthir

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.

没有明确的限制。所有现代浏览器都是多进程或类似的,因此设计不当的页面(或 SW)不会比崩溃本​​身更糟糕。

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.)

请注意,SW 规范非常明确地指出浏览器可以随时出于任何原因终止和重新启动 SW。(如果 DevTools 在页面上打开,Chrome 会故意不断地杀死该页面的 SW,以鼓励您采用良好的做法。)

回答by Miguel Lattuada

On latests browser you can use StorageManagerwhich is an implementation of a new standard for browser storage, take a look at thismozilla article.

在最新浏览器上,您可以使用StorageManager,它是浏览器存储新标准的实现,请查看这篇mozilla 文章。

let _storageStats = await navigator.storage.estimate();
console.log(_storageStats);
/*
  Will prompt something like this
  {quota: 15946471833, usage: 682}
  Which is a representation of quota/usage in bytes
  As you can see I get an insane quota of almost 16 GB
*/

回答by Josef Je?ek

More info about navigator.storage.estimate()and navigator.webkitTemporaryStorage.queryUsageAndQuota()is here https://developers.google.com/web/updates/2017/08/estimating-available-storage-space

有关更多信息navigator.storage.estimate(),并navigator.webkitTemporaryStorage.queryUsageAndQuota()在这里 https://developers.google.com/web/updates/2017/08/estimating-available-storage-space

Testing page is here https://thimbleprojects.org/startpolymer/361372/

测试页面在这里https://thimbleprojects.org/startpolymer/361372/

回答by Ryuu

I'm not 100% certain, but I think you're limited entirely by what's available on the client machine. As in, there is no fixed upper limit

我不是 100% 确定,但我认为您完全受客户端计算机上可用内容的限制。如在,没有固定的上限

If someone was running a beast of a machine and the browser was the only active application, then you'd most likely have plenty of storage available

如果有人正在运行机器的野兽并且浏览器是唯一的活动应用程序,那么您很可能有足够的可用存储空间

However, if it was an old limited machine that's barely chugging along; you'd have very little

但是,如果它是一台几乎无法运转的旧式有限机器;你会很少

It depends entirely on what you're trying to do really. You should only really be using service workers to store things vital to your page/application working

这完全取决于你真正想要做什么。您应该只真正使用服务工作者来存储对您的页面/应用程序工作至关重要的内容