-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow more flexibility in choosing which requests get the COI workaro…
…und.
- Loading branch information
Showing
5 changed files
with
60 additions
and
35 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
// Copyright 2024 Remy Blank <[email protected]> | ||
// SPDX-License-Identifier: MIT | ||
|
||
(({document: doc, navigator: {serviceWorker: workers}}) => { | ||
if (!doc) { | ||
// Running in the service worker. Capture fetch() requests and add the | ||
// relevant headers. | ||
addEventListener('install', () => skipWaiting()); | ||
addEventListener('activate', e => e.waitUntil(clients.claim())); | ||
addEventListener('fetch', e => { | ||
const req = e.request; | ||
if (req.cache === 'only-if-cached' && req.mode !== 'same-origin') { | ||
return; | ||
} | ||
e.respondWith((async () => { | ||
const resp = await fetch(req); | ||
const {status, statusText, body} = resp; | ||
if (!status || status >= 400) return resp; | ||
const headers = new Headers(resp.headers); | ||
headers.set('Cross-Origin-Embedder-Policy', 'require-corp'); | ||
headers.set('Cross-Origin-Opener-Policy', 'same-origin'); | ||
headers.set('Cross-Origin-Resource-Policy', 'cross-origin'); | ||
return new Response(resp.body, {status, statusText, headers}); | ||
})()); | ||
}); | ||
return; | ||
} | ||
|
||
// Running in the main thread. Register the service worker if necessary. | ||
if (crossOriginIsolated) { | ||
console.info("[t-doc] Already cross-origin isolated"); | ||
return; | ||
} | ||
if (!isSecureContext) { | ||
console.warn("[t-doc] Not a secure context; COI workaround disabled"); | ||
return; | ||
} | ||
if (!workers) { | ||
console.warn( | ||
"[t-doc] No service worker container; COI workaround disabled"); | ||
return; | ||
} | ||
const script = doc.currentScript; | ||
workers.register(script.src, {scope: script.getAttribute('scope')}) | ||
.then(reg => { | ||
console.info('[t-doc] COI service worker registered'); | ||
reg.addEventListener('updatefound', () => location.reload()); | ||
if (reg.active && !workers.controller) location.reload(); | ||
}) | ||
.catch(e => { | ||
console.error( | ||
`[t-doc] Failed to register COI service worker: ${e}`); | ||
}); | ||
})(self); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters