【问题标题】:PWA - service-worker JS in subdirectoryPWA - 子目录中的 service-worker JS
【发布时间】:2019-10-01 13:10:04
【问题描述】:

我试图将我的 sw.js 文件放在子目录而不是根目录中。但随后我收到一条错误消息,提示无法安装 service-worker。

真的有必要把这个文件放在根目录下还是我只是遗漏了什么?

JS-代码:

if ('serviceWorker' in navigator) {

    navigator.serviceWorker.register('/js/pwa/sw.js', { scope: '/js/pwa' }).then(function(reg) {
        console.log('Successfully registered. Scope is ' + reg.scope);
    }).catch(function(error) {
        console.log('Registering failed ' + error);
    });
} else {
    console.log('Service worker can not be registered on this device');
}

【问题讨论】:

    标签: service-worker progressive-web-apps


    【解决方案1】:

    如果我做同样的事情,我得到以下错误

    Registering failed SecurityError: Failed to register a ServiceWorker: The path of the provided scope ('/js/pwa') is not under the max scope allowed ('/js/pwa/'). Adjust the scope, move the Service Worker script, or use the Service-Worker-Allowed HTTP header to allow the scope.
    

    以下部分看起来很奇怪

    { scope: '/js/pwa' }
    

    请按以下方式进行

    { scope: '/js/pwa/' }
    

    【讨论】:

    • 谢谢,看来这是罪魁祸首。现在感觉有点愚蠢......但这会过去的;)
    • 感谢您的回复!✨ 很高兴解决它!
    猜你喜欢
    • 2018-09-29
    • 2019-01-08
    • 2020-11-20
    • 1970-01-01
    • 1970-01-01
    • 2022-08-07
    • 2019-06-14
    • 2020-02-05
    • 2022-01-10
    相关资源
    最近更新 更多