Skip to Content
Senri
Runtimes

Browser (Service Worker)

Answer the Service Worker fetch event, or call app.fetch in a page

Senri can answer the Service Worker fetch event directly. Without a server, Senri replies to fetch calls from the page.

sw.ts
import {  } from "@senrijs/senri";

const  = ();
.("/api/todos/:id", ({  }) => ({ , : false }));

.("fetch", () => {
    if (new (..)..("/api/")) {
        .(.(.));
    }
});

Bundle sw.ts into one file with bun build or Vite, then register it in the page.

navigator.serviceWorker.register("/sw.js");
  • Requests outside /api/ are not answered, so they go to the network as usual
  • For the Service Worker types, add "webworker" to lib in tsconfig and write declare const self: ServiceWorkerGlobalScope;

Call app.fetch in a page

You can also call app.fetch directly in a page, without a Service Worker. This is useful for tests and for imitating a server.

import {  } from "@senrijs/senri";

const  = ();
.("/api/todos/:id", ({  }) => ({ , : false }));

const  = await .(new ("http://localhost/api/todos/1"));
await .();   // { id: "1", done: false }

CI checks that it works both in a Chromium page and in a Service Worker.

On this page