demo/service_worker.js
async function fetchCacheableResource(request) {
  const cache = await caches.open(CACHE_NAME);
  const cachedResponse = await cache.match(request);

  if (!navigator.onLine) {
    // We are offline, and we know it.  Just return the cached response, to
    // avoid a bunch of pointless errors in the JS console that will confuse
    // us developers.  If there is no cached response, this will just be a
    // failed request.
    return cachedResponse;
  }

  if (cachedResponse) {
    // We have it in cache.  Try to fetch a live version and update the cache,
    // but limit how long we will wait for the updated version.
    try {
      return timeout(NETWORK_TIMEOUT, fetchAndCache(cache, request));
    } catch (error) {
      // We tried to fetch a live version, but it either failed or took too
      // long.  If it took too long, the fetch and cache operation will continue
      // in the background.  In both cases, we should go ahead with a cached
      // version.
      return cachedResponse;
    }
  } else {
    // We should have this in cache, but we don't.  Fetch and cache a fresh
    // copy and then return it.
    return fetchAndCache(cache, request);
  }
}
async function fetchCacheableResource(request) {
  const cache = await caches.open(CACHE_NAME);
  const cachedResponse = await cache.match(request);

  if (!navigator.onLine) {
    // We are offline, and we know it.  Just return the cached response, to
    // avoid a bunch of pointless errors in the JS console that will confuse
    // us developers.  If there is no cached response, this will just be a
    // failed request.
    return cachedResponse;
  }

  if (cachedResponse) {
    // We have it in cache.  Try to fetch a live version and update the cache,
    // but limit how long we will wait for the updated version.
    try {
      return timeout(NETWORK_TIMEOUT, fetchAndCache(cache, request));
    } catch (error) {
      // We tried to fetch a live version, but it either failed or took too
      // long.  If it took too long, the fetch and cache operation will continue
      // in the background.  In both cases, we should go ahead with a cached
      // version.
      return cachedResponse;
    }
  } else {
    // We should have this in cache, but we don't.  Fetch and cache a fresh
    // copy and then return it.
    return fetchAndCache(cache, request);
  }
}
async function fetchCacheableResource ( request ) { const cache = await caches . open ( CACHE_NAME ) ; const cachedResponse = await cache . match ( request ) ; if ( ! navigator . onLine ) { // We are offline, and we know it.  Just return the cached response, to // avoid a bunch of pointless errors in the JS console that will confuse // us developers.  If there is no cached response, this will just be a // failed request. return cachedResponse ; } if ( cachedResponse ) { // We have it in cache.  Try to fetch a live version and update the cache, // but limit how long we will wait for the updated version. try { return timeout ( NETWORK_TIMEOUT , fetchAndCache ( cache , request ) ) ; } catch ( error ) { // We tried to fetch a live version, but it either failed or took too // long.  If it took too long, the fetch and cache operation will continue // in the background.  In both cases, we should go ahead with a cached // version. return cachedResponse ; } } else { // We should have this in cache, but we don't.  Fetch and cache a fresh // copy and then return it. return fetchAndCache ( cache , request ) ; } }
Fetch a cacheable resource.  Decide whether to request from the network,
the cache, or both, and return the appropriate version of the resource.

@param {!Request} request
@return {!Promise.<!Response>}
Fetch a cacheable resource . Decide whether to request from the network the cache or both and return the appropriate version of the resource .