Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
59 lines (58 loc) · 3.36 KB
/
Copy pathsw.js
File metadata and controls
59 lines (58 loc) · 3.36 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
const PREFIX = "reconversion-control-";
const BUILD = "__BUILD_SHA__";
const CACHE = PREFIX + (BUILD.indexOf("__") === 0 ? "dev" : BUILD.slice(0, 12));
const CORE = ["./", "./index.html", "./access-mode.js", "./courses-public.html", "./courses-public.js", "./style.css", "./data.js", "./course-foundations.js", "./course-race.js", "./course-operations.js", "./learning.js", "./english.js", "./drive-learning.js", "./daily-five.js", "./deep-dives.js", "./evidence-checks.js", "./cloud-transport.js", "./cloud-ui.js", "./app.js", "./project-plan.js", "./manifest.webmanifest", "./assets/icon-192.png", "./assets/icon-512.png"];
const BASE = new URL(self.registration.scope);
const INDEX = new URL("./index.html", BASE).href;
const ASSETS = new Set(CORE.map(function(path) { return new URL(path, BASE).pathname; }));
self.addEventListener("install", function(event) {
event.waitUntil(caches.open(CACHE).then(function(cache) {
// Une nouvelle version ne doit pas recopier un ancien HTTP cache Safari.
return cache.addAll(CORE.map(function(path) {
return new Request(new URL(path, BASE), { cache: "reload" });
}));
}).then(function() { return self.skipWaiting(); }));
});
self.addEventListener("activate", function(event) {
event.waitUntil(caches.keys().then(function(keys) {
// Les autres applications GitHub Pages partagent le même domaine.
return Promise.all(keys.filter(function(key) {
return key.startsWith(PREFIX) && key !== CACHE;
}).map(function(key) { return caches.delete(key); }));
}).then(function() { return self.clients.claim(); }));
});
self.addEventListener("fetch", function(event) {
const request = event.request, url = new URL(request.url);
if (request.method !== "GET" || url.origin !== BASE.origin || !url.pathname.startsWith(BASE.pathname)) return;
if (request.mode === "navigate") {
// An OAuth code must never be cached or replaced by an offline app shell.
if (url.searchParams.has("code") || url.searchParams.has("error")) {
event.respondWith(fetch(new Request(request, {cache:"no-store"})).catch(() => new Response("Connexion interrompue : reconnecte-toi avec le réseau. Aucune donnée locale effacée.", {status:503, headers:{"Content-Type":"text/plain; charset=utf-8","Cache-Control":"no-store"}})));
return;
}
const publicIndex = new URL("./courses-public.html", BASE).href;
const fallback = url.pathname === new URL(publicIndex).pathname ? publicIndex : INDEX;
event.respondWith(caches.open(CACHE).then(async function(cache) {
try {
const response = await fetch(new Request(request, { cache: "no-cache" }));
if (response.ok) {
if (url.pathname === BASE.pathname || url.pathname === new URL(INDEX).pathname) await cache.put(INDEX, response.clone());
return response;
}
return await cache.match(fallback) || response;
} catch (error) {
return await cache.match(fallback) || Response.error();
}
}));
return;
}
if (!ASSETS.has(url.pathname)) return;
event.respondWith(caches.open(CACHE).then(async function(cache) {
// Les paramètres de version ne font pas perdre les fichiers préchargés.
const key = url.origin + url.pathname, cached = await cache.match(key);
if (cached) return cached;
const response = await fetch(request);
if (response.ok) await cache.put(key, response.clone());
return response;
}));
});