-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsw.js
More file actions
147 lines (128 loc) · 3.67 KB
/
Copy pathsw.js
File metadata and controls
147 lines (128 loc) · 3.67 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
/* ==========================================
PulseGrowth Service Worker (PWA)
------------------------------------------
- Precache app shell
- Versioned cache
- Network-first for navigation
- Stale-while-revalidate for same-origin assets
========================================== */
const CACHE_NAME = "pulsegrowth-v2";
const APP_SHELL = [
"./",
"./index.html",
"./manifest.json",
"./icon-192.png",
"./icon-512.png",
"./icon-maskable-192.png",
"./icon-maskable-512.png",
"./src/css/app.css",
"./src/js/i18n.js",
"./src/js/app.js"
];
/**
* INSTALL
* Cache core assets and activate immediately.
*/
self.addEventListener("install", (event) => {
event.waitUntil(
(async () => {
const cache = await caches.open(CACHE_NAME);
await cache.addAll(APP_SHELL);
await self.skipWaiting();
})()
);
});
/**
* ACTIVATE
* Remove old caches and take control of open clients.
*/
self.addEventListener("activate", (event) => {
event.waitUntil(
(async () => {
const keys = await caches.keys();
await Promise.all(
keys.map((key) => {
if (key !== CACHE_NAME) {
return caches.delete(key);
}
return Promise.resolve();
})
);
await self.clients.claim();
})()
);
});
/**
* FETCH
*
* 1) Navigation requests (pages): network-first
* - Good for app updates
* - Falls back to cached index when offline
*
* 2) Same-origin GET assets: stale-while-revalidate
* - Returns cached asset fast
* - Updates cache in background
*
* 3) Everything else: let browser handle it
*/
self.addEventListener("fetch", (event) => {
const request = event.request;
if (request.method !== "GET") return;
const url = new URL(request.url);
const isSameOrigin = url.origin === self.location.origin;
// 1) HTML navigation
if (request.mode === "navigate") {
event.respondWith(
(async () => {
try {
const networkResponse = await fetch(request);
// Refresh cached index for offline use
const cache = await caches.open(CACHE_NAME);
await cache.put("./index.html", networkResponse.clone());
return networkResponse;
} catch (err) {
const cached =
(await caches.match("./index.html")) ||
(await caches.match("./")) ||
(await caches.match("/index.html"));
if (cached) return cached;
return new Response("Offline", {
status: 503,
statusText: "Offline",
headers: { "Content-Type": "text/plain; charset=utf-8" }
});
}
})()
);
return;
}
// 2) Same-origin assets (icons, manifest, css/js if embedded externally later)
if (isSameOrigin) {
event.respondWith(
(async () => {
const cache = await caches.open(CACHE_NAME);
const cachedResponse = await cache.match(request);
const networkPromise = fetch(request)
.then(async (networkResponse) => {
if (
networkResponse &&
networkResponse.ok &&
(networkResponse.type === "basic" || networkResponse.type === "default")
) {
await cache.put(request, networkResponse.clone());
}
return networkResponse;
})
.catch(() => null);
// stale-while-revalidate
if (cachedResponse) {
event.waitUntil(networkPromise);
return cachedResponse;
}
const networkResponse = await networkPromise;
if (networkResponse) return networkResponse;
return new Response("", { status: 404, statusText: "Not Found" });
})()
);
}
});