Storage API

您正在阅读此内容的英文版本,因为该语系尚未翻译。 帮助我们翻译此文章吧!

Secure context
This feature is available only in secure contexts (HTTPS), in some or all supporting browsers.

Web 存储标准,the Storage Standard,定义了一个通用的、共享的存储系统,供所有 API 和技术使用,以存储各个网站的内容可访问数据。 Storage API 允许网站的代码、Web 应用程序知道它们可以使用、已经使用多少存储空间。空间不足时,用户代理会自动清理站点数据,以便为其他用途腾出空间。而 Storage API 甚至可以控制:在执行清理之前,是否需要提醒代码或 Web 应用程序,以便作出反应。

Note: 此特性在 Web Worker 中可用。

站点存储——由存储标准管理的网站数据——包括以下几种:

站点存储单元

The site storage system described by the Storage Standard and interacted with using the Storage API consists of a single site storage unit for each origin. In essence, every Web site or Web application has its own storage unit into which its data gets placed. The diagram below shows a site storage pool with three storage units within, showing how storage units can have different data types stored within and may have different quotas (maximum storage limits).

A diagram showing how the site storage pool consists of multiple storage units that contain data from various APIs as well as possible unused space left before the quota is reached.

  • 来源 1 有一些 Web Storage 数据和一些 IndexedDB 数据,还有一些空闲空间。目前它的使用量没有达到配额限制。
  • 来源 2 中没有存储任何数据;它只是 it's just an empty box waiting for content. This origin, however, has a lower quota than the other two do. It may be a less-visited site, or one known to have lower data storage requirements.
  • 来源 3 的存储单元已被完全填满;它达到了它的配额限制,并且无法在不删除某些现有数据的情况下存储更多数据。

User agent are likely to use various techniques to determine the quota for various origins. One of the most likely methods—one which the specification specifically encourages, in fact—would be to consider the popularity and/or usage levels of individual sites to determine what their quotas should be.  It's also conceivable that the browser might offer a user interface to customize these quotas.

存储模式

每个站点存储单元中的实际数据存储,被称为它的 / box)。每个站点存储单元只有一个盒,盒中包含其所有数据,且有一个盒存储模式 / 存储模式box mode),用于描述其数据保留策略。目前有两种模式:

"best-effort"
用户代理将会将尽可能久地保留 box 中包含的数据,但若因存储空间不足儿必须清空 box 以释放存储压力时,用户代理不会警告用户
"persistent"
用户代理将会将尽可能久地保留此模式的数据,只有在所有被标记为 "best-effort" 的 box 都已被清理后,才会清理被标记为 "persistent" 的 box。If it becomes necessary to consider clearing persistent boxes, the user agent will notify the user and provide a way to clear one or more persistent boxes as needed.

修改某个来源的 box mode 需要取得使用 "persistent-storage" 特性的权限。

数据持久性与清理

If the site or app has the "persistent-storage" feature permission, it can use the StorageManager.persist() method to request that its box be made persistent. It's also possible for the user agent to decide to make the site's storage unit persistent due to usage characteristics or other metrics. The "persistent-storage" feature's permission-related flags, algorithms, and types are all set to the standard defaults for a permission, except that the permission state must be the same across the entire origin, and that if the permission state isn't "granted" (meaning that for whatever reason, access to the persistent storage feature was denied), the origin's site storage unit's box mode is always "best-effort".

注意:请参考 使用 Permissions API 以了解更多关于申请与管理权限的信息。

When clearing site storage units, an origin's box is treated as a single entity; if the user agent needs to clear it and the user approves, the entire data store is cleared rather than providing some means of clearing only data from individual APIs.

If a box is marked as "persistent", the contents won't be cleared by the user agent without either the data's origin itself or the user specifically doing so. This includes scenarios such as the user selecting a "Clear Caches" or "Clear Recent History" option. The user will be asked specifically for permission to remove persistent site storage units.

