cdn
This commit is contained in:
37
sw.js
37
sw.js
@ -2,17 +2,50 @@ importScripts(
|
||||
'https://storage.googleapis.com/workbox-cdn/releases/6.4.1/workbox-sw.js'
|
||||
);
|
||||
|
||||
let localStorageData = {
|
||||
cdns: [],
|
||||
disableCDN: true,
|
||||
};
|
||||
// yandere sim big files, cuphead, renpy, renpy, buckshot roulette, half life, undertale & deltarune, hollow knight
|
||||
let cacheable_files = ["YandereSim.data", "cuphead/Build/Build.data.unityweb.", "renpy.data", "game.zip", "buckshot-roulette.pck", "halva_en-", "game.unx", "bog.data"];
|
||||
|
||||
self.addEventListener("message", e => {
|
||||
if(e.data && e.data.type == "localStorage") {
|
||||
localStorageData = e.data;
|
||||
console.log(localStorageData);
|
||||
}
|
||||
});
|
||||
|
||||
if(workbox) {
|
||||
self.skipWaiting();
|
||||
workbox.core.clientsClaim();
|
||||
|
||||
workbox.routing.registerRoute(
|
||||
({url}) => {
|
||||
console.log(localStorageData)
|
||||
if(localStorageData.disableCDN) return false;
|
||||
if(localStorageData.cdns.length == 0) return false;
|
||||
if(url.origin != location.origin) return false;
|
||||
if(!url.pathname.includes("/resources/")) return false;
|
||||
for (const file of cacheable_files) {
|
||||
if(url.pathname.includes(file)) return true;
|
||||
}
|
||||
return false;
|
||||
},
|
||||
async ({url}) => {
|
||||
let cdn = localStorageData.cdns[0];
|
||||
console.warn(url.href);
|
||||
return Response.redirect(cdn + url.pathname, 302);
|
||||
}
|
||||
);
|
||||
|
||||
workbox.routing.registerRoute(
|
||||
/\.(?:png|gif|jpg|jpeg|svg|webp|avif|gif)$/,
|
||||
new workbox.strategies.CacheFirst({
|
||||
cacheName: 'images-cache',
|
||||
plugins: [
|
||||
new workbox.expiration.ExpirationPlugin({
|
||||
maxEntries: 600,
|
||||
maxEntries: 1200,
|
||||
maxAgeSeconds: 365 * 24 * 60 * 60,
|
||||
}),
|
||||
],
|
||||
@ -24,7 +57,7 @@ if(workbox) {
|
||||
cacheName: 'resources-cache',
|
||||
plugins: [
|
||||
new workbox.expiration.ExpirationPlugin({
|
||||
maxEntries: 100,
|
||||
maxEntries: 500,
|
||||
maxAgeSeconds: 365 * 24 * 60 * 60,
|
||||
}),
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user