79 lines
2.1 KiB
JavaScript
79 lines
2.1 KiB
JavaScript
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',
|
|
'push-handlers.js',
|
|
],
|
|
// Hand-written Web Push handlers (public/push-handlers.js) are pulled into
|
|
// the generated SW at startup. importScripts runs them in the SW global
|
|
// scope, so their `push` / `notificationclick` listeners compose with
|
|
// Workbox's precache + runtime caching setup below.
|
|
importScripts: ['/push-handlers.js'],
|
|
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`,
|
|
) |