配额与使用量的估算

The user agent determines, using whatever mechanism it chooses, the maximum amount of storage a given site can use. This maximum is the origin's quota. The amount of this space which is in use by the site is called its usage. Both of these values are estimates; there are several reasons why they're not precise:

  • User agents are encouraged to obscure the exact size of the data used by a given origin, to prevent these values from being used for fingerprinting purposes.
  • De-duplication, compression, and other methods to reduce the physical size of the stored data may be used.
  • Quotas are conservative estimates of the space available for the origin's use, and should be less than the available space on the device to help prevent overruns.

User agents may use any method they choose to determine the size of origins' quotas, and are encouraged by the specification to provide popular or frequently-used sites with extra space.

To determine the estimated quota and usage values for a given origin, use the navigator.storage.estimate() method, which returns a promise that, when resolved, receives a StorageEstimate that contains these figures. For example:

navigator.storage.estimate().then(estimate => {
  // estimate.quota 是估得的配额
  // estimate.usage 是估得的使用量,单位为 byte 比特
});

规范

规范 状态 备注
Storage Living Standard Initial definition.

浏览器兼容性

StorageManager

Update compatibility data on GitHub
DesktopMobile
ChromeEdgeFirefoxInternet ExplorerOperaSafariAndroid webviewChrome for AndroidFirefox for AndroidOpera for AndroidSafari on iOSSamsung Internet
StorageManagerChrome Full support 48Edge ? Firefox Full support 57
Full support 57
No support 51 — 57
Notes Disabled
Notes See bug 1304966 and bug 1399038.
Disabled From version 51 until version 57 (exclusive): this feature is behind the dom.storageManager.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
IE ? Opera Full support YesSafari ? WebView Android Full support 48Chrome Android Full support 48Firefox Android Full support 51
Notes Disabled
Full support 51
Notes Disabled
Notes See bug 1304966 and bug 1399038.
Disabled From version 51: this feature is behind the dom.storageManager.enabled preference (needs to be set to true). To change preferences in Firefox, visit about:config.
Opera Android Full support YesSafari iOS ? Samsung Internet Android Full support Yes
estimateChrome Full support 52Edge ? Firefox Full support 51IE ? Opera Full support YesSafari ? WebView Android Full support 52Chrome Android Full support 52Firefox Android Full support 51Opera Android Full support YesSafari iOS ? Samsung Internet Android Full support Yes
persistChrome Full support 52
Full support 52
No support 48 — 52
Alternate Name
Alternate Name Uses the non-standard name: requestPersistent
Edge ? Firefox Full support 55IE ? Opera Full support YesSafari ? WebView Android Full support 52
Full support 52
No support 48 — 52
Alternate Name
Alternate Name Uses the non-standard name: requestPersistent
Chrome Android Full support 52
Full support 52
No support 48 — 52
Alternate Name
Alternate Name Uses the non-standard name: requestPersistent
Firefox Android Full support 55Opera Android Full support YesSafari iOS ? Samsung Internet Android Full support Yes
persistedChrome Full support 52
Full support 52
No support 48 — 52
Alternate Name
Alternate Name Uses the non-standard name: persistentPermission
Edge ? Firefox Full support 55IE ? Opera Full support YesSafari ? WebView Android Full support 52
Full support 52
No support 48 — 52
Alternate Name
Alternate Name Uses the non-standard name: persistentPermission
Chrome Android Full support 52
Full support 52
No support 48 — 52
Alternate Name
Alternate Name Uses the non-standard name: persistentPermission
Firefox Android Full support 55Opera Android Full support YesSafari iOS ? Samsung Internet Android Full support Yes

Legend

Full support  
Full support
Compatibility unknown  
Compatibility unknown
See implementation notes.
See implementation notes.
User must explicitly enable this feature.
User must explicitly enable this feature.
Uses a non-standard name.
Uses a non-standard name.

参见