Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using the Cache API to cache Web pages without Service Workers

I'm using the Cache API and I'm exploring how much I can do with it without having to use Service Workers.

I have two main use cases:

  1. Loading cached assets when the user is offline
  2. Reloading the page when the user is offline

For the first use case, I can cache images using the following code:

if('caches' in window) {
  caches.open('my-cache').then(function(cache_obj) { 
    cache_obj.addAll(['/', '/img/first.png', '/img/second.png'])
      .then(function() { 
        console.log('Cached!');
      });
  });
}

When I take the user offline, then I load the image into the DOM programmatically like so:

$('body').append('<img src="/img/first.png">');

The image appears on the page as expected. If I take out first.png from the list of cached images, and then repeat this, the image does not appear [because it is not cached]. So the Cache API works quite nicely for the first use case.

However, I'm wondering if I can do the same thing for the second use case. Can I cache a full HTML page offline, and then have it reload when the user is offline, but without having to setup a service worker?

Note: I have a way I can do something very similar using localStorage/sessionStorage to cache the HTML content of the page and then intelligently load it into the current page when the user is offline [using offline.js detection], but I'm looking for a simpler approach using the Cache API.

like image 873
ObiHill Avatar asked Jun 11 '17 18:06

ObiHill


People also ask

Can we do caching in web API?

Caching is very common to make applications performant and scalable. If a result is already computed by the application, it is cached in a store so that next time when the same request comes, cached result can be fetched instead of processing the request again.

How does API cache work?

The Cache API is a system for storing and retrieving network requests and their corresponding responses. These might be regular requests and responses created in the course of running your application, or they could be created solely for the purpose of storing data for later use.

Should service worker be cached?

Service worker caching strategies and use cases #It's preferable to serve the fresh content. However if the network fails or is unstable, it's acceptable to serve slightly old content. It's okay to serve cached content right away, but updated cached content should be used in the future.


1 Answers

This should work for you.

You can also read the property navigator.onLine [true/false]

When the connection is offline, the class is added to the body

The Service Worker is not necessary for this task

// We capture the events online - offline
// You can also: if (navigator.onLIne) {... }

window.addEventListener('online', function () {
  $('body').toggleClass('offline').append('<img src="/img/first.png">');
});
window.addEventListener('offline', function () {
  $('body').toggleClass('offline').append('<img src="/img/second.png">');
});
like image 131
Alfredo González Portero Avatar answered Nov 14 '22 23:11

Alfredo González Portero