-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
35 lines (32 loc) · 936 Bytes
/
sw.js
File metadata and controls
35 lines (32 loc) · 936 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
const CACHE_NAME = 'snake-pro-v1';
// Add all the files your game needs to run here
const ASSETS_TO_CACHE = [
'./',
'./index.html',
'./style.css',
'./script.js',
'./snake-logo.jpg',
'./Sounds/eat.mp3',
'./Sounds/dead.mp3',
'./Sounds/start.mp3',
'./Sounds/levelup.mp3',
'./Sounds/bgm.mp3'
];
// Install event: Downloads and caches all assets
self.addEventListener('install', (event) => {
event.waitUntil(
caches.open(CACHE_NAME).then((cache) => {
console.log('Opened cache');
return cache.addAll(ASSETS_TO_CACHE);
})
);
});
// Fetch event: Serves files from cache if offline
self.addEventListener('fetch', (event) => {
event.respondWith(
caches.match(event.request).then((response) => {
// Return cached file if found, otherwise fetch from network
return response || fetch(event.request);
})
);
});