From 33058f2b292b3a581333bdfb21b8f671898c5060 Mon Sep 17 00:00:00 2001 From: Peter Bengtsson Date: Tue, 8 Dec 2020 14:40:17 -0500 Subject: initial commit --- files/zh-cn/web/api/cachestorage/delete/index.html | 125 +++++++++++++++++++++ files/zh-cn/web/api/cachestorage/has/index.html | 125 +++++++++++++++++++++ files/zh-cn/web/api/cachestorage/index.html | 115 +++++++++++++++++++ files/zh-cn/web/api/cachestorage/keys/index.html | 122 ++++++++++++++++++++ files/zh-cn/web/api/cachestorage/match/index.html | 91 +++++++++++++++ files/zh-cn/web/api/cachestorage/open/index.html | 85 ++++++++++++++ 6 files changed, 663 insertions(+) create mode 100644 files/zh-cn/web/api/cachestorage/delete/index.html create mode 100644 files/zh-cn/web/api/cachestorage/has/index.html create mode 100644 files/zh-cn/web/api/cachestorage/index.html create mode 100644 files/zh-cn/web/api/cachestorage/keys/index.html create mode 100644 files/zh-cn/web/api/cachestorage/match/index.html create mode 100644 files/zh-cn/web/api/cachestorage/open/index.html (limited to 'files/zh-cn/web/api/cachestorage') diff --git a/files/zh-cn/web/api/cachestorage/delete/index.html b/files/zh-cn/web/api/cachestorage/delete/index.html new file mode 100644 index 0000000000..d41dd26026 --- /dev/null +++ b/files/zh-cn/web/api/cachestorage/delete/index.html @@ -0,0 +1,125 @@ +--- +title: CacheStorage.delete() +slug: Web/API/CacheStorage/delete +translation_of: Web/API/CacheStorage/delete +--- +

{{APIRef("Service Workers API")}}{{SeeCompatTable}}

+ +

{{domxref("CacheStorage")}} 接口的 delete() 方法查找匹配 cacheName 的 {{domxref("Cache")}} 对象,如果找到,则删除 {{domxref("Cache")}} 对象并返回一个resolve为true的 {{jsxref("Promise")}} . 如果未找到 {{domxref("Cache")}} 对象,则返回 false.

+ +

语法

+ +
caches.delete(cacheName).then(function(true) {
+  //your cache is now deleted
+});
+
+ +

Returns

+ +

如果找到 {{domxref("Cache")}} 对象,删除它,返回一个resolve为 true 的 {{jsxref("Promise")}} ,否则,返回 false .

+ +

Parameters

+ +
+
cacheName
+
想要删除的 cache 对象的名称。
+
+ +

实例

+ +

在此代码片段中,我们等待一个activate事件,然后运行一个 {{domxref("ExtendableEvent.waitUntil","waitUntil()")}} 块,其在一个新的 service worker 被激活前清除所有旧的、未使用的cache. 这里我们有一个白名单,其中包含我们想要保留的cache的name. 我们使用 {{domxref("CacheStorage.keys")}} 返回 {{domxref("CacheStorage")}} 对象中cache的键,然后检查每个键值,以查看它是否在白名单中。如果没有,我们使用 delete() 删除它。

+ +
this.addEventListener('activate', function(event) {
+  var cacheWhitelist = ['v2'];
+
+  event.waitUntil(
+    caches.keys().then(function(keyList) {
+      return Promise.all(keyList.map(function(key) {
+        if (cacheWhitelist.indexOf(key) === -1) {
+          return caches.delete(key);
+        }
+      }));
+    })
+  );
+});
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Service Workers', '#cache-storage', 'CacheStorage')}}{{Spec2('Service Workers')}}Initial definition.
+ +

浏览器兼容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatChrome(40.0)}}{{CompatGeckoDesktop(44)}}[1]{{CompatNo}}{{CompatUnknown}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidAndroid WebviewFirefox Mobile (Gecko)IE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}}{{CompatNo}}{{CompatGeckoMobile(44)}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatChrome(40.0)}}
+
+ +

