问题
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
回答1:
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.
回答2:
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.
来源:https://stackoverflow.com/questions/45534076/site-cannot-be-installed-no-matching-service-worker-detected