简体   繁体   English

无法安装站点:未检测到匹配的服务工作者

[英]Site cannot be installed: no matching service worker detected

Hey I am trying to program my first pwa and got the following problem:嘿,我正在尝试编写我的第一个 pwa 并遇到以下问题:

when I start my web app I get the following error:当我启动我的 web 应用程序时,出现以下错误:

Site cannot be installed: no matching service worker detected.无法安装站点:未检测到匹配的服务工作者。 You may need to reload the page, or check that the service worker for the current page also controls the start URL from the manifest您可能需要重新加载页面,或者检查当前页面的 service worker 是否也控制清单中的 start URL

I think my manifest url is right because of this link我认为我的清单 url 是正确的,因为这个链接

manifest.json清单.json

"start_url": ".",
"display": "standalone",
"orientation": "portrait",
"theme_color": "#29BDBB",
"background_color": "#29BDBB"

and I register my sw like this:我这样注册我的软件:

if ('serviceWorker' in navigator) {
navigator.serviceWorker.register('./sw.js').then(function(reg) {
    console.log('Successfully registered service worker', reg);
}).catch(function(err) {
    console.warn('Error whilst registering service worker', err);
});
}

I got my sw from here我从这里得到了我的 sw

So I am trying to make a simple web app which I can host with firebase .所以我正在尝试制作一个简单的 web 应用程序,我可以用firebase托管它。

Whats the problem?有什么问题? Thanks for your help谢谢你的帮助

将 Service Worker 脚本文件放在您网站的根目录并将start_url设置为/ ,或者将其放在您想要的任何位置并使用scope属性和Service-Worker-Allowed HTTP 标头,如我的其他答案

Just in case this happens to anyone else using Angular8+.以防万一其他使用 Angular8+ 的人发生这种情况。 You may need to change your registration strategy to:您可能需要将注册策略更改为:

ServiceWorkerModule.register('ngsw-worker.js', { enabled: environment.production, registrationStrategy: 'registerImmediately' }),

By default, this is set to registerWhenStable , which is when there are no pending tasks, things like setTimeout() , etc. By changing it to registerImmediately , it will register your service worker as soon as it possibly can.默认情况下,它被设置为registerWhenStable ,当没有挂起的任务时, like setTimeout()等。通过将其更改为registerImmediately ,它会尽快注册您的服务工作者。

Just thought I'd share this and add it here, as this lost me a few days trying to work it out.只是想我会分享这个并在这里添加它,因为这让我失去了几天的时间来解决它。

The create react app service worker only goes into effect in production, so build it and serve it locally to test that the service worker is working properly. create react app service worker 只在生产中生效,所以构建它并在本地提供它以测试 service worker 是否正常工作。

Install serve npm first, if it isn't already: npm i serve -g如果还没有,请先安装 serve npm: npm i serve -g

Then...那么...

npm run build

serve -s build

It's also likely that once the service worker registers successully, but something like beforeinstallprompt might not get called.也有可能一旦服务工作者成功注册,但可能不会调用beforeinstallprompt东西。 If that's the case, publish the app using a host like firebase, which will serve the site over https , then try the beforeinstallprompt there.如果是这种情况,请使用beforeinstallprompt类的主机发布应用程序,该主机将通过https为站点提供服务,然后在那里尝试beforeinstallprompt

Try making the following changes:尝试进行以下更改:

  • In manifest.json , the start_url and scope is set like this (or based on your preference):manifest.json中, start_urlscope设置如下(或根据您的喜好):

     "start_url": "/", "scope": "."
  • The sw.js file is located at project root, taking start_url as reference. sw.js文件位于项目根目录,以 start_url 为参考。

for my work well updating the library "@angular/service-worker":"^7.2.15" to the version, "@angular/service-worker":"^ 8.2.3"为了我的工作,将库“@angular/service-worker”:“^7.2.15”更新到版本,“@angular/service-worker”:“^ 8.2.3”

also placing: ServiceWorkerModule.register ('ngsw-worker.js', {enabled: environment.production, registrationStrategy: 'registerImmediately'}),还放置: ServiceWorkerModule.register ('ngsw-worker.js', {enabled: environment.production, registrationStrategy: 'registerImmediately'}),

one way to solve this problem is by adding a query parameter after your service worker include.解决此问题的一种方法是在 service worker 包含后添加查询参数。 so normally you html will look like this.所以通常你 html 看起来像这样。

<script src="https://example.com"></script> 

you could modify it to look like this你可以修改它看起来像这样

<script src="https://example.com?v=1.1"></script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM