daily pastebin goal
8%
SHARE
TWEET

Untitled

a guest Dec 18th, 2018 55 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const CACHE_NAME = 'static';
  2.  
  3. self.addEventListener('install', event => {
  4.     async function onInstall() {
  5.         const cache = await caches.open(CACHE_NAME);
  6.         await cache.addAll([
  7.             'images/Dog.jpg'
  8.         ]);
  9.         return cache;
  10.     }
  11.  
  12.     event.waitUntil(onInstall());
  13. });
  14.  
  15. self.addEventListener('fetch', event => {
  16.     const request = event.request;
  17.     const url = new URL(request.url);
  18.  
  19.     if (url.pathname.endsWith('Cat.jpg')) {
  20.         event.respondWith(async function () {
  21.             const cache = await caches.open('static');
  22.             url.pathname = 'images/Dog.jpg'; // Dogs are better than cats
  23.             event.request.URL = url;
  24.             const cachedResponse = await cache.match(url);
  25.             if (cachedResponse) {
  26.                 return cachedResponse;
  27.             }
  28.             return fetch(event.request);
  29.         }());
  30.     }
  31. });
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
 
Top