Search code examples
laravelservice-workersw-precachesw-toolbox

Service worker Failed to load resource: net::ERR_UNSAFE_REDIRECT


would you please tell me what i did wrong with my service worker installation, i have those errors appear in chrome console (see the image-1).

The script resource is behind a redirect, which is disallowed.                          service-worker.js:1

Failed to load resource: net::ERR_UNSAFE_REDIRECT          https://harampress.com/post/45/service-worker.js

i use Laravel framework in backend if that could help

just to know that my service-worker.js is in the root of the website, works fine on home page no errors on chrome console, but the moment i try to navigate to a post for example, those two errors appears (see image-2).

knowing that i use sw-precache and sw-toolbox, i generate the service worker useing sw-precache with gulp.

image-1

image-2


Solution

  • see the solutions here on github discussion

    the message from the discussion in github that actually answered to my problem :


    What @wanderview means is that when you do

    navigator.serviceWorker.register('/sw.js')
    

    The actual requesting URL is the sw.js located in the root

    but when you do

    navigator.serviceWorker.register('sw.js')
    

    This is actually a Relative URL so what you actually request might be /post/blah/sw.js and your server might treat it as 404 and do some redirect.