jvanmelckebeke

sw.js

Jul 20th, 2025
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. const CACHE_NAME = 'cat-button-pwa-v1';
  2. const urlsToCache = [
  3.   '/cat-button/',
  4.   '/cat-button/index.html',
  5.   '/cat-button/cat.jpg',
  6.   '/cat-button/Cat-screaming-sound-effect.mp3'
  7. ];
  8.  
  9. // Install event - cache resources
  10. self.addEventListener('install', (event) => {
  11.   event.waitUntil(
  12.     caches.open(CACHE_NAME)
  13.       .then((cache) => {
  14.         return cache.addAll(urlsToCache);
  15.       })
  16.   );
  17. });
  18.  
  19. // Fetch event - serve from cache when offline
  20. self.addEventListener('fetch', (event) => {
  21.   event.respondWith(
  22.     caches.match(event.request)
  23.       .then((response) => {
  24.         // Return cached version or fetch from network
  25.         return response || fetch(event.request);
  26.       })
  27.   );
  28. });
  29.  
  30. // Activate event - clean up old caches
  31. self.addEventListener('activate', (event) => {
  32.   event.waitUntil(
  33.     caches.keys().then((cacheNames) => {
  34.       return Promise.all(
  35.         cacheNames.map((cacheName) => {
  36.           if (cacheName !== CACHE_NAME) {
  37.             return caches.delete(cacheName);
  38.           }
  39.         })
  40.       );
  41.     })
  42.   );
  43. });
Advertisement
Add Comment
Please, Sign In to add comment