79

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?

Dieter Information
  • 1,045
  • 2
  • 8
  • 13
  • Thanks for this. I totally forgot about the req for https also and couldn't figure out why my first site worked (https) and my other wasn't (http). That's a couple hours I'll never get back. – raddevus Feb 12 '21 at 23:34

5 Answers5

142

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

Josh Lee
  • 171,072
  • 38
  • 269
  • 275
  • 4
    Ohw yes you're absolutely right. I tried to access the Service Worker via the local IP. With localhost it works again. – Dieter Information Sep 13 '18 at 08:05
  • 6
    Does anyone know how to allow this on Virtual Hosts on my local dev machine? I have several projects under different .test domains pointing to my local dev machine. So I'm on localhost only Chrome doesn't see it. Or should I use another TLD? – Dante Feb 05 '19 at 08:09
127

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.

Sean256
  • 2,849
  • 4
  • 30
  • 39
4

Another reason might be running a browser in private-mode

meydlo
  • 41
  • 1
2

insteade http://192.168.1.105:8080/ go to http://localhost:8080/ it worked for me

1

Go to your vscode settings in the user>Extension option scroll down to live server config

set your host to localhost --ScreenShot Set host to local host

Now scroll down and uncheck use local IP option -- ScreenShot uncheck localIP

go to your Port json setting and set the port to 8080 --ScreenShot json setting set port to 8080 Go live and check console

Daniya Niazi
  • 180
  • 3
  • 10