问题
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:
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?
回答1:
Serve your page over HTTPS or use localhost
. Service workers require a Secure Context.
(MDN page, Chromium page).
The value of window.isSecureContext
indicates whether [SecureContext]
features are visible or hidden. (This is true
on a file://
URL and the serviceWorker API will be visible, but it won't work, of course.)
回答2:
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".
In the addressbar: chrome://flags/#unsafely-treat-insecure-origin-as-secure
Then add your domain like http://my-local-dev-domain.test
Click enable and click the restart button.
来源:https://stackoverflow.com/questions/52299246/cant-find-serviceworker-in-navigator-anymore