Guest User

Untitled

a guest
Dec 18th, 2018
92
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.86 KB | None | 0 0
  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. });
Add Comment
Please, Sign In to add comment