2014-11-23 37 views
2

我已經開始在Google Chrome v38中使用ServiceWorker。我已將#enable-experimental-web-platform-features設置爲True,並將其設置爲about:config,並將我的本地主機設置爲通過https://服務請求。註冊ServiceWorker返回AbortError代碼:20

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 

任何想法如何解決這個問題呢?

回答

2

與服務人員一起開發時,請使用谷歌瀏覽器40或更高版本(無需撥打chrome://flags)。 Chrome 38可能對某些服務工作者API有一些非常非常初步的支持,但從Chrome 38開始,API的規格和成熟度都發生了顯着變化。Chrome 39的同上。

理想情況下,爲了開發目的,我實際上推薦使用當前的Google Chrome Canary(41),因爲自從Chrome 40分支被刪除以來,出現了一些對錯誤消息的增強,而更準確/表達的錯誤在幫助我們找出問題的原因時有很大的幫助。

+0

我正在使用金絲雀(41)沒有修改標誌,並且無法使其在本地工作(http)。 – Aleksandrenko 2015-01-05 14:07:17

+0

服務人員通常只會在通過「https」服務的頁面上工作。但是,有一種豁免會允許在與'localhost'相對時使用'http'訪問。如果您在本地測試服務人員,請確保您通過「http:// localhost:」URL訪問受控頁面。 – 2015-01-05 14:52:29

+0

它在本地工作,我打破了實施。 :) – Aleksandrenko 2015-01-06 09:04:52

1

我一直在ServiceWorker出現語法錯誤的錯誤。由於Chrome甚至沒有在「網絡」選項卡中記錄服務工作者提取,所以花了一段時間才意識到這是原因。

對於獲取此錯誤的其他人,請在隱身標籤中嘗試使用空的sw.js.

0

另外,服務人員仍在爲語法錯誤和Chrome DevTools支持進行報告。當我寫這篇文章時,Chrome 42處於測試階段。