I'm trying to integrate service workers into my app, but I've found the service worker tries to retrieve cached content even when online, but I want it to prefer the network in these situations. How can I do this? Below is the code I have now, but I don't believe it is working. SW Install code is omitted for brevity.
var CACHE_NAME = 'my-cache-v1'; var urlsToCache = [ /* my cached file list */ ]; self.addEventListener('install', function(event) { // Perform install steps event.waitUntil( caches.open(CACHE_NAME) .then(function(cache) { console.log('Opened cache'); return cache.addAll(urlsToCache); }) ); }); /* request is being made */ self.addEventListener('fetch', function(event) { event.respondWith( //first try to run the request normally fetch(event.request).catch(function() { //catch errors by attempting to match in cache return caches.match(event.request).then(function(response) { // Cache hit - return response if (response) { return response; } }); }) ); });
This seems to lead to warnings like The FetchEvent for "[url]" resulted in a network error response: an object that was not a Response was passed to respondWith().
I'm new to service workers, so apologies for any mistaken terminology or bad practices, would welcome any tips. Thank you!
Our app is caching its resources on install and serving them with fetch from the cache, so it works even if the user is offline.
Service workers are a fundamental part of a PWA. They enable fast loading (regardless of the network), offline access, push notifications, and other capabilities. Users expect apps to start on slow or flaky network connections, or even when offline.
Using a Service worker you can easily set an app up to use cached assets first, thus providing a default experience even when offline, before then getting more data from the network (commonly known as Offline First).
In my experience service workers aren't mandatory to deliver a PWA with offline functionality. As long as your PWA pages are properly marked as cacheable you can run your PWA completely offline and it won't need a connection to your server if the lifetime of the page is in the browsers cache lifetime.
Without testing this out, my guess is that you're not resolving respondWith()
correctly in the case where there is no cache match. According to MDN, the code passed to respondWith()
is supposed to "resolve by returning a Response or network error to Fetch." So why not try just doing this:
self.addEventListener('fetch', function(event) { event.respondWith( fetch(event.request).catch(function() { return caches.match(event.request); }) ); });
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With