javascript 无法在导航器中找到 serviceWorker
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/52299246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Can't find serviceWorker in navigator anymore
提问by Dieter Information
since the new update of Google Chrome (version 69.0.3497.92 (official build) (64-bit)) I can't find the serviceWorker service in the Navigator anymore. Actually I could register my Service Worker as follows but now I get an error that serviceWorker cannot be found in the navigator:
自从 Google Chrome 的新更新(版本 69.0.3497.92(官方版本)(64 位))以来,我在导航器中再也找不到 serviceWorker 服务了。实际上,我可以按如下方式注册我的 Service Worker,但现在我收到一个错误,在导航器中找不到 serviceWorker:
if('serviceWorker' in navigator) {
/*
*
* Register the Service Worker
*
* */
navigator.serviceWorker.register('sw.js').then(function(registration) {
console.log('Service Worker Registered');
});
} else console.log('Your browser does not support the Service-Worker!');
How can I now use the Service Worker again, or how can I get it to run again for all Chrome versions?
我现在如何再次使用 Service Worker,或者如何让它为所有 Chrome 版本再次运行?
回答by Josh Lee
Serve your page over HTTPS or use localhost. Service workers requirea Secure Context.
通过 HTTPS 提供您的页面或使用localhost. Service Worker需要一个Secure Context。
The value of window.isSecureContextindicates whether [SecureContext]features are visible or hidden. (This is trueon a file://URL and the serviceWorker API will be visible, but it won't work, of course.)
的值window.isSecureContext指示[SecureContext]要素是可见还是隐藏。(这是true在一个file://URL 上,serviceWorker API 将是可见的,但它当然不起作用。)
回答by Sean256
If you are using a virtual host locally for a domain other than localhost, you can tell chrome to treat your custom domain as "secure".
如果您在本地为 localhost 以外的域使用虚拟主机,您可以告诉 chrome 将您的自定义域视为“安全”。
In the addressbar: chrome://flags/#unsafely-treat-insecure-origin-as-secure
在地址栏中: chrome://flags/#unsafely-treat-insecure-origin-as-secure
Then add your domain like http://my-local-dev-domain.test
然后添加您的域,例如 http://my-local-dev-domain.test
Click enable and click the restart button.
单击启用并单击重新启动按钮。

