61 lines
1.8 KiB
JavaScript
61 lines
1.8 KiB
JavaScript
'use strict';
|
|
|
|
const name = 'example-pwa-site-v1';
|
|
const assets = [
|
|
'/',
|
|
'/index.html',
|
|
'/styles.css',
|
|
'/app.js',
|
|
'/images/Bear.png',
|
|
'/images/DeusEx.png',
|
|
'/images/Face.png',
|
|
'/images/Tony_Stark.jpg',
|
|
];
|
|
|
|
// install - https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/install_event
|
|
// activate - https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/activate_event
|
|
// message - https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/message_event
|
|
// fetch - https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/fetch_event
|
|
// sync - https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/sync_event
|
|
// push - https://developer.mozilla.org/en-US/docs/Web/API/ServiceWorkerGlobalScope/push_event
|
|
|
|
self.addEventListener('install', (event) => {
|
|
event.waitUntil(
|
|
caches.open(name).then((cache) => {
|
|
cache.addAll(assets);
|
|
}),
|
|
);
|
|
});
|
|
|
|
self.addEventListener('fetch', (event) => {
|
|
event.respondWith(
|
|
caches.match(event.request).then((res) => {
|
|
return res || fetch(event.request);
|
|
}),
|
|
);
|
|
});
|
|
|
|
const enableNavigationPreload = async () => {
|
|
if (self.registration.navigationPreload) {
|
|
await self.registration.navigationPreload.enable();
|
|
}
|
|
};
|
|
|
|
self.addEventListener('activate', (event) => {
|
|
event.waitUntil(enableNavigationPreload());
|
|
});
|
|
|
|
self.addEventListener('push', function (event) {
|
|
console.log('[Service Worker] Push Received.');
|
|
console.log(`[Service Worker] Push had this data: "${event.data.text()}"`);
|
|
|
|
const title = 'Push Codelab';
|
|
const options = {
|
|
body: 'Yay it works.',
|
|
icon: 'images/icon.png',
|
|
badge: 'images/badge.png',
|
|
};
|
|
|
|
event.waitUntil(self.registration.showNotification(title, options));
|
|
});
|