问题
I set up a service worker for a static web application that needs to work on mobile phones. I'm using react/webpack2 for this application.
Service Worker installs and works greatly when I open the application via desktop, but when I try to visit the application through mobile, it does not install. When SW finishes installing, it gives you an alert message ("onInstalled");
https://abo-deg.surge.sh (here is an example)
https://abo-deg.surge.sh/survey/background
https://github.com/strongharris/sample (sw.js located inside src, main entrypoint: src/index.js, webpack.config)
The alert message shows up via desktop, but not on mobile browsers. Am I missing something? Is there a different way to set up a service worker for mobile web applications? Any resources, or tips, or guesses would be greatly appreciated.
回答1:
I think the answer is pretty simple. Your server does not automatically transfer you to https://abo-deg.surge.sh
when you type abo-deg.surge.sh
on your mobile.
I was able to run it on Chrome Mobile by typing full URL with https://
Service worker API is available only for websites running over HTTPS, because
Having modified network requests wide open to man in the middle attacks would be really bad https://developer.mozilla.org/en-US/docs/Web/API/Service_Worker_API
Please keep in mind, that your code will work only on these browsers, so don't expect it to work on iOS
来源:https://stackoverflow.com/questions/45024738/service-worker-not-working-for-mobile-web-chrome-firefox-ie