Service Worker by Patrik

Making Service Worker always load files from server, unless Offline

Based on your use case I would say you can go with "Network first" approach.

self.addEventListener('fetch', function (event) {
  event.respondWith(
    fetch(event.request).catch(function() {
      return caches.match(event.request)
    })
  )
})

In your fetch event handler we are doing following things:-

Use the fetch method to get data from the server.

Add a catch handler in case of network failure and get data from the cache storage.(which will in case of offline state).

Note:- We are assuming you are caching data in your install event of service worker. if you are not then we can do that in fetch event also.

Making Service Worker always load files from server, unless Offline - STACKOOM

Comments

Leave a Comment

All fields are required. Your email address will not be published.