In my php application I have a file called main.js and in it has a service worker implementation to register a service worker in a web app. This is the sample snippet from the main.js file needed to register a service worker
(function () {
//If serviceWorker supports, then register it.
if ("serviceWorker" in navigator) {
navigator.serviceWorker.register('http://localhost:88/weburl/serviceWorker.js', { scope: "./" }) //setting scope of sw
.then(function(registration) {
console.info('Service worker is registered!');
checkForPageUpdate(registration); // To check if new content is updated or not
})
.catch(function(error) {
console.error('Service worker failed ', error);
});
}
This is the path to my project http://localhost:88/weburl.com/serviceWorker.js
when I launch the above project it keeps telling
A bad HTTP response code (404) was received when fetching the script. serviceworker.js
And on inspecting the element, the base url to the project added in script as shown is never loaded as sampled
navigator.serviceWorker.register('http://localhost:88/weburl/serviceWorker.js', { scope: "./" }) //setting scope of sw
instead I see this that results in error 404 not found
navigator.serviceWorker.register('./serviceWorker.js', { scope: "./" }) //setting scope of sw
please what could be wrong
Can you just try without local host with correct path, I have just given an example below
navigator.serviceWorker.register('/weburl/serviceWorker.js', { scope: "./" })
Or look at this link from google - https://developers.google.com/web/fundamentals/getting-started/primers/service-workers
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.