Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const CACHE_NAME = 'cat-button-pwa-v1';
- const urlsToCache = [
- '/cat-button/',
- '/cat-button/index.html',
- '/cat-button/cat.jpg',
- '/cat-button/Cat-screaming-sound-effect.mp3'
- ];
- // Install event - cache resources
- self.addEventListener('install', (event) => {
- event.waitUntil(
- caches.open(CACHE_NAME)
- .then((cache) => {
- return cache.addAll(urlsToCache);
- })
- );
- });
- // Fetch event - serve from cache when offline
- self.addEventListener('fetch', (event) => {
- event.respondWith(
- caches.match(event.request)
- .then((response) => {
- // Return cached version or fetch from network
- return response || fetch(event.request);
- })
- );
- });
- // Activate event - clean up old caches
- self.addEventListener('activate', (event) => {
- event.waitUntil(
- caches.keys().then((cacheNames) => {
- return Promise.all(
- cacheNames.map((cacheName) => {
- if (cacheName !== CACHE_NAME) {
- return caches.delete(cacheName);
- }
- })
- );
- })
- );
- });
Advertisement
Add Comment
Please, Sign In to add comment