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/extendableevent/index.html | 197 +++++++++++++++++++++ .../web/api/extendableevent/waituntil/index.html | 73 ++++++++ 2 files changed, 270 insertions(+) create mode 100644 files/zh-cn/web/api/extendableevent/index.html create mode 100644 files/zh-cn/web/api/extendableevent/waituntil/index.html (limited to 'files/zh-cn/web/api/extendableevent') diff --git a/files/zh-cn/web/api/extendableevent/index.html b/files/zh-cn/web/api/extendableevent/index.html new file mode 100644 index 0000000000..4f1adf6b7f --- /dev/null +++ b/files/zh-cn/web/api/extendableevent/index.html @@ -0,0 +1,197 @@ +--- +title: ExtendableEvent +slug: Web/API/ExtendableEvent +tags: + - API + - Experimental + - ExtendableEvent + - Interface + - NeedsTranslation + - Offline + - Reference + - Service Workers + - ServiceWorker + - TopicStub + - Workers +translation_of: Web/API/ExtendableEvent +--- +
{{APIRef("Service Workers API")}}{{SeeCompatTable}}
+ +
作为 service worker 生命周期的一部分,ExtendableEvent接口延长了在全局范围上{{event("install")}}和{{event("activate")}}事件的生命周期。这样可以确保在升级数据库架构并删除过时的caches之前,不会调度任何函数事件(如{{domxref("FetchEvent")}})。
+ +
如果在ExtendableEvent处理程序之外调用{{domxref("ExtendableEvent.waitUntil","waitUntil()")}},浏览器应该抛出一个InvalidStateError;还要注意,多个调用将堆积起来,结果promises 将添加到extend lifetime promises.
+ +
 
+ +
+

提示: 上述段落中描述的行为在firefox 43中得到了修复(参见 {{bug(1180274)}} )。

+
+ +

此接口继承自{{domxref("Event")}}接口。

+ +

{{InheritanceDiagram(700, 60, 20)}}

+ +
+

提示: 只有当全局范围是 {{domxref("ServiceWorkerGlobalScope")}} 时,此接口才可用。当它是一个 {{domxref("Window")}} 或其他类型 worker 的作用域时,它不可用。

+
+ +

构造函数

+ +
+
{{domxref("ExtendableEvent.ExtendableEvent()", "ExtendableEvent()")}}
+
创建新的ExtendableEvent对象。
+
+ +

特性

+ +

不实现任何特定属性,但从其父级事件继承属,{{domxref("Event")}}

+ +

方法

+ +

从他的父辈继承, {{domxref("Event")}}.

+ +
+
{{domxref("ExtendableEvent.waitUntil", "ExtendableEvent.waitUntil()")}}
+
+

延长事件的生存期。它将在service worker 的 installactivate 事件中被调用。

+
+
+ +

实例

+ +

代码片段来自service worker prefetch sample (查看 prefetch example live.)。代码在{{domxref("ServiceWorkerGlobalScope.oninstall")}}中调用{{domxref("ExtendableEvent.waitUntil()")}},延迟将{{domxref("ServiceWorkerRegistration.installing")}} Worker视为已安装,直到传递的promise resolve(在所有资源都已被提取和缓存的情况,或者发生任何异常时的问题.)

+ +

代码段还显示了对service worker使用的缓存进行版本控制的最佳实践。虽然在这个例子中只有一个缓存,但是相同的方法可以用于多个缓存。它将缓存的速记标识符映射到特定的、版本化的缓存名称。

+ +
+

提示:在chrome中,日志记录语句通过chrome://service worker internals访问的相关服务工作者的“inspect”接口可见。

+
+ +
var CACHE_VERSION = 1;
+var CURRENT_CACHES = {
+  prefetch: 'prefetch-cache-v' + CACHE_VERSION
+};
+
+self.addEventListener('install', function(event) {
+  var urlsToPrefetch = [
+    './static/pre_fetched.txt',
+    './static/pre_fetched.html',
+    'https://www.chromium.org/_/rsrc/1302286216006/config/customLogo.gif'
+  ];
+
+  console.log('Handling install event. Resources to pre-fetch:', urlsToPrefetch);
+
+  event.waitUntil(
+    caches.open(CURRENT_CACHES['prefetch']).then(function(cache) {
+      cache.addAll(urlsToPrefetch.map(function(urlToPrefetch) {
+        return new Request(urlToPrefetch, {mode: 'no-cors'});
+      })).then(function() {
+        console.log('All resources have been fetched and cached.');
+      });
+    }).catch(function(error) {
+      console.error('Pre-fetching failed:', error);
+    })
+  );
+});
+ +
重点: 在获取资源时,如果有可能资源是由不支持 CORS 的服务器提供的,那么使用 {mode: 'no-cors'} 非常重要。在本例中, www.chromium.org 不支持CORS。
+ +

