1
    2
    3
    4
    5
    6
    7
    8
    9
   10
   11
   12
   13
   14
   15
   16
   17
   18
   19
   20
   21
   22
   23
   24
   25
   26
   27
   28

content / test / data / service_worker / fetch_event_response_via_cache.js [blame]

// Copyright 2016 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

this.onfetch = function(event) {
    var CACHE_NAME = 'cache_name';
    var cache = undefined;
    var url = event.request.url;
    event.respondWith(
        caches.open(CACHE_NAME)
          .then(function(c) {
              cache = c;
              return cache.match(url);
            })
          .then(function(response) {
              if (response)
                return response;
              var cloned_response = undefined;
              return fetch(url)
                .then(function(res) {
                    cloned_response = res.clone();
                    return cache.put(url, res);
                  })
                .then(function() {
                    return cloned_response;
                  });
            }));
  };