diff --git a/.gitignore b/.gitignore index bba97bb..7618350 100644 --- a/.gitignore +++ b/.gitignore @@ -17,7 +17,7 @@ yarn.lock /playwright-report/ /blob-report/ /playwright/.cache/ -/scripts/ +/_scripts/ /pb_data/ /.tanstack/ /dist/ \ No newline at end of file diff --git a/scripts/generate-sw.mjs b/scripts/generate-sw.mjs new file mode 100644 index 0000000..05a4f21 --- /dev/null +++ b/scripts/generate-sw.mjs @@ -0,0 +1,73 @@ +import { generateSW } from 'workbox-build' + +const ONE_YEAR = 60 * 60 * 24 * 365 +const THIRTY_DAYS = 60 * 60 * 24 * 30 + +const { count, size, warnings } = await generateSW({ + swDest: 'dist/client/sw.js', + globDirectory: 'dist/client', + globPatterns: [ + 'assets/**/*.{js,css}', + 'favicon*.{ico,png}', + 'apple-touch-icon.png', + 'icon-192x192.png', + 'icon-512x512.png', + 'site.webmanifest', + 'styles.css', + ], + navigateFallback: null, + skipWaiting: true, + clientsClaim: true, + cleanupOutdatedCaches: true, + sourcemap: false, + runtimeCaching: [ + { + urlPattern: ({ url, request }) => + url.origin === self.location.origin && request.destination === 'image', + handler: 'CacheFirst', + options: { + cacheName: 'images-cache', + expiration: { + maxEntries: 60, + maxAgeSeconds: THIRTY_DAYS, + }, + cacheableResponse: { + statuses: [0, 200], + }, + }, + }, + { + urlPattern: /^https:\/\/fonts\.googleapis\.com\/.*/i, + handler: 'StaleWhileRevalidate', + options: { + cacheName: 'google-fonts-cache', + expiration: { + maxEntries: 10, + maxAgeSeconds: ONE_YEAR, + }, + cacheableResponse: { + statuses: [0, 200], + }, + }, + }, + { + urlPattern: /^https:\/\/fonts\.gstatic\.com\/.*/i, + handler: 'CacheFirst', + options: { + cacheName: 'gstatic-fonts-cache', + expiration: { + maxEntries: 10, + maxAgeSeconds: ONE_YEAR, + }, + cacheableResponse: { + statuses: [0, 200], + }, + }, + }, + ], +}) + +for (const warning of warnings) console.warn(warning) +console.log( + `sw.js generated: precached ${count} files, ${(size / 1024).toFixed(1)} KiB`, +) \ No newline at end of file