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

View File

@ -31,7 +31,9 @@
"source-sans": "^3.46.0", "source-sans": "^3.46.0",
"vue": "^3.4.11", "vue": "^3.4.11",
"vue-i18n": "^9.13.1", "vue-i18n": "^9.13.1",
"vue-router": "^4.3.2" "vue-router": "^4.3.2",
"workbox-core": "^7.1.0",
"workbox-precaching": "^7.1.0"
}, },
"devDependencies": { "devDependencies": {
"@types/node": "^20.12.12", "@types/node": "^20.12.12",
@ -8806,7 +8808,7 @@
"version": "7.1.0", "version": "7.1.0",
"resolved": "https://registry.npmjs.org/workbox-core/-/workbox-core-7.1.0.tgz", "resolved": "https://registry.npmjs.org/workbox-core/-/workbox-core-7.1.0.tgz",
"integrity": "sha512-5KB4KOY8rtL31nEF7BfvU7FMzKT4B5TkbYa2tzkS+Peqj0gayMT9SytSFtNzlrvMaWgv6y/yvP9C0IbpFjV30Q==", "integrity": "sha512-5KB4KOY8rtL31nEF7BfvU7FMzKT4B5TkbYa2tzkS+Peqj0gayMT9SytSFtNzlrvMaWgv6y/yvP9C0IbpFjV30Q==",
"dev": true "license": "MIT"
}, },
"node_modules/workbox-expiration": { "node_modules/workbox-expiration": {
"version": "7.1.0", "version": "7.1.0",
@ -8843,7 +8845,7 @@
"version": "7.1.0", "version": "7.1.0",
"resolved": "https://registry.npmjs.org/workbox-precaching/-/workbox-precaching-7.1.0.tgz", "resolved": "https://registry.npmjs.org/workbox-precaching/-/workbox-precaching-7.1.0.tgz",
"integrity": "sha512-LyxzQts+UEpgtmfnolo0hHdNjoB7EoRWcF7EDslt+lQGd0lW4iTvvSe3v5JiIckQSB5KTW5xiCqjFviRKPj1zA==", "integrity": "sha512-LyxzQts+UEpgtmfnolo0hHdNjoB7EoRWcF7EDslt+lQGd0lW4iTvvSe3v5JiIckQSB5KTW5xiCqjFviRKPj1zA==",
"dev": true, "license": "MIT",
"dependencies": { "dependencies": {
"workbox-core": "7.1.0", "workbox-core": "7.1.0",
"workbox-routing": "7.1.0", "workbox-routing": "7.1.0",
@ -8877,7 +8879,6 @@
"version": "7.1.0", "version": "7.1.0",
"resolved": "https://registry.npmjs.org/workbox-routing/-/workbox-routing-7.1.0.tgz", "resolved": "https://registry.npmjs.org/workbox-routing/-/workbox-routing-7.1.0.tgz",
"integrity": "sha512-oOYk+kLriUY2QyHkIilxUlVcFqwduLJB7oRZIENbqPGeBP/3TWHYNNdmGNhz1dvKuw7aqvJ7CQxn27/jprlTdg==", "integrity": "sha512-oOYk+kLriUY2QyHkIilxUlVcFqwduLJB7oRZIENbqPGeBP/3TWHYNNdmGNhz1dvKuw7aqvJ7CQxn27/jprlTdg==",
"dev": true,
"dependencies": { "dependencies": {
"workbox-core": "7.1.0" "workbox-core": "7.1.0"
} }
@ -8886,7 +8887,6 @@
"version": "7.1.0", "version": "7.1.0",
"resolved": "https://registry.npmjs.org/workbox-strategies/-/workbox-strategies-7.1.0.tgz", "resolved": "https://registry.npmjs.org/workbox-strategies/-/workbox-strategies-7.1.0.tgz",
"integrity": "sha512-/UracPiGhUNehGjRm/tLUQ+9PtWmCbRufWtV0tNrALuf+HZ4F7cmObSEK+E4/Bx1p8Syx2tM+pkIrvtyetdlew==", "integrity": "sha512-/UracPiGhUNehGjRm/tLUQ+9PtWmCbRufWtV0tNrALuf+HZ4F7cmObSEK+E4/Bx1p8Syx2tM+pkIrvtyetdlew==",
"dev": true,
"dependencies": { "dependencies": {
"workbox-core": "7.1.0" "workbox-core": "7.1.0"
} }

View File

@ -53,6 +53,8 @@
"vite": "^5.2.11", "vite": "^5.2.11",
"vite-plugin-pwa": "^0.20.0", "vite-plugin-pwa": "^0.20.0",
"vitest": "^1.6.0", "vitest": "^1.6.0",
"vue-tsc": "^1.8.27" "vue-tsc": "^1.8.27",
"workbox-core": "^7.1.0",
"workbox-precaching": "^7.1.0"
} }
} }

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);
});
})
);
});

View File

@ -28,7 +28,13 @@ export default defineConfig({
mode: "development", mode: "development",
base: "/", base: "/",
injectRegister: "auto", injectRegister: "auto",
includeAssets: ["favicon.ico", "apple-touch-icon.png", "mask-icon.svg"], includeAssets: [
"favicon.ico",
"apple-touch-icon.png",
"mask-icon.svg",
"robots.txt",
"sitemap.xml"
],
manifest: { manifest: {
name: "HTWKalender", name: "HTWKalender",
short_name: "HTWKalender", short_name: "HTWKalender",
@ -81,47 +87,6 @@ export default defineConfig({
], ],
}, },
registerType: "autoUpdate", registerType: "autoUpdate",
workbox: {
globPatterns: ["**/*.{js,css,html,ico,png,svg,json,vue,txt,woff2}"],
cleanupOutdatedCaches: true,
runtimeCaching: [
{
urlPattern: ({ url }) => url.pathname.startsWith("/api/feed"),
method: "GET",
handler: "NetworkFirst",
options: {
cacheName: "calendar-feed-cache",
expiration: {
maxAgeSeconds: 12 * 60 * 60, // 12 hours
},
},
},
{
// Add the runtime caching strategy for /api/modules
urlPattern: ({url}) => url.pathname.startsWith('/api/modules'),
method: 'GET',
handler: 'NetworkFirst',
options: {
cacheName: 'modules-cache',
expiration: {
maxAgeSeconds: 24 * 60 * 60, // 1 day
},
},
},
{
urlPattern: /^https?.*/,
handler: "NetworkFirst",
options: {
cacheName: "https-calls",
expiration: {
maxEntries: 150,
maxAgeSeconds: 30 * 12 * 60 * 60, // 1 month
},
networkTimeoutSeconds: 10, // fall back to cache if api does not response within 10 seconds
},
},
],
},
devOptions: { devOptions: {
enabled: true, enabled: true,
/* when using generateSW the PWA plugin will switch to classic */ /* when using generateSW the PWA plugin will switch to classic */
@ -129,6 +94,7 @@ export default defineConfig({
navigateFallback: "index.html", navigateFallback: "index.html",
suppressWarnings: true, suppressWarnings: true,
}, },
strategies: "injectManifest",
}), }),
], ],
resolve: { resolve: {