[1] Service workers (and Push) have been disabled in the Firefox 45 & 52 Extended Support Releases (ESR.)

+ +

See also

+ + diff --git a/files/zh-cn/web/api/cachestorage/has/index.html b/files/zh-cn/web/api/cachestorage/has/index.html new file mode 100644 index 0000000000..b9e985a673 --- /dev/null +++ b/files/zh-cn/web/api/cachestorage/has/index.html @@ -0,0 +1,125 @@ +--- +title: CacheStorage.has() +slug: Web/API/CacheStorage/has +translation_of: Web/API/CacheStorage/has +--- +

{{APIRef("Service Workers API")}}{{SeeCompatTable}}

+ +

{{domxref("CacheStorage")}} 对象的 has()方法返回一个 {{jsxref("Promise")}} 对象,当 {{domxref("Cache")}} 对象有 cacheName  时被处理为  true

+ +

语法

+ +
caches.has(cacheName).then(function(boolean) {
+  // true: 缓存存在
+});
+
+ +

返回值

+ +

返回一个 {{jsxref("Promise")}} 对象,缓存存在时resolve的布尔值为 true 否则为 false 。

+ +

参数

+ +
+
cacheName
+
一个表示你正在 {{domxref("CacheStorage")}} 中查找的 {{domxref("Cache")}} 对象name的 {{domxref("DOMString")}}.
+
+ +

例子

+ +

在下面的例子中首先检测是否有名为 v1 的缓存存在, 如果存在我们会向其添加内容,,如果不存在我们会做些对应的初始化动作。

+ +
caches.has('v1').then(function(hasCache) {
+  if (!hasCache) {
+    someCacheSetupfunction();
+  } else {
+    caches.open('v1').then(function(cache) {
+      return cache.addAll(myAssets);
+    });
+  }
+}).catch(function() {
+  // 处理异常
+});
+ +

规范

+ + + + + + + + + + + + + + +
规范状态附加信息
{{SpecName('Service Workers', '#cache-storage', 'CacheStorage')}}{{Spec2('Service Workers')}}初始定义
+ +

浏览器兼容性

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatChrome(40.0)}}{{CompatVersionUnknown}}{{CompatGeckoDesktop(44)}}[1]{{CompatNo}}{{CompatUnknown}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}}{{CompatNo}}{{CompatGeckoMobile(44)}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatVersionUnknown}}{{CompatChrome(40.0)}}
+
+ +

[1] Service workers (and Push) 在 Firefox 45 & 52 Extended Support Releases (ESR.)被默认禁用。

+ +

参考

+ + diff --git a/files/zh-cn/web/api/cachestorage/index.html b/files/zh-cn/web/api/cachestorage/index.html new file mode 100644 index 0000000000..4c86445492 --- /dev/null +++ b/files/zh-cn/web/api/cachestorage/index.html @@ -0,0 +1,115 @@ +--- +title: CacheStorage +slug: Web/API/CacheStorage +tags: + - API + - CacheStorage + - ServiceWorker +translation_of: Web/API/CacheStorage +--- +

{{APIRef("Service Workers API")}}{{SeeCompatTable}}

+ +

CacheStorage 接口表示 {{domxref("Cache")}} 对象的存储。它提供了一个 {{domxref("ServiceWorker")}} 、其它类型worker或者 {{domxref("window")}} 范围内可以访问到的所有命名cache的主目录(它并不是一定要和service workers一起使用,即使它是在service workers规范中定义的),并维护一份字符串名称到相应 {{domxref("Cache")}} 对象的映射。

+ +

