Provide ability to passthrough and hit network

This commit is contained in:
Eli Davis 2025-02-05 17:48:27 -08:00 committed by GitHub
parent 57a311369e
commit 268c971467
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

6
sw.js
View File

@ -1,4 +1,4 @@
function registerWasmHTTPListener(wasm, { base, cacheName, args = [] } = {}) { function registerWasmHTTPListener(wasm, { base, cacheName, passthroughFunc, args = [] } = {}) {
let path = new URL(registration.scope).pathname let path = new URL(registration.scope).pathname
if (base && base !== '') path = `${trimEnd(path, '/')}/${trimStart(base, '/')}` if (base && base !== '') path = `${trimEnd(path, '/')}/${trimStart(base, '/')}`
@ -17,6 +17,10 @@ function registerWasmHTTPListener(wasm, { base, cacheName, args = [] } = {}) {
WebAssembly.instantiateStreaming(source, go.importObject).then(({ instance }) => go.run(instance)) WebAssembly.instantiateStreaming(source, go.importObject).then(({ instance }) => go.run(instance))
addEventListener('fetch', e => { addEventListener('fetch', e => {
if (passthroughFunc && passthroughFunc(e.request)) {
e.respondWith(fetch(e.request))
return;
}
const { pathname } = new URL(e.request.url) const { pathname } = new URL(e.request.url)
if (!pathname.startsWith(path)) return if (!pathname.startsWith(path)) return