Repository navigation
Expand file tree
/
Copy pathsw.js
More file actions
82 lines (72 loc) · 3.63 KB
/
Copy pathsw.js
File metadata and controls
82 lines (72 loc) · 3.63 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
/**
* BenchD Service Worker
*
* Purpose: GitHub Pages cannot set custom HTTP headers.
* We inject Cross-Origin-Opener-Policy and Cross-Origin-Embedder-Policy
* on every response so the page becomes "cross-origin isolated".
* This is required to enable SharedArrayBuffer (needed for WASM threads
* and true multi-core benchmarking).
*
* Debug: all lifecycle events and every header injection are logged.
* Open DevTools → Application → Service Workers to confirm active state.
* Open DevTools → Console to see per-request injection logs.
*/
const SW_VERSION = '1.1.0';
const DEBUG = false;
// ── Install ────────────────────────────────────────────────────────────────
self.addEventListener('install', (event) => {
if (DEBUG) console.log(`[SW ${SW_VERSION}] install event fired — calling skipWaiting()`);
// skipWaiting forces this SW to become active immediately,
// even if a previous SW is still running.
self.skipWaiting();
});
// ── Activate ───────────────────────────────────────────────────────────────
self.addEventListener('activate', (event) => {
if (DEBUG) console.log(`[SW ${SW_VERSION}] activate event fired — calling clients.claim()`);
// clients.claim() makes this SW take control of all open pages right now.
// Without this, pages opened BEFORE the SW registered won't be controlled
// and won't get the injected headers until a full reload.
event.waitUntil(
self.clients.claim().then(() => {
if (DEBUG) console.log(`[SW ${SW_VERSION}] clients.claim() done — controlling all open clients`);
})
);
});
// ── Fetch (COOP/COEP header injection) ────────────────────────────────────
self.addEventListener('fetch', (event) => {
if (event.request.method !== 'GET') return;
const url = event.request.url;
// Only intercept same-origin requests.
// Cross-origin responses (e.g. Google Fonts) are opaque — we can't add headers to them.
if (!url.startsWith(self.location.origin)) {
// Uncomment the line below if you want to see ALL skipped requests:
// if (DEBUG) console.log(`[SW] SKIP (cross-origin): ${url}`);
return;
}
event.respondWith(
fetch(event.request)
.then((response) => {
if (response.status === 204 || response.status === 304) {
return response;
}
const newHeaders = new Headers(response.headers);
// These two headers together make the page "cross-origin isolated"
// which re-enables SharedArrayBuffer in modern browsers.
newHeaders.set('Cross-Origin-Opener-Policy', 'same-origin');
newHeaders.set('Cross-Origin-Embedder-Policy', 'require-corp');
const short = url.replace(self.location.origin, '') || '/';
if (DEBUG) console.log(
`[SW] INJECTED COOP+COEP → ${short} (HTTP ${response.status})`
);
return new Response(response.body, {
status: response.status,
statusText: response.statusText,
headers: newHeaders,
});
})
.catch((err) => {
console.error(`[SW] Fetch FAILED for: ${url}`, err);
throw err;
})
);
});