blog/source/sw.js

253 lines
5.7 KiB
JavaScript
Raw Permalink Normal View History

2023-11-26 04:17:18 +00:00
importScripts('/js/workbox/workbox-sw.js');
2023-11-26 03:32:35 +00:00
workbox.setConfig({
2023-11-26 04:17:18 +00:00
modulePathPrefix: '/js/workbox/'
2023-11-26 03:32:35 +00:00
});
const { core, precaching, routing, strategies, expiration, cacheableResponse, backgroundSync } = workbox;
const { CacheFirst, NetworkFirst, NetworkOnly, StaleWhileRevalidate } = strategies;
const { ExpirationPlugin } = expiration;
const { CacheableResponsePlugin } = cacheableResponse;
2024-01-01 04:16:21 +00:00
const cacheSuffixVersion = '-240101a',
2023-11-26 03:32:35 +00:00
// 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) => {
2023-11-26 04:17:18 +00:00
if (key.includes('waline-cdn-cache')) return caches.delete(key);
if (key.includes('waline-img-cache')) return caches.delete(key);
2023-11-26 03:32:35 +00:00
if (!key.includes(cacheSuffixVersion)) return caches.delete(key);
}));
})
);
});
core.setCacheNameDetails({
prefix: 'amaneblog',
suffix: cacheSuffixVersion
});
core.skipWaiting();
core.clientsClaim();
precaching.cleanupOutdatedCaches();
2023-11-26 03:35:12 +00:00
routing.registerRoute(
2024-01-01 04:16:21 +00:00
/.*cfdn\.nofated\.win/,
2023-11-26 03:32:35 +00:00
new CacheFirst({
cacheName: 'static-immutable' + cacheSuffixVersion,
fetchOptions: {
mode: 'cors',
credentials: 'omit'
},
plugins: [
new ExpirationPlugin({
maxAgeSeconds: 30 * 24 * 60 * 60,
purgeOnQuotaError: true
})
]
})
);
routing.registerRoute(
/.*cdn\.staticfile\.org/,
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
})
2023-11-30 10:31:09 +00:00
]
})
);
routing.registerRoute(
/.*www\.libravatar\.org/,
new CacheFirst({
cacheName: 'static-immutable' + cacheSuffixVersion,
fetchOptions: {
mode: 'cors',
credentials: 'omit'
},
plugins: [
new ExpirationPlugin({
maxAgeSeconds: 30 * 24 * 60 * 60,
purgeOnQuotaError: true
})
2023-11-26 03:32:35 +00:00
]
})
);
routing.registerRoute(
/.*cdn\.jsdelivr\.net/,
new CacheFirst({
cacheName: 'static-immutable' + cacheSuffixVersion,
fetchOptions: {
mode: 'cors',
credentials: 'omit'
},
plugins: [
new ExpirationPlugin({
maxAgeSeconds: 30 * 24 * 60 * 60,
purgeOnQuotaError: true
})
]
})
);
routing.registerRoute(
/.*fonts\.googleapis\.cn/,
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
})
]
})
);
routing.registerRoute(
new RegExp('google-analytics.com'),
new NetworkOnly({
plugins: [
new backgroundSync.BackgroundSyncPlugin('ga', {
maxRetentionTime: 12 * 60
}),
]
}),
"POST"
);
routing.registerRoute(
2023-11-26 04:17:18 +00:00
new RegExp('waline'),
2023-11-26 03:32:35 +00:00
new NetworkFirst({
plugins: [
2023-11-26 04:17:18 +00:00
new backgroundSync.BackgroundSyncPlugin('waline', {
2023-11-26 03:32:35 +00:00
maxRetentionTime: 12 * 60
}),
]
})
);
routing.registerRoute(
new RegExp('analytics.google.com'),
new NetworkOnly({
plugins: [
new backgroundSync.BackgroundSyncPlugin('ga_new', {
maxRetentionTime: 12 * 60
}),
]
}),
"POST"
)
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
);
2023-11-28 13:01:31 +00:00
routing.registerRoute(
2023-11-30 10:07:28 +00:00
// Cache fonts files
/.*\.(woff2|woff)/,
2023-11-28 13:01:31 +00:00
// Use cache but update in the background ASAP
StaleWhileRevalidateInstance
);
2023-11-26 03:32:35 +00:00
/*
* sw.js - Revalidate every time
* staleWhileRevalidate
*/
routing.registerRoute(
2023-11-26 04:30:58 +00:00
'/sw.js',
2023-11-26 03:32:35 +00:00
StaleWhileRevalidateInstance
);
routing.registerRoute(
new RegExp('blog'),
StaleWhileRevalidateInstance
);
routing.registerRoute(
/.*localhost/,
new NetworkOnly()
);
/*
* Default - Serve as it is
* StaleWhileRevalidate
*/
routing.setDefaultHandler(
new NetworkOnly()
2023-11-26 04:17:18 +00:00
);