javascript - 为什么我的 Service Worker 会看到 "Error - Only secure origins are allowed"?

标签 javascript jquery progressive-web-apps

当我尝试在渐进式 Web 应用程序页面上添加 Service Worker 时,为什么浏览器控制台显示以下错误?

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

JS 代码:

(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');
            });
    }
})();

最佳答案

来自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.

关于javascript - 为什么我的 Service Worker 会看到 "Error - Only secure origins are allowed"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41373166/

相关文章:

javascript - 每次点击的总和

javascript - 这个 HTML 片段中的 onclick 函数在哪里定义?

jquery - CSS 和/或 jQuery 将可点击的小图像添加到多选列表

javascript - jQuery.keypad 性能问题

Angular 通用 + pwa 错误 { 错误 : Uncaught (in promise): Error: Cannot match any routes. URL 段: 'ngsw_worker.es6.js.map'

javascript - Service Worker 创建问题 : ERR_CONNECTION_REFUSED

javascript - 用nodejs在文件上写http请求

javascript - 如何防止在我的 Chrome 扩展程序中放大弹出窗口

javascript - 如果另一个数组中有数据,如何将数据推送到数组中

javascript - 检查渐进式网络应用程序中的网络更改?