Phase 8/module platform prerequisites #40
@@ -1,5 +1,6 @@
|
|||||||
/// <reference lib="webworker" />
|
/// <reference lib="webworker" />
|
||||||
import {
|
import {
|
||||||
|
classifyDisplayRequest,
|
||||||
DISPLAY_CACHE_NAMES,
|
DISPLAY_CACHE_NAMES,
|
||||||
type DisplayOfflineEnvironment,
|
type DisplayOfflineEnvironment,
|
||||||
type DisplayOfflineStatus,
|
type DisplayOfflineStatus,
|
||||||
@@ -56,14 +57,23 @@ self.addEventListener("activate", (event) => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
self.addEventListener("fetch", (event) => {
|
self.addEventListener("fetch", (event) => {
|
||||||
const handled = handleDisplayFetch(event.request, self.location.origin, environment);
|
// Classified synchronously, and `respondWith` is called **only** for what this worker actually
|
||||||
|
// handles. Answering every request — even by re-issuing it — would route the whole page through
|
||||||
|
// the worker, re-send request bodies from a different context, and make the worker's presence
|
||||||
|
// observable on requests it has no interest in. Not responding leaves the browser doing exactly
|
||||||
|
// what it would have done without a worker at all.
|
||||||
|
if (!classifyDisplayRequest(event.request, self.location.origin)) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
event.respondWith(
|
event.respondWith(
|
||||||
handled.then(
|
handleDisplayFetch(event.request, self.location.origin, environment).then((response) => {
|
||||||
(response) =>
|
if (!response) {
|
||||||
// `undefined` means this worker recognized nothing and must not change what the browser would
|
// Unreachable: the classification above is the same one `handleDisplayFetch` performs. A
|
||||||
// have done. Re-issuing the request is the only way to say that from inside `respondWith`.
|
// network error is the honest answer if that ever stops being true.
|
||||||
response ?? fetch(event.request),
|
throw new TypeError("Ekklesia display request was not handled.");
|
||||||
),
|
}
|
||||||
|
return response;
|
||||||
|
}),
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user