bd-chapril/sw.js

45 lines
1.1 KiB
JavaScript
Raw Normal View History

2024-12-22 08:50:58 +01:00
// Service Worker
'use strict';
2024-12-24 13:54:42 +01:00
import RESOURCES from "resources.js";
2024-12-22 08:50:58 +01:00
const addResourcesToCache = async (RESOURCES) => {
const cache = await caches.open('v1');
await cache.addAll(RESOURCES);
};
const enableNavigationPreload = async () => {
if (self.registration.navigationPreload) {
// Enable navigation preloads!
await self.registration.navigationPreload.enable();
}
};
self.addEventListener('activate', (event) => {
event.waitUntil(enableNavigationPreload());
});
self.addEventListener('install', (event) => {
event.waitUntil(
addResourcesToCache(RESOURCES)
);
});
const putInCache = async (request, response) => {
const cache = await caches.open('v1');
await cache.put(request, response);
};
const cacheFirst = async (request) => {
const responseFromCache = await caches.match(request);
if (responseFromCache) {
return responseFromCache;
}
const responseFromNetwork = await fetch(request);
putInCache(request, responseFromNetwork.clone());
return responseFromNetwork;
};
self.addEventListener('fetch', (event) => {
event.respondWith(cacheFirst(event.request));
});