0

I'm currently working on integrating a Service-Worker in my application. But I have problems when I want to test it on my local machine.

The caching in the install-Event is failing. I searched for the error for two days but I don't have ideas anymore. When I deploy the site on e.g. netlify everything works fine.

That's what the network tab is giving me:

network tab

Developer console is printing: developer console

Browser: Chromium Version 70.0.3538.77 (Official Build)

I activated #allow-insecure-localhost in the chrome flags. Webpack is serving over https://localhost:8081 (with a self-signed certificate). If I right click the failed network requests and click Open in new tab everything works fine. The weird thing is that the failing files are changing every time.

Code in my index.jsx (entry point for webpack):

...
if ('serviceWorker' in navigator) {
  window.addEventListener('load', function() {
    navigator.serviceWorker
    .register('/serviceWorker.js', { scope: "/" })
    .then(e => {
      console.log('Service Worker Registered');
      console.log(e);
    })
    .catch(e => {
      console.log("Service Worker fail");
      console.log(e);
    })
  });
}
...

Code in my serviceWorker.js

let cacheName = 'test';
let cachedURLs = [
    "/",
    "/bundle.js",
    "/manifest.webmanifest",
    "/favicon.ico",
    "/favicon-256.png"
];

self.addEventListener('install', function(e) {
  console.log('[ServiceWorker] Install');
  e.waitUntil(
      caches.open(cacheName).then(function(cache) {
        console.log('[ServiceWorker] Caching app shell');
        return cache.addAll(cachedURLs);
      })
  );
});

self.addEventListener('fetch', function(event) {    
  event.respondWith(
      caches.match(event.request).then(function(response) {
        return response || fetch(event.request);
      })
  );    
});

Edit: As mentioned in the comments I added a catch-block to the cache.addAll:

return cache.addAll(cachedURLs).catch(err => console.log('error: ', err));

But unfortunately it isn't more verbose:

error:  TypeError: Failed to fetch

For completeness this is what Chromium is reporting for a failed request (in this case it is the bundle.js)

detail of failed request

Dirk
  • 23
  • 5
  • Hi, could you add a `catch` block to your promise in the `fetch` `event-listener`: `.catch(err => console.log('error: ', err)));` Maybe it'll be more expressive – t3__rry Nov 22 '18 at 08:26
  • I had similar setup, but not having the window.addEventListener('load', ... in index.jsx. Also take care all cachedURLs should exist. – Niels Steenbeek Nov 22 '18 at 08:27
  • @t3__rry Yep, but this doesn't change anything, because the `fetch`-listener is never fired when initial rendering the page. The error is thrown in the `install`-listener (when caching the files with `cache.addAll`). @Niels: Every file/URL exists, as I already pointed out in my post. I can load them if I open the failed requests. – Dirk Nov 22 '18 at 08:36
  • @Dirk ok, did you try adding a `catch` block to the `install` listener then? – t3__rry Nov 22 '18 at 08:52
  • @t3__rry Yep, I tried it, but it doesn't give more insights. I updated my post and added a screenshot of a failed requests. Does this help? – Dirk Nov 22 '18 at 09:50
  • @Dirk not really, I thought you'd have more insights... I've came across this thread if it can help you: https://stackoverflow.com/questions/47160929/progressive-web-app-uncaught-in-promise-typeerror-failed-to-fetch – t3__rry Nov 22 '18 at 10:56
  • @t3__rry I already found this thread, but thank you anyway. But I tried it without the `fetch`-listener. Same result. I tried it in incognito mode (without Chromium Addons) -> same result. And the files are spelled correctly. My guess was, that the error lays within the `webpack-dev-server`. But it doesn't seem so, I activated `verbose`mode of `express`. The failed requests doesn't even reach express. So my guess is that Chromium is failing... – Dirk Nov 22 '18 at 12:00
  • @Dirk yes that's weird... and your implementation is right so I guess it's on Chromium's end as well – t3__rry Nov 22 '18 at 13:08

0 Answers0