Improve WebGL asset caching

This commit is contained in:
yangbear
2026-07-14 02:03:44 +08:00
parent 53f856e44e
commit 53fe281ecc
3 changed files with 67 additions and 6 deletions
+21
View File
@@ -15,11 +15,32 @@ self.addEventListener('install', function (e) {
const cache = await caches.open(cacheName);
console.log('[Service Worker] Caching all: app shell and content');
await cache.addAll(contentToCache);
await self.skipWaiting();
})());
});
self.addEventListener('activate', function (e) {
e.waitUntil((async function () {
const keys = await caches.keys();
await Promise.all(keys.map(function (key) {
return key === cacheName ? Promise.resolve() : caches.delete(key);
}));
await self.clients.claim();
})());
});
self.addEventListener('fetch', function (e) {
if (e.request.method !== 'GET') {
return;
}
e.respondWith((async function () {
const url = new URL(e.request.url);
const isBuildAsset = url.pathname.includes('/Build/') || url.pathname.includes('/StreamingAssets/');
if (!isBuildAsset && !url.pathname.endsWith('/TemplateData/style.css')) {
return fetch(e.request);
}
let response = await caches.match(e.request);
console.log(`[Service Worker] Fetching resource: ${e.request.url}`);
if (response) { return response; }