CacheStorage  同样暴露了 {{domxref("CacheStorage.open()")}} 和 {{domxref("CacheStorage.match()")}}方法。使用 {{domxref("CacheStorage.open()")}} 获取 {{domxref("Cache")}} 实例。使用 {{domxref("CacheStorage.match()")}} 检查给定的 {{domxref("Request")}} 是否是 CacheStorage 对象跟踪的任何 {{domxref("Cache")}} 对象中的键。

+ +

你可以通过 {{domxref("WorkerGlobalScope.caches", "caches")}} 属性访问 CacheStorage .

+ +
注意: CacheStorage 总是对不受信任的源(即那些不使用HTTPS,尽管此定义将来很可能变得更加复杂。)使用 SecurityError reject. 测试时,你可以在Firefox Devtools选项/齿轮菜单中通过选中"通过HTTP启用 Service Workers (当工具箱打开时)" 选项来绕开这个限制。
+ +
注意: {{domxref("CacheStorage.match()")}} 是一个便捷方法。匹配cache条目的同等功能可以通过执行 {{domxref("CacheStorage.open()")}} 打开cache,使用 {{domxref("CacheStorage.keys()")}} 返回它包含的条目,并将你所需的条目与 {{domxref("CacheStorage.match()")}} 匹配。
+ +

方法

+ +
+
{{domxref("CacheStorage.match()")}}
+
检查给定的 {{domxref("Request")}} 是否是 {{domxref("CacheStorage")}} 对象跟踪的任何 {{domxref("Cache")}} 对象的键,并返回一个resolve为该匹配的 {{jsxref("Promise")}} .
+
{{domxref("CacheStorage.has()")}}
+
如果存在与 cacheName 匹配的 {{domxref("Cache")}} 对象,则返回一个resolve为true的 {{jsxref("Promise")}} .
+
{{domxref("CacheStorage.open()")}}
+
返回一个 {{jsxref("Promise")}} ,resolve为匹配  cacheName (如果不存在则创建一个新的cache)的 {{domxref("Cache")}} 对象
+
{{domxref("CacheStorage.delete()")}}
+
查找匹配 cacheName 的 {{domxref("Cache")}} 对象,如果找到,则删除 {{domxref("Cache")}} 对象并返回一个resolve为true的 {{jsxref("Promise")}} 。如果没有找到 {{domxref("Cache")}} 对象,则返回 false.
+
{{domxref("CacheStorage.keys()")}}
+
返回一个 {{jsxref("Promise")}} ,它将使用一个包含与 {{domxref("CacheStorage")}} 追踪的所有命名 {{domxref("Cache")}} 对象对应字符串的数组来resolve. 使用该方法迭代所有 {{domxref("Cache")}} 对象的列表。
+
+ +

示例

+ +

此代码片段来自于MDN sw-test example (请参阅sw-test running live.)此 service worker 脚本等待 {{domxref("InstallEvent")}} 触发,然后运行 {{domxref("ExtendableEvent.waitUntil","waitUntil")}} 来处理应用程序的安装过程。这包括调用 {{domxref("CacheStorage.open")}} 创建一个新的cache,然后使用 {{domxref("Cache.addAll")}} 向其添加一系列资源。

+ +

在第二个代码块,我们等待 {{domxref("FetchEvent")}} 触发。我们构建自定义相应,像这样:

+ +
    +
  1. 检查CacheStorage中是否找到了匹配请求的内容。如果是,使用匹配内容。
  2. +
  3. 如果没有,从网络获取请求,然后同样打开第一个代码块中创建的cache,并使用 {{domxref("Cache.put")}} (cache.put(event.request, response.clone()).) 将请求的clone副本添加到它。
  4. +
  5. 如果此操作失败(例如,因为网络关闭),返回备用相应。
  6. +
+ +

最后,使用 {{domxref("FetchEvent.respondWith")}} 返回自定义响应最终等于的内容。

