puerta/internal/server/static/serviceworker.js

33 lines
898 B
JavaScript
Raw Normal View History

2023-01-04 04:21:49 +00:00
// SPDX-License-Identifier: Apache-2.0
// Copyright © 2022 Roberto Hidalgo <nidito@un.rob.mx>
2022-12-30 06:51:51 +00:00
importScripts(
'https://storage.googleapis.com/workbox-cdn/releases/6.4.1/workbox-sw.js'
);
workbox.loadModule('workbox-strategies');
2022-12-30 06:51:51 +00:00
self.addEventListener("install", event => {
console.log("Service worker installed");
2023-01-04 04:21:49 +00:00
const urlsToCache = ["/login", "/", "index.css", "/index.js", "/login.js", "/webauthn.js"];
2022-12-30 06:51:51 +00:00
event.waitUntil(
caches.open("pwa-assets")
.then(cache => {
return cache.addAll(urlsToCache);
})
);
});
self.addEventListener("activate", event => {
console.log("Service worker activated");
});
self.addEventListener('fetch', event => {
if (event.request.url.endsWith('.js') || event.request.url.endsWith('.css')) {
2022-12-30 06:51:51 +00:00
const cacheFirst = new workbox.strategies.CacheFirst();
event.respondWith(cacheFirst.handle({request: event.request}));
}
2023-01-04 04:21:49 +00:00
});