Offline Web Page Access with Service Worker

Kevin Kurniawan
Blibli.com Tech Blog
4 min readJun 30, 2022

If you are not familiar with Service Worker, you can check Introduction to the Service Worker first before continue reading this article.

When user want to access our web page, sometimes there is a problem with their connection, they close the tab/browser, and usually forgot to return to our web page after the connection is back to online.

Example of Offline Page Access automatically handled by Google Chrome.

To avoid this, we can handle our web page offline access using Service Worker Caching feature.

Offline Page Handle Types

Keep in mind that all of this type needs user to access the web page with internet connection first to store the assets to browser cache.

Caching specific assets

When we are access the web page for the first time, we can caching the assets that we want in our browser. Next time, when we come back without internet connection, the specific assets will still available.

  1. Define the cache name and assets location.
  2. In Install event, cache all assets that already defined in step 1. Before caching, call self.skipWaiting() to force the waiting service worker to become the active service worker immediately.
  3. In Activate event, remove unwanted caches. Check if there are caches with different cache name that already defined. This step is important for us to always get fresh caches when activate the service worker.
  4. In Fetch event, check every fetch request URL. If the fetch event is failed and the URL matches with the assets that already defined, respond the request with the assets from browser’s cache. We can modify the sequence based on our needs: cache first, network first, or hybrid. This example is using network fist approach.
Caching specific assets code
Console log for caching specific assets.
Page access when offline (caching specific assets).

Caching all assets

We can cache all assets from web page that we already accessed before. In this type, we do not need to defined the assets because all the assets in the web page will be cached.

  1. Define the cache name.
  2. In Install event, call self.skipWaiting() to force the waiting service worker to become the active service worker immediately.
  3. In Activate event, remove unwanted caches same as previous type.
  4. In Fetch event, when fetch event is succeed, cache the response to the browser. The response is stream type so it only can be consumed once. Do not forget to make a copy/clone the response first to cache it. When fetch event is failed, intercept the response with the cache that already saved in browser.
Caching all assets code
Console log for caching all assets.
All assets still available when offline.

Show offline page

If we want to show specific information to user when they access our web page with no internet connection, we can create a specific offline page that will be shown to users when they access our page offline.

  1. Define the cache name and offline page location.
  2. In Install event, call self.skipWaiting() to force the waiting service worker to become the active service worker immediately. Then, cache the offline assets that already defined in step 1.
  3. In Activate event, remove unwanted caches same as previous type.
  4. In Fetch event, if the fetch event is failed and the URL matches with the assets that already defined, check if the request mode is “navigate” to check if it is an HTTP request. If it is an HTTP request, respond the request with the offline assets that already been cached in step 2.
Caching offline page code
Console log for caching offline page.
Show cached offline page when offline.

References

--

--