+ +
this.addEventListener('install', function(event) {
+  event.waitUntil(
+    caches.open('v1').then(function(cache) {
+      return cache.addAll([
+        '/sw-test/',
+        '/sw-test/index.html',
+        '/sw-test/style.css',
+        '/sw-test/app.js',
+        '/sw-test/image-list.js',
+        '/sw-test/star-wars-logo.jpg',
+        '/sw-test/gallery/bountyHunters.jpg',
+        '/sw-test/gallery/myLittleVader.jpg',
+        '/sw-test/gallery/snowTroopers.jpg'
+      ]);
+    })
+  );
+});
+
+this.addEventListener('fetch', function(event) {
+  var response;
+  event.respondWith(caches.match(event.request).catch(function() {
+    return fetch(event.request);
+  }).then(function(r) {
+    response = r;
+    caches.open('v1').then(function(cache) {
+      cache.put(event.request, response);
+    });
+    return response.clone();
+  }).catch(function() {
+    return caches.match('/sw-test/gallery/myLittleVader.jpg');
+  }));
+});
+ +

说明

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Service Workers', '#cache-storage', 'CacheStorage')}}{{Spec2('Service Workers')}}Initial definition.
+ +

浏览器兼容

+ + + +

{{Compat("api.CacheStorage")}}

+ +

参见

+ + diff --git a/files/zh-cn/web/api/cachestorage/keys/index.html b/files/zh-cn/web/api/cachestorage/keys/index.html new file mode 100644 index 0000000000..8d174f9c2c --- /dev/null +++ b/files/zh-cn/web/api/cachestorage/keys/index.html @@ -0,0 +1,122 @@ +--- +title: CacheStorage.keys() +slug: Web/API/CacheStorage/keys +translation_of: Web/API/CacheStorage/keys +--- +

{{APIRef("Service Workers API")}}{{SeeCompatTable}}

+ +

{{domxref("CacheStorage")}} 接口的 keys() 方法返回一个 {{jsxref("Promise")}}对象,它使用一个数组resolve,该数组包含 {{domxref("CacheStorage")}} 对象按创建顺序跟踪的所有命名 {{domxref("Cache")}}  对象对应的字符串。使用此方法迭代所有 {{domxref("Cache")}} 对象。

+ +

语法

+ +
caches.keys().then(function(keyList) {
+  //对keyList做操作
+});
+
+ +

返回

+ +

一个使用 {{domxref("CacheStorage")}} 对象中 {{domxref("Cache")}} 名称数组resolve的 {{jsxref("Promise")}} 

+ +

参数

+ +

无。

+ +

示例

+ +

在此代码片段中,我们监听{{domxref("ServiceWorkerGlobalScope.onactivate", "activate")}} 事件,然后运行一个 {{domxref("ExtendableEvent.waitUntil","waitUntil()")}} 方法,该方法在新的 service worker 被激活之前清除老的、无用的cache。 这里我们设置一个包含缓存名称的白名单。 通过使用 keys()方法 来返回{{domxref("CacheStorage")}} 对象中的keys集合,然后检查缓存key是否在白名单中,如果不存在,则使用 {{domxref("CacheStorage.delete")}} 方法来删除该缓存。

