Site cannot be installed: no matching service worker detected

JavascriptJsonWeb ApplicationsProgressive Web-Apps

Javascript Problem Overview


Hey I am trying to program my first pwa and got the following problem:

when I start my web app I get the following error:

> 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

I think my manifest url is right because of this link

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

So I am trying to make a simple web app which I can host with firebase.

Whats the problem? Thanks for your help

Javascript Solutions


Solution 1 - Javascript

Place the service worker script file at the root of your website and set start_url to /, or place it wherever you want and use the scope property and the Service-Worker-Allowed HTTP header as in my other answer.

Solution 2 - Javascript

Just in case this happens to anyone else using 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.

Just thought I'd share this and add it here, as this lost me a few days trying to work it out.

Solution 3 - Javascript

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.

Install serve npm first, if it isn't already: 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. 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.

Solution 4 - Javascript

Try making the following changes:

  • In manifest.json, the start_url and scope is set like this (or based on your preference):

      "start_url": "/",
      "scope": "."  
    
  • The sw.js file is located at project root, taking start_url as reference.

Solution 5 - Javascript

for my work well updating the library "@angular/service-worker":"^7.2.15" to the version, "@angular/service-worker":"^ 8.2.3"

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

Solution 6 - Javascript

one way to solve this problem is by adding a query parameter after your service worker include. so normally you html will look like this.

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

you could modify it to look like this

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

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJohannes GnadlingerView Question on Stackoverflow
Solution 1 - JavascriptAshraf SabryView Answer on Stackoverflow
Solution 2 - JavascriptWesley CoetzeeView Answer on Stackoverflow
Solution 3 - JavascriptandimiyaView Answer on Stackoverflow
Solution 4 - JavascriptHarita RavindranathView Answer on Stackoverflow
Solution 5 - JavascriptRolando MoncadaView Answer on Stackoverflow
Solution 6 - JavascriptDilanTsasiView Answer on Stackoverflow