handler.js 490 B

1234567891011121314151617181920
  1. // src/adapter/service-worker/handler.ts
  2. var handle = (app, opts = {
  3. // To use `fetch` on a Service Worker correctly, bind it to `globalThis`.
  4. fetch: globalThis.fetch.bind(globalThis)
  5. }) => {
  6. return (evt) => {
  7. evt.respondWith(
  8. (async () => {
  9. const res = await app.fetch(evt.request, {}, evt);
  10. if (opts.fetch && res.status === 404) {
  11. return await opts.fetch(evt.request);
  12. }
  13. return res;
  14. })()
  15. );
  16. };
  17. };
  18. export {
  19. handle
  20. };