MDN wants to talk to developers like you: https://qsurvey.mozilla.com/s3/8d22564490d8

ServiceWorkerContainer.register()

这篇翻译不完整。请帮忙从英语翻译这篇文章

这是一个实验中的功能
此功能某些浏览器尚在开发中,请参考浏览器兼容性表格以得到在不同浏览器中适合使用的前缀。由于该功能对应的标准文档可能被重新修订,所以在未来版本的浏览器中该功能的语法和行为可能随之改变。

ServiceWorkerContainer 接口的 register() 方法创建或更新一个给定scriptURL的ServiceWorkerRegistration 

如果成功,一个服务工作者注册将提供的脚本URL与一个范围进行关联,后者用于导航匹配。如果该方法无法返回一个 ServiceWorkerRegistration,则返回一个 Promise

您可以从受控页无条件调用此方法, 即, 您不需要首先检查是否有一个有效的注册。

句法

ServiceWorkerContainer.register(scriptURL, options)
    .then(
        function(ServiceWorkerRegistration) {
            // do something
        }
);

参数

scriptURL
The URL of the service worker script.
options 可选
An options object to provide options upon registration. Currently available options are:
  • scope: A USVString representing a URL that defines a service worker's registration scope; what range of URLs a service worker can control. This is usually a relative URL. The default value is the URL you'd get if you resolved './' using the service worker script's location as the base.

返回

A Promise that resolves to a ServiceWorkerRegistration object.

例子

if ('serviceWorker' in navigator) {
  navigator.serviceWorker.register('service-worker.js', {scope: './'})
  .then(function(registration) {
    document.querySelector('#status').textContent = 'succeeded';
  }).catch(function(error) {
    document.querySelector('#status').textContent = error;
  });
} else {
  // The current browser doesn't support service workers.
  let aElement = document.createElement('a');
  aElement.href = `
     http://www.chromium.org/blink/serviceworker/service-worker-faq
  `;
  aElement.textContent = 'unavailable';
  document.querySelector('#status').appendChild(aElement);
}

Specifications

Specification Status Comment
Service Workers
ServiceWorkerContainer
Working Draft Initial definition.

Browser compatibility

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
Basic support 40.0 44.0 (44.0)[1] 未实现 24 未实现
Feature Android Firefox Mobile (Gecko) Firefox OS IE Mobile Opera Mobile Safari Mobile Chrome for Android
Basic support ? 44.0 (44.0) (Yes) 未实现 ? 未实现 40.0

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

文档标签和贡献者

 此页面的贡献者: xgqfrms-GitHub, 178784676
 最后编辑者: xgqfrms-GitHub,