3

我已经开始使用ServiceWorkerGoogle Chrome v38。我已经设置#enable-experimental-web-platform-features并设置Trueabout:config我的本地主机以通过https://.

我的sw.js文件包含简单的例子:

// The SW will be shutdown when not in use to save memory,
// be aware that any global state is likely to disappear
console.log("SW startup");

self.addEventListener('install', function(event) {
  console.log("SW installed");
});

self.addEventListener('activate', function(event) {
  console.log("SW activated");
});

self.addEventListener('fetch', function(event) {
  console.log("Caught a fetch!");
  event.respondWith(new Response("Hello world!"));
});

和我的app.js文件:

if ('serviceWorker' in navigator) {
    console.log('registering sw.js');
    navigator.serviceWorker.register('/static/js/sw.js').then(function(reg) {
        console.log('Yey!', reg);
    }, function(err) {
        console.log('Boo!', err);
    });
}

我的控制台输出是:

registering sw.js app.js:27
Boo!
DOMException: The Service Worker installation failed. {message: "The Service Worker installation failed.", name: "AbortError", code: 20, INDEX_SIZE_ERR: 1, DOMSTRING_SIZE_ERR: 2…} app.js:31

任何想法如何解决这个问题?

4

3 回答 3

2

使用 Service Worker 进行开发时,请使用 Google Chrome 40 或更高版本(无需chrome://flags摆弄)。Chrome 38 可能已经对一些 service worker API 提供了一些非常非常初步的支持,但是自 Chrome 38 以来,API 的规范和成熟度都发生了显着变化。Chrome 39 也是如此。

理想情况下,出于开发目的,我实际上建议使用当前的Google Chrome Canary (41),因为自 Chrome 40 分支削减以来对错误消息进行了多项增强,并且更准确/表达性更强的错误在弄清楚时有很大帮助怎么了。

于 2014-11-24T15:12:22.517 回答
1

我一直在为带有语法错误的 ServiceWorker 收到此错误。由于 Chrome 甚至没有在 Network 选项卡中记录 service worker 获取,所以花了一段时间才意识到这是原因。

对于遇到此错误的任何其他人,请尝试使用空 sw.js 的隐身选项卡。

于 2015-03-08T20:01:58.147 回答
0

此外,Service Workers 的语法错误报告和 Chrome DevTools 支持总体上仍在开发中。在我写这篇文章时,Chrome 42 处于测试阶段。

于 2015-03-31T20:06:39.767 回答