GoogleChromeLabs / sw-toolbox

[Deprecated] A collection of service worker tools for offlining runtime requests
https://developers.google.com/web/tools/workbox/guides/migrations/migrate-from-sw
Apache License 2.0
3.62k stars 331 forks source link

Why do I get TypeError failed to fetch on every page refresh #290

Open marcofranssen opened 4 years ago

marcofranssen commented 4 years ago

I'm struggling to resolve the following error on my webblog.

Why do I get TypeError failed to fetch on every page refresh. Only the very first time I load the page it loads without errors. Then on every refresh I get errors.

https://marcofranssen.nl

The FetchEvent for "https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js" resulted in a network error response: the promise was rejected.
Promise.then (async)
r @ sw.js?t=1576961691623:17
sw.js?t=1576961691623:1 Uncaught (in promise) TypeError: Failed to fetch
(index):1780 GET https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js net::ERR_FAILED
(index):1812 ServiceWorker Register Successfully.

I have been checking this for a while now, but don't have any clue how to resolve.

I hope someone is able to point me in a solution direction.

My code looks as following:

self.addEventListener('install', function () {
  return self.skipWaiting();
});
self.addEventListener('active', function () {
  return self.clients.claim();
});

var precacheUrls = [
  '/',
  '/about/',
  '/git-recipes/',
  '/signing-docker-images-using-docker-content-trust/',
  '/secure-2fa-ssh-and-pgp-using-krypton/',
  '/howto-secure-shell-easily-from-the-terminal/',
  '/manage-go-tools-via-go-modules/',
  '/install-fresh-raspbian-image-on-your-raspberry-pi-part-2/',
];

toolbox.precache(precacheUrls);
toolbox.options = {"networkTimeoutSeconds":5};

toolbox.router.any(/^https:\/\/(www\.google-analytics\.com|pagead2\.googlesyndication\.com)/, toolbox.networkOnly);
toolbox.router.any(/.*\.(js|css|jpg|jpeg|png|gif)$/, toolbox.cacheFirst);
toolbox.router.any(/content.json$/, toolbox.cacheFirst);
toolbox.router.any(/\//, toolbox.networkFirst);
Lucent commented 3 years ago

Did you figure this out? I have the same issue, but only with Edge Chromium. First request to ads goes through, but further that are handled by a fetch event in a service worker return that error. Is it cross-origin related?

marcofranssen commented 3 years ago

@Lucent This is what I run now. Never tried to get ads working via service worker as I couldn't really figure it out.

self.addEventListener('install', function () { return self.skipWaiting(); });
self.addEventListener('active', function () { return self.clients.claim(); });

toolbox.precache(['/','/about/','/git-recipes/','/hello-next-js-goodbye-hexo/','/remove-files-from-git-history-using-git-filter-repo/','/nginx-1-19-supports-environment-variables-and-templates-in-docker/','/building-a-elasticsearch-cluster-using-docker-compose-and-traefik/','/use-the-acme-dns-challenge-to-get-a-tls-certificate/']);
toolbox.options = {"networkTimeoutSeconds":5};

toolbox.router.any(/www\.google-analytics\.com/, toolbox.networkOnly);
toolbox.router.any(/.*\.(js|css|jpg|jpeg|png|gif)$/, toolbox.cacheFirst);
toolbox.router.any(/content.json$/, toolbox.cacheFirst);
toolbox.router.any(/\//, toolbox.networkFirst);
Lucent commented 3 years ago

Did you have uBlock Origin enabled at the time and your site whitelisted? If so, I identified a problem with the extension on Edge that may have been the cause, for future visitors: https://github.com/uBlockOrigin/uBlock-issues/issues/1478

gorhill commented 3 years ago

@Lucent The change in uBO causing the issue was caused by a relatively recent change, it did not exist back in December 2019.