Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
m2-starter-repo/sw.js
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
46 lines (42 sloc)
1.29 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Name of our cache | |
const cacheName = 'offline-pwa-gallery-v1'; | |
// URLs to cache for offline mode | |
const urlsToCache = [ | |
'https://cdn.skypack.dev/@ionic/core@next/css/core.css', | |
'https://cdn.skypack.dev/@ionic/core@next/css/ionic.bundle.css', | |
'https://cdn.jsdelivr.net/npm/@ionic/core/dist/ionic/ionic.esm.js', | |
'https://cdn.jsdelivr.net/npm/@ionic/core/dist/ionic/ionic.js', | |
'images/antelope.jpg', | |
'images/bobcat.jpg', | |
'images/caracal.jpg', | |
'images/caribou-bulls.jpg', | |
'images/caribou.jpg', | |
'images/cheetah.jpg', | |
'images/coyote.jpg', | |
'images/ferrets.jpg', | |
'images/ox.jpg', | |
'images/tiger.jpg', | |
'index.html', | |
'css/styles.css', | |
'./' | |
]; | |
// Cache files on service worker registration | |
self.addEventListener('install', (event) => { | |
event.waitUntil( | |
caches.open(cacheName).then((cache) => { | |
return cache.addAll(urlsToCache); | |
}) | |
); | |
}); | |
// Intercept fetch requests to serve cached assets | |
self.addEventListener('fetch', (event) => { | |
if(event.request.url.match(/^https:\/\//)){ | |
console.log('Browser is requesting:', event.request.url); | |
} | |
event.respondWith( | |
caches.match(event.request).then((cachedResponse) => { | |
// It can update the cache to serve updated content on the next request | |
return cachedResponse || fetch(event.request); | |
}) | |
); | |
}); |