
Requests to sw.js (404) after publishing a new website
After going live with one of our sites, we notice several requests to /sw.js
:
This can cause unwanted effects, especially if rules are in place to respond to 404s, such as Fail2Ban.
This especially happens if the new website replaces a site that was on a platform like Wix. This is because a Service Worker has been registered in the visitor's browser by the previous platform:
navigator.serviceWorker.register("/sw.js", { scope: "/", });
Solution 1 : Remove the Service Worker in the Browser
To fix the problem, it is possible to unregister a service worker in the browser, when you have control over it.
Source : Stack Overflow
Solution 2 : Request the removal of the Service Worker from the new site
We usually don't have control over the browsers of users who visit our sites.
It is then possible to unregister the file from browsers to stop requests, using a server script: ServiceWorkerRegistration unregister
Solution 3 : Create an empty file
And finally, a somewhat silly solution, but one that solves the problem while avoiding 404 errors, is to create an empty sw.js
file on the web server.