Specifications

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Service Workers', '#extendable-event', 'ExtendableEvent')}}{{Spec2('Service Workers')}}Initial definition.
+ +

Browser compatibility

+ +
{{CompatibilityTable}}
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureChromeFirefox (Gecko)Internet ExplorerOperaSafari (WebKit)
Basic support{{CompatChrome(40.0)}}{{ CompatGeckoDesktop("44.0") }}[1]{{CompatNo}}24{{CompatNo}}
async waitUntil(){{CompatUnknown}}{{ CompatGeckoDesktop("53.0") }}[2]{{CompatNo}}{{CompatUnknown}}{{CompatNo}}
+
+ +
+ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
FeatureAndroidFirefox Mobile (Gecko)IE MobileOpera MobileSafari MobileChrome for Android
Basic support{{CompatNo}}{{ CompatGeckoMobile("44.0") }}{{CompatNo}}{{CompatUnknown}}{{CompatNo}}{{CompatChrome(40.0)}}
async waitUntil(){{CompatNo}}{{ CompatGeckoMobile("53.0") }}[2]{{CompatNo}}{{CompatUnknown}}{{CompatNo}}{{CompatUnknown}}
+
+ +

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

+ +

[2] {{domxref("ExtendableEvent.waitUntil", "ExtendableEvent.waitUntil()")}} can now be called asynchronously (see {{bug(1263304)}}).

+ +

See also

+ + diff --git a/files/zh-cn/web/api/extendableevent/waituntil/index.html b/files/zh-cn/web/api/extendableevent/waituntil/index.html new file mode 100644 index 0000000000..8e48586b67 --- /dev/null +++ b/files/zh-cn/web/api/extendableevent/waituntil/index.html @@ -0,0 +1,73 @@ +--- +title: ExtendableEvent.waitUntil() +slug: Web/API/ExtendableEvent/waitUntil +translation_of: Web/API/ExtendableEvent/waitUntil +--- +

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

+ +

ExtendableEvent.waitUntil() 方法告诉事件分发器该事件仍在进行。这个方法也可以用于检测进行的任务是否成功。在服务工作线程中,这个方法告诉浏览器事件一直进行,直至 promise 解决,浏览器不应该在事件中的异步操作完成之前终止服务工作线程。

+ +

服务工作线程(service workers)中的 {{domxref("ServiceWorkerGlobalScope/install_event", "install")}} 事件使用 waitUntil() 来将服务工作线程保持在 {{domxref("ServiceWorkerRegistration.installing", "installing")}} 阶段。如果传入 waitUntil() 的 promise 被拒绝,则将此次安装视为失败,丢弃这个服务工作线程。这主要用于确保在服务工作线程安装以前,所有依赖的核心缓存都已经成功载入。

+ +

服务工作线程(service workers)中的 {{domxref("ServiceWorkerGlobalScope/activate_event", "activate")}} 事件使用 waitUntil() 来延迟函数事件,如 fetch 和 push,直至传入 waitUntil() 的 promise 被解决。这让服务工作线程有时间更新数据库架构(database schema)和删除过时缓存({{domxref("Cache", "caches")}}),让其他事件能在一个完成更新的状态下进行。

+ +

 waitUntil() 方法最初必须在事件回调里调用,在此之后,方法可以被调用多次,直至所有传入的 promise 被解决。

+ +
+

注意: 上述段落描述的行为已经在Firefox 43中被修复 (参见 {{bug(1180274)}}.)

+
+ +

语法

+ +
extendableEvent.waitUntil(promise);
+ +

参数

+ +

一个 {{jsxref("Promise")}}.

+ +

示例

+ +

在服务工作线程的 install 事件中使用 waitUntil()

+ +
addEventListener('install', event => {
+  const preCache = async () => {
+    const cache = await caches.open('static-v1');
+    return cache.addAll([
+      '/',
+      '/about/',
+      '/static/styles.css'
+    ]);
+  };
+  event.waitUntil(preCache());
+});
+ +

规范

+ + + + + + + + + + + + + + +
SpecificationStatusComment
{{SpecName('Service Workers', '#dom-extendableevent-waituntil', 'waitUntil()')}}{{Spec2('Service Workers')}}Initial definition
+ +

浏览器兼容性

+ +

{{Compat("api.ExtendableEvent.waitUntil")}}

+ +

另参见

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