+ +
then.addEventListener('activate', function(event) {
+  var cacheWhitelist = ['v2'];
+
+  event.waitUntil(
+    caches.keys().then(function(keyList) {
+      return Promise.all(keyList.map(function(key) {
+        if (cacheWhitelist.indexOf(key) === -1) {
+          return caches.delete(key);
+        }
+      });
+    })
+  );
+});
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Service Workers', '#cache-storage', 'CacheStorage')}}{{Spec2('Service Workers')}}Initial definition.
+ +

浏览器兼容

+ +

{{CompatibilityTable}}

+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureChromeEdgeFirefox (Gecko)Internet ExplorerOperaSafari
Basic support{{CompatChrome(40)}}{{CompatVersionUnknown}}{{CompatGeckoDesktop(44)}}[1]{{CompatNo}}{{CompatOpera(27)}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + +
FeatureAndroid WebviewChrome for AndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari Mobile
Basic support{{CompatChrome(40)}}{{CompatChrome(40)}}{{CompatGeckoMobile(44)}}{{CompatVersionUnknown}}{{CompatOperaMobile(27)}}{{CompatVersionUnknown}}
+
+ +

[1] Service workers (and Push) have been disabled in the Firefox 45 & 52 Extended Support Releases (ESR.)

+ +

亦可参考

+ + diff --git a/files/zh-cn/web/api/cachestorage/match/index.html b/files/zh-cn/web/api/cachestorage/match/index.html new file mode 100644 index 0000000000..c978402bf0 --- /dev/null +++ b/files/zh-cn/web/api/cachestorage/match/index.html @@ -0,0 +1,91 @@ +--- +title: CacheStorage.match() +slug: Web/API/CacheStorage/match +translation_of: Web/API/CacheStorage/match +--- +

{{APIRef("Service Workers API")}}{{SeeCompatTable}}

+ +

{{domxref("CacheStorage")}} 接口(可适用于全局性caches) 的 match() 方法检查给定的{{domxref("Request")}} 对象或url字符串是否是一个已存储的 {{domxref("Response")}} 对象的key. 这个方法针对 {{domxref("Response")}} 返回一个 {{jsxref("Promise")}} ,如果没有匹配则返回 undefined

+ +

cache对象按创建顺序查询。

+ +
提示: {{domxref("CacheStorage.match()", "caches.match()")}} 是一个便捷方法。其作用等同于在每个缓存上调用 {{domxref("cache.match()")}} 方法 (按照{{domxref("CacheStorage.keys()", "caches.keys()")}}返回的顺序) 直到返回{{domxref("Response")}} 对象。
+ +

语法

+ +
caches.match(request, options).then(function(response) {
+  // Do something with the response
+});
+
+ +

参数

+ +
+
request
+
想要匹配的 {{domxref("Request")}}。这个参数可以是 {{domxref("Request")}} 对象或 URL 字符串。
+
options {{optional_inline}}
+
这个对象中的属性控制在匹配操作中如何进行匹配选择。可选择参数如下: +
    +
  • ignoreSearch: {{domxref("Boolean")}}值, 指定匹配过程是否应该忽略url中查询参数。举个例子,如果该参数设置为true, 那么 ?value=bar 作为 http://foo.com/?value=bar 中的查询参数将会在匹配过程中被忽略。该参数默认 false
  • +
  • ignoreMethod:  {{domxref("Boolean")}} 值,当被设置为 true 时,将会阻止在匹配操作中对 {{domxref("Request")}}请求的 http 方式的验证 (通常只允许 GETHEAD 两种请求方式)。该参数默认为 false.
  • +
  • ignoreVary:  {{domxref("Boolean")}} 值,当该字段被设置为 true, 告知在匹配操作中忽略对VARY头信息的匹配。换句话说,当请求 URL 匹配上,你将获取匹配的 {{domxref("Response")}} 对象,无论请求的 VARY  头存在或者没有。该参数默认为 false.
  • +
  • cacheName:  {{domxref("DOMString")}} 值, 表示所要搜索的缓存名称。
  • +
+
+
+ +

返回值

+ +

返回resolve为匹配 {{domxref("Response")}} 的 {{jsxref("Promise")}} 对象。如果没有与指定request相匹配response,promise将使用 undefined resolve.

+ +

例子

+ +

此示例来自于MDN sw-test example (请参阅 sw-test running live)。这里,等待 {{domxref("FetchEvent")}} 事件触发。我们构建自定义响应,像这样:

+ +
    +
  1. 使用 {{domxref("CacheStorage.match","CacheStorage.match()")}} 检查 {{domxref("CacheStorage")}} 中是否存在匹配请求,如果存在,则使用它。
  2. +
  3. 如果没有,使用  open() 打开 v1 cache,使用 {{domxref("Cache.put","Cache.put()")}}  将默认网络请求放入 cache 中,并只用 return response.clone() 返回默认网络请求的克隆副本。最后一个是必须的,因为 put() 使用响应主体。
  4. +
  5. 如果此操作失败(例如,因为网络已关闭),则返回备用响应。
  6. +
+ +
caches.match(event.request).then(function(response) {
+  return response || fetch(event.request).then(function(r) {
+    caches.open('v1').then(function(cache) {
+      cache.put(event.request, r);
+    });
+    return r.clone();
+  });
+}).catch(function() {
+  return caches.match('/sw-test/gallery/myLittleVader.jpg');
+});
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Service Workers', '#cache-storage', 'CacheStorage')}}{{Spec2('Service Workers')}}Initial definition.
+ +

浏览器兼容性

+ + + +

{{Compat("api.CacheStorage.match")}}

+ +

亦可参考

+ + diff --git a/files/zh-cn/web/api/cachestorage/open/index.html b/files/zh-cn/web/api/cachestorage/open/index.html new file mode 100644 index 0000000000..cd2b4f46b0 --- /dev/null +++ b/files/zh-cn/web/api/cachestorage/open/index.html @@ -0,0 +1,85 @@ +--- +title: CacheStorage.open() +slug: Web/API/CacheStorage/open +translation_of: Web/API/CacheStorage/open +--- +

{{APIRef("Service Workers API")}}{{SeeCompatTable}}

+ +

{{domxref("CacheStorage")}} 接口的 open() 方法返回一个resolve为匹配 cacheName 的 {{domxref("Cache")}} 对象的 {{jsxref("Promise")}} .

+ +
+

注意: 如果指定的 {{domxref("Cache")}} 不存在,则使用该 cacheName 创建一个新的cache,并返回一个resolve为该新 {{domxref("Cache")}} 对象的{{jsxref("Promise")}}.

+
+ +

语法

+ +
// "caches" is a global read-only variable, which is an instance of CacheStorage,
+// For more info, refer to: https://developer.mozilla.org/en-US/docs/Web/API/WindowOrWorkerGlobalScope/caches
+
+caches.open(cacheName).then(function(cache) {
+  // Do something with your cache
+});
+
+ +

参数

+ +
+
cacheName
+
要打开的cache对象name.
+
+ +

返回值

+ +

一个resolve为请求的 {{domxref("Cache")}} 对象的 {{jsxref("Promise")}} .

+ +

示例

+ +

此示例来自于MDN sw-test example (请参阅 sw-test running live)。这里,等待 {{domxref("FetchEvent")}} 事件触发。我们构建自定义响应,像这样:

+ +
    +
  1. 使用 {{domxref("CacheStorage.match","CacheStorage.match()")}} 检查 {{domxref("CacheStorage")}} 中是否存在匹配请求,如果存在,则使用它。
  2. +
  3. 如果没有,使用  {{domxref("CacheStorage.open","CacheStorage.open()")}} 打开 v1 cache,使用 {{domxref("Cache.put","Cache.put()")}}  将默认网络请求放入 cache 中,并使用 return response.clone() 返回默认网络请求的克隆副本。最后一个是必须的,因为 put() 使用响应主体。
  4. +
  5. 如果此操作失败(例如,因为网络已关闭),则返回备用响应。
  6. +
+ +
var cachedResponse = caches.match(event.request).catch(function() {
+  return fetch(event.request);
+}).then(function(response) {
+  caches.open('v1').then(function(cache) {
+    cache.put(event.request, response);
+  });
+  return response.clone();
+}).catch(function() {
+  return caches.match('/sw-test/gallery/myLittleVader.jpg');
+});
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Service Workers', '#cache-storage', 'CacheStorage')}}{{Spec2('Service Workers')}}Initial definition.
+ +

浏览器兼容性

+ + + +

{{Compat("api.CacheStorage.open")}}

+ +

See also

+ + -- cgit v1.2.3-54-g00ecf