diff --git a/README.md b/README.md index 0f92940..fe00359 100644 --- a/README.md +++ b/README.md @@ -182,7 +182,9 @@ URL string of the WebAssembly module, example: `"path/to/my-module.wasm"`. An optional object containing: - `base` (`string`): Base path of the server, relative to the ServiceWorker's scope. +- `cacheName` (`string`): Name of the [Cache](https://developer.mozilla.org/en-US/docs/Web/API/Cache) to store the WebAssembly binary. - `args` (`string[]`): Arguments for the WebAssembly module. +- `passthrough` (`(request: Request): boolean`): Optional callback to allow passing the request through to network. ## Contributors ✨ diff --git a/sw.js b/sw.js index 7348a6d..11e0ae0 100644 --- a/sw.js +++ b/sw.js @@ -1,4 +1,4 @@ -function registerWasmHTTPListener(wasm, { base, cacheName, args = [] } = {}) { +function registerWasmHTTPListener(wasm, { base, cacheName, passthrough, args = [] } = {}) { let path = new URL(registration.scope).pathname if (base && base !== '') path = `${trimEnd(path, '/')}/${trimStart(base, '/')}` @@ -17,6 +17,11 @@ function registerWasmHTTPListener(wasm, { base, cacheName, args = [] } = {}) { WebAssembly.instantiateStreaming(source, go.importObject).then(({ instance }) => go.run(instance)) addEventListener('fetch', e => { + if (passthrough?.(e.request)) { + e.respondWith(fetch(e.request)) + return; + } + const { pathname } = new URL(e.request.url) if (!pathname.startsWith(path)) return