Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
116 lines (105 loc) · 2.52 KB
/
Copy pathsw.js
File metadata and controls
116 lines (105 loc) · 2.52 KB
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
const CACHE_NAME = 'clicker-toni-s-studios-v2';
// JS à la racine
const rootJS = [
'main.js',
'rebirthSystem.js'
].map(file => `./${file}`);
// JS dans /menus/
const menuJS = [
'dev.js',
'machines.js',
'settings.js',
'shop.js',
'upgrades.js'
].map(file => `./menus/${file}`);
// JS dans /modules/
const moduleJS = [
'animations.js',
'formatters.js',
'initCoinDrop.js',
'stats.js'
].map(file => `./modules/${file}`);
// Fichiers statiques
const staticFiles = [
'./',
'./index.html',
'./style.css',
];
// Icônes
const iconPaths = [
'favicon.ico',
'favicon.svg',
'apple-touch-icon.png',
'favicon-96x96.png',
'site.webmanifest',
'web-app-manifest-192x192.png',
'web-app-manifest-512x512.png'
].map(file => `./icons/${file}`);
// Liste finale
const urlsToCache = [
...staticFiles,
...rootJS,
...menuJS,
...moduleJS,
...iconPaths
];
// --- INSTALL ---
self.addEventListener('install', event => {
event.waitUntil(
caches.open(CACHE_NAME).then(async cache => {
await Promise.allSettled(
urlsToCache.map(async url => {
try {
const resp = await fetch(url);
if (resp.ok) {
await cache.put(url, resp.clone());
} else {
console.warn(`❌ Skip: ${url} (status ${resp.status})`);
}
} catch (err) {
console.warn(`❌ Skip: ${url} (${err})`);
}
})
);
self.skipWaiting();
})
);
});
// --- ACTIVATE ---
self.addEventListener('activate', event => {
event.waitUntil(
caches.keys().then(keys =>
Promise.all(
keys.filter(key => key !== CACHE_NAME)
.map(key => caches.delete(key))
)
).then(() => self.clients.claim())
);
});
// --- FETCH : stratégie network-first ---
self.addEventListener('fetch', event => {
if (event.request.method !== 'GET') return;
// Navigation HTML
if (event.request.mode === 'navigate') {
event.respondWith(
fetch(event.request)
.then(resp => {
const respClone = resp.clone();
caches.open(CACHE_NAME).then(cache => cache.put(event.request, respClone));
return resp;
})
.catch(() => caches.match('./index.html'))
);
return;
}
// Autres ressources
event.respondWith(
fetch(event.request)
.then(resp => {
const respClone = resp.clone();
caches.open(CACHE_NAME).then(cache => cache.put(event.request, respClone));
return resp;
})
.catch(() => caches.match(event.request))
);
});