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

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

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

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

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

语法

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

参数

scriptURL
service worker脚本的URL.
options 可选
注册时提供选项的配置对象。 目前可用的选项包括:
  • scope: 一个 USVString,表示定义service worker注册范围的URL ;service worker可以控制的URL范围。通常是相对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);
}

规范

Specification Status Comment
Service Workers
ServiceWorkerContainer
Working Draft Initial definition.

浏览器兼容性

We're converting our compatibility data into a machine-readable JSON format. This compatibility table still uses the old format, because we haven't yet converted the data it contains. Find out how you can help!

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

文档标签和贡献者

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