Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why am I seeing "Error - Only secure origins are allowed" for my service worker?

When I try to add a service worker on my progressive web app page, why does the browser console show the following error?

ERROR "Uncaught (in promise) DOMException: Only secure origins are allowed

JS Code:

(function () {
    'use strict';

    // TODO add service worker code here
    if ('serviceWorker' in navigator) {
        navigator.serviceWorker
            .register('service-worker.js')
            .then(function () {
                console.log('Service Worker Registered');
            });
    }
})();
like image 890
user7348450 Avatar asked Dec 29 '16 04:12

user7348450


4 Answers

From Service Worker FAQ:

Q: I get an error message about "Only secure origins are allowed". Why?

A: Service workers are only available to "secure origins" (HTTPS sites, basically) in line with a policy to prefer secure origins for powerful new features. However http://localhost is also considered a secure origin, so if you can, developing on localhost is an easy way to avoid this error.

You can also use the --unsafely-treat-insecure-origin-as-secure command-line flag. This flag must be combined with a --user-data-dir flag. For example:

$ ./chrome --user-data-dir=/tmp/foo --unsafely-treat-insecure-origin-as-secure=http://your.insecure.site

If you want to test on https://localhost with a self-signed certificate, do:

$ ./chrome --allow-insecure-localhost https://localhost

You might also find the --ignore-certificate-errors flag useful.

like image 184
tony19 Avatar answered Nov 14 '22 22:11

tony19


Try using http://127.0.0.1:8080 for hosting locally instead of http://192.168.29.53:8080

like image 30
Harshal Avatar answered Nov 15 '22 00:11

Harshal


You can check for protocol before registering the service worker like this, location.protocol === 'https:' && serviceWorker.register('service-worker.js')

like image 3
Rajesh Hegde Avatar answered Nov 14 '22 22:11

Rajesh Hegde


in chrome type chrome://flags

search "secure origin"

enter image description here

then click on Enabled, relaunch chrome.

like image 3
CLIFFORD P Y Avatar answered Nov 14 '22 22:11

CLIFFORD P Y