2021-06-05 10:14:16 +02:00

43 lines
1.1 KiB
JavaScript

const filesToCache = [
"./",
"fonts/VT323-Regular.ttf",
"bundle.js",
"bundle.js.map",
"favicon.ico",
"nesBattle.png",
"sound/brick.ogg",
"sound/explosion.ogg",
"sound/gameover.ogg",
"sound/gamestart.ogg",
"sound/steel.ogg",
"styles/main.css",
"stages/stage-1.json",
"stages/stage-2.json",
"stages/stage-3.json",
"libs/jquery-3.6.0.min.js"
]
const cacheName = "battleCity";
self.addEventListener('install', event => {
self.skipWaiting();
event.waitUntil(
caches.open(cacheName).then(cache => cache.addAll(filesToCache))
);
});
self.addEventListener('fetch', event => {
event.respondWith (
caches.match(event.request).then(response => {
if (response) return response;
return fetch(event.request)
.then(response => {
if (response.ok)
return caches.open(cacheName).then(cache => {
cache.put(event.request.url, response.clone());
return response;
});
});
})
)
});