nof.moe/public/sw.js

138 lines
3.0 KiB
JavaScript

importScripts('/workbox/workbox-sw.js');
workbox.setConfig({
modulePathPrefix: '/workbox/'
});
const { core, precaching, routing, strategies, expiration, cacheableResponse, backgroundSync } = workbox;
const { CacheFirst, NetworkFirst, NetworkOnly, StaleWhileRevalidate } = strategies;
const { ExpirationPlugin } = expiration;
const { CacheableResponsePlugin } = cacheableResponse;
const cacheSuffixVersion = '-240101a',
// precacheCacheName = core.cacheNames.precache,
// runtimeCacheName = core.cacheNames.runtime,
maxEntries = 100;
self.addEventListener('activate', (event) => {
event.waitUntil(
caches.keys().then((keys) => {
return Promise.all(keys.map((key) => {
if (!key.includes(cacheSuffixVersion)) return caches.delete(key);
}));
})
);
});
core.setCacheNameDetails({
prefix: 'amaneintro',
suffix: cacheSuffixVersion
});
core.skipWaiting();
core.clientsClaim();
precaching.cleanupOutdatedCaches();
routing.registerRoute(
/.*cfdn\.nofated\.win/,
new CacheFirst({
cacheName: 'static-immutable' + cacheSuffixVersion,
fetchOptions: {
mode: 'cors',
credentials: 'omit'
},
plugins: [
new ExpirationPlugin({
maxAgeSeconds: 30 * 24 * 60 * 60,
purgeOnQuotaError: true
})
]
})
);
routing.registerRoute(
/.*cdn\.nofated\.win/,
new CacheFirst({
cacheName: 'static-immutable' + cacheSuffixVersion,
fetchOptions: {
mode: 'cors',
credentials: 'omit'
},
plugins: [
new ExpirationPlugin({
maxAgeSeconds: 30 * 24 * 60 * 60,
purgeOnQuotaError: true
})
]
})
);
routing.registerRoute(
/.*fonts\.gstatic\.cn/,
new CacheFirst({
cacheName: 'static-immutable' + cacheSuffixVersion,
fetchOptions: {
mode: 'cors',
credentials: 'omit'
},
plugins: [
new ExpirationPlugin({
maxAgeSeconds: 30 * 24 * 60 * 60,
purgeOnQuotaError: true
})
]
})
)
const StaleWhileRevalidateInstance = new StaleWhileRevalidate();
/*
* Others img
* Method: staleWhileRevalidate
* cacheName: img-cache
*/
routing.registerRoute(
// Cache image files
/.*\.(?:png|jpg|jpeg|gif|webp)/,
StaleWhileRevalidateInstance
);
/*
* Static Assets
* Method: staleWhileRevalidate
* cacheName: static-assets-cache
*/
routing.registerRoute(
// Cache CSS files
/.*\.(css|js)/,
// Use cache but update in the background ASAP
StaleWhileRevalidateInstance
);
/*
* sw.js - Revalidate every time
* staleWhileRevalidate
*/
routing.registerRoute(
'/sw.js',
StaleWhileRevalidateInstance
);
routing.registerRoute(
new RegExp('moe'),
StaleWhileRevalidateInstance
);
routing.registerRoute(
/.*localhost/,
new NetworkOnly()
);
/*
* Default - Serve as it is
* StaleWhileRevalidate
*/
routing.setDefaultHandler(
new NetworkOnly()
);