feat:#5 added own service worker script for precache api during install

This commit is contained in:
Elmar Kresse
2024-08-13 20:41:45 +02:00
parent b848d26704
commit a22c7a1073
4 changed files with 44 additions and 48 deletions

28
frontend/public/sw.js Normal file
View File

@@ -0,0 +1,28 @@
import { precacheAndRoute, cleanupOutdatedCaches } from 'workbox-precaching';
import { clientsClaim } from 'workbox-core';
self.skipWaiting();
clientsClaim();
cleanupOutdatedCaches();
// Precache the files from the manifest
precacheAndRoute(self.__WB_MANIFEST);
// Custom precaching logic for the /api/modules endpoint
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open('api-modules-cache').then((cache) => {
return fetch('/api/modules')
.then((response) => {
if (response.ok) {
return cache.put('/api/modules', response);
}
throw new Error('Failed to fetch /api/modules');
})
.catch((error) => {
console.error('Precaching /api/modules failed:', error);
});
})
);
});