Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
156 lines (142 loc) · 5.79 KB
/
Copy pathsw.js
File metadata and controls
156 lines (142 loc) · 5.79 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
148
149
150
151
152
153
154
155
156
/**
* Obelus's offline shell.
*
* The app opens with the Writer's existing Documents while offline: the shell
* (HTML) and the build's hashed JS/CSS are precached at install, and every
* successful online navigation re-syncs the cache to the current deploy. The
* Documents themselves live in IndexedDB and need no cache.
*
* Two properties matter:
*
* - A new worker takes over immediately (`skipWaiting` + `clients.claim`), and
* superseded assets are pruned, so a `wrangler rollback` actually restores
* the old shell instead of stranding the new one, and the cache cannot grow
* without bound across releases.
* - Only same-origin shell assets are handled. Provider calls and any other
* same-origin request are never intercepted, cached or replayed.
*/
// A constant, not keyed to the release: every deploy shares this one cache.
// What keeps it on the current deploy is the navigation-time prune below
// (`syncShellAssets`), which runs in whichever worker is active on every
// successful online navigation, so a deploy or a rollback needs no worker
// update. `activate` deleting other names only clears a cache this worker no
// longer uses.
const CACHE_NAME = "obelus-shell-v1";
const ASSET_PREFIX = "/assets/";
const SHELL_STATIC_URLS = ["/manifest.webmanifest", "/icon.svg"];
const CACHEABLE_DESTINATIONS = new Set(["script", "style", "image", "font", "manifest"]);
self.addEventListener("install", (event) => {
event.waitUntil(installShell());
});
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()),
);
});
self.addEventListener("fetch", (event) => {
const request = event.request;
if (request.method !== "GET") return;
const url = new URL(request.url);
if (url.origin !== self.location.origin) return;
if (request.mode === "navigate") {
// Network-first so a fresh deploy is picked up, falling back to the cached
// shell so the app still opens offline.
event.respondWith(networkFirstShell(request));
return;
}
// Only the shell's own static subresources are cached. A provider call is a
// `fetch()` with an empty destination, so even a Custom Connection pointed at
// this origin is passed through and can never be stored or replayed.
if (!isCacheableShellAsset(request, url.pathname)) return;
event.respondWith(cacheFirst(request));
});
/**
* Precache the current shell and everything it points at. Parsing the built
* `index.html` is what lets a static worker know the content-hashed asset
* names: without it, the very first offline open after install would find the
* HTML in the cache but none of its JS or CSS.
*/
async function installShell() {
const cache = await caches.open(CACHE_NAME);
const response = await fetch("/index.html", { cache: "reload" });
if (!response.ok) {
throw new Error(`Could not precache the shell (HTTP ${response.status}).`);
}
const html = await response.clone().text();
await cache.put("/index.html", response.clone());
await cache.put("/", response.clone());
await cache.addAll([...SHELL_STATIC_URLS, ...assetUrlsFrom(html)]);
await self.skipWaiting();
}
async function networkFirstShell(request) {
const cache = await caches.open(CACHE_NAME);
try {
const response = await fetch(request);
if (response.ok) {
await cache.put("/index.html", response.clone());
await syncShellAssets(cache, await response.clone().text());
}
return response;
} catch (error) {
// Offline or the network failed: serve the shell cached at install. The
// Library lives in IndexedDB, so an existing Document opens from here.
const cached = await cache.match("/index.html");
if (cached !== undefined) return cached;
throw error;
}
}
async function cacheFirst(request) {
const cache = await caches.open(CACHE_NAME);
const cached = await cache.match(request);
if (cached !== undefined) return cached;
const response = await fetch(request);
// Only same-origin, non-opaque responses are stored, so a provider reply can
// never enter the cache even if the origin check above were to change.
if (response.ok && response.type === "basic") {
await cache.put(request, response.clone());
}
return response;
}
/** Hashed assets that today's shell does not reference are from an old deploy. */
async function syncShellAssets(cache, html) {
const wanted = new Set(assetUrlsFrom(html));
const requests = await cache.keys();
await Promise.all(
requests
.filter((request) => {
const pathname = new URL(request.url).pathname;
return pathname.startsWith(ASSET_PREFIX) && !wanted.has(pathname);
})
.map((request) => cache.delete(request)),
);
}
function isCacheableShellAsset(request, pathname) {
// The browser sets a destination for subresources it loads (the built script
// and stylesheet, the manifest, the icon) and leaves it empty for every
// `fetch()` the app makes. That is what keeps a same-origin Custom Connection
// out of the cache however its base URL is shaped.
if (!CACHEABLE_DESTINATIONS.has(request.destination)) return false;
return pathname.startsWith(ASSET_PREFIX) || SHELL_STATIC_URLS.includes(pathname);
}
/**
* The `/assets/…` URLs Vite emits into the built `index.html` (the module
* script and the stylesheet), deduplicated. `DOMParser` is not available in a
* service worker, so this is a deliberate, narrow parse of markup this repo's
* own build produces.
*/
function assetUrlsFrom(html) {
const urls = [];
const pattern = /(?:src|href)="(\/assets\/[^"]+)"/g;
let match;
while ((match = pattern.exec(html)) !== null) {
if (!urls.includes(match[1])) urls.push(match[1]);
}
return urls;
}