Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript service worker: Fetch resource from cache, but also update it

I'm using a service worker on chrome to cache network responses. What I intend to do when a client requests a resource:

Check cache - If it exists, return from cache, but also send a request to server and update cache if file differs from the cached version. If cache does not have it, send a request for it to the server and then cache the response.

Here's my current code for doing the same:

self.addEventListener('fetch', function (event) {
    var requestURL = new URL(event.request.url);
    var freshResource = fetch(event.request).then(function (response) {
        if (response.ok && requestURL.origin === location.origin) {
            // All good? Update the cache with the network response
            caches.open(CACHE_NAME).then(function (cache) {
                cache.put(event.request, response);
            });
        }
        // Return the clone as the response would be consumed while caching it
        return response.clone();
    });
    var cachedResource = caches.open(CACHE_NAME).then(function (cache) {
        return cache.match(event.request);
    });
    event.respondWith(cachedResource.catch(function () {
        return freshResource;
    }));
});

This code does not work as it throws an error:

The FetchEvent for url resulted in a network error response: an object that was not a Response was passed to respondWith().

Can anyone point me in the right direction?

like image 889
Awol Avatar asked Sep 07 '16 07:09

Awol


People also ask

Can service workers access cache?

# Access to a JavaScript-driven caching API An indispensable aspect of service worker technology is the Cache interface, which is a caching mechanism wholly separate from the HTTP cache. The Cache interface can be accessed within the service worker scope and within the scope of the main thread.

Can I use fetch in service worker?

Using fetch in Service WorkersThe main event that you use in a Service Worker is the fetch event. The fetch event runs every time the browser attempts to access content within the scope of the Service Worker. self. addEventListener( "fetch", event => { console.

What is use of serviceWorker?

Service workers essentially act as proxy servers that sit between web applications, the browser, and the network (when available).


1 Answers

Okay, I fiddled with the code after people pointed out suggestions (thank you for that) and found a solution.

self.addEventListener('fetch', function (event) {
    var requestURL = new URL(event.request.url);
    var freshResource = fetch(event.request).then(function (response) {
        var clonedResponse = response.clone();
        // Don't update the cache with error pages!
        if (response.ok) {
            // All good? Update the cache with the network response
            caches.open(CACHE_NAME).then(function (cache) {
                cache.put(event.request, clonedResponse);
            });
        }
        return response;
    });
    var cachedResource = caches.open(CACHE_NAME).then(function (cache) {
        return cache.match(event.request).then(function(response) {
            return response || freshResource;
        });
    }).catch(function (e) {
        return freshResource;
    });
    event.respondWith(cachedResource);
});

The entire problem originated in the case where the item is not present in cache and cache.match returned an error. All I needed to do was fetch actual network response in that case (Notice return response || freshResource)

This answer was the Aha! moment for me (although the implementation is different): Use ServiceWorker cache only when offline

like image 50
Awol Avatar answered Sep 28 '22 08:09

Awol