mirror of
https://github.com/Azgaar/Fantasy-Map-Generator.git
synced 2025-12-17 01:41:22 +01:00
Caching service worker (#794)
* register service worker * versioning code * cleanup * cache in Prod only
This commit is contained in:
parent
02e0669282
commit
f5cb800295
4 changed files with 155 additions and 51 deletions
68
sw.js
Normal file
68
sw.js
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
importScripts("https://storage.googleapis.com/workbox-cdn/releases/6.2.0/workbox-sw.js");
|
||||
|
||||
const {Route, registerRoute} = workbox.routing;
|
||||
const {CacheFirst} = workbox.strategies;
|
||||
const {CacheableResponsePlugin} = workbox.cacheableResponse;
|
||||
const {ExpirationPlugin} = workbox.expiration;
|
||||
|
||||
const DAY = 24 * 60 * 60;
|
||||
|
||||
const getPolitics = ({entries, days}) => {
|
||||
return [new CacheableResponsePlugin({statuses: [200]}), new ExpirationPlugin({maxEntries: entries, maxAgeSeconds: days * DAY})];
|
||||
};
|
||||
|
||||
registerRoute(
|
||||
({request, url}) => request.destination === "script" && !url.pathname.endsWith("min.js") && !url.pathname.includes("versioning.js"),
|
||||
new CacheFirst({
|
||||
cacheName: "fmg-scripts",
|
||||
plugins: getPolitics({entries: 100, days: 30})
|
||||
})
|
||||
);
|
||||
|
||||
registerRoute(
|
||||
({request}) => request.destination === "style",
|
||||
new CacheFirst({
|
||||
cacheName: "fmg-stylesheets",
|
||||
plugins: getPolitics({entries: 100, days: 30})
|
||||
})
|
||||
);
|
||||
|
||||
registerRoute(
|
||||
({request, url}) => request.destination === "script" && url.pathname.endsWith("min.js"),
|
||||
new CacheFirst({
|
||||
cacheName: "fmg-libs",
|
||||
plugins: getPolitics({entries: 100, days: 30})
|
||||
})
|
||||
);
|
||||
|
||||
registerRoute(
|
||||
new RegExp(".json$"),
|
||||
new CacheFirst({
|
||||
cacheName: "fmg-json",
|
||||
plugins: getPolitics({entries: 100, days: 30})
|
||||
})
|
||||
);
|
||||
|
||||
registerRoute(
|
||||
({request}) => request.destination === "image",
|
||||
new CacheFirst({
|
||||
cacheName: "fmg-images",
|
||||
plugins: getPolitics({entries: 1000, days: 30})
|
||||
})
|
||||
);
|
||||
|
||||
registerRoute(
|
||||
new RegExp(".svg$"),
|
||||
new CacheFirst({
|
||||
cacheName: "fmg-charges",
|
||||
plugins: getPolitics({entries: 1000, days: 30})
|
||||
})
|
||||
);
|
||||
|
||||
registerRoute(
|
||||
({request}) => request.destination === "font",
|
||||
new CacheFirst({
|
||||
cacheName: "fmg-fonts",
|
||||
plugins: getPolitics({entries: 200, days: 30})
|
||||
})
|
||||
);
|
||||
Loading…
Add table
Add a link
Reference in a new issue