Serverless Python Backend in the Browser: How Datasette Lite Intercepts Requests via Service Worker and Runs Them in WebAssemblyNote · digest for December 2
Ligne claire comic cutaway of a browser: a Service Worker gatekeeper at the entrance hands a request envelope to a Web Worker office running Python and SQLite.
Note3 sections≈ 5 min

Serverless Python Backend in the Browser: How Datasette Lite Intercepts Requests via Service Worker and Runs Them in WebAssembly

You open a site with maps, SQL filters, POST forms, cookies, and streaming CSV exports. The address bar shows real URLs, the Back button works, and Python 3.14 runs under the hood—yet there is no backend server at all. It's hosted as pure static files for zero dollars.

That is how Datasette Lite works after its November 2026 reboot—the browser edition of Datasette, Simon Willison's tool for exploring SQLite databases. You can reuse this same pattern for any Python web app without rewriting it as an SPA.

Why WebAssembly Alone Isn't Enough and Why Service Worker Helps

The first Datasette Lite (2022) took a brute-force approach: Python in WebAssembly (WASM) rendered HTML and injected it via innerHTML. That worked for basic tables, but <script> tags never executed—breaking JS plugins, static assets, POST forms, and cookies.

To convince the browser it's talking to a real server, you need a Service Worker (sw.js), a background proxy that intercepts fetch requests on the origin. You can't run Python inside the Service Worker itself, though: browsers kill an idle Service Worker after 30 seconds, and Chromium aborts fetch handlers after five minutes.

Picture a lobby guard who gets pulled off duty after 30 seconds of quiet. Storing boxes under their desk is pointless—they'll vanish. Instead, the guard reads the room number on each envelope and hands it to a worker sitting inside an open office.

In Datasette Lite, sw.js is the lobby guard, the browser tab (shell.js) is the room, and a Module Web Worker (webworker.js) sits at the desk. It stays alive as long as the tab stays open, keeping Pyodide 314.0.8 (CPython 3.14 in WASM), Datasette, and SQLite loaded in memory.

Tracing One Request: From iframe to SQLite Response

When you open lite.datasette.io, shell.js generates a random <session> ID and opens an <iframe> at /app/<session>/data/places. That prefix isolates tabs: one Service Worker serves the whole origin, so if one tab runs a datasette-maplibre map while another tests a bug in Datasette 1.0a42, their requests never collide.

Here is the actual interceptor from sw.js:

const SCOPE_PATH = new URL("./", self.location.href).pathname;
const APP_PREFIX = SCOPE_PATH + "app/";

// Extract the tab's session ID from /app/<session>/...
function sessionFor(rawUrl) {
  const url = new URL(rawUrl);
  if (url.origin !== self.location.origin) return null;
  if (!url.pathname.startsWith(APP_PREFIX)) return null;
  const session = url.pathname.slice(APP_PREFIX.length).split("/")[0];
  return session || null;
}

self.addEventListener("fetch", (event) => {
  const session = sessionFor(event.request.url);
  if (session) {
    // Route the request to the matching tab's Python worker
    event.respondWith(handleAppRequest(event.request, session));
    return;
  }
  const url = new URL(event.request.url);
  const isInternal = url.origin === self.location.origin
    && url.pathname.startsWith("/-/");
  if (isInternal) {
    // Catch plugins that hardcode "/-/..." paths
    event.respondWith(handleHardcodedPath(event, url));
  }
});

Using clients.matchAll(), the Service Worker finds the owning tab and forwards the request down shell.js -> webworker.js. To stay ahead of Chromium's five-minute cutoff, a 4-minute timeout (REPLY_TIMEOUT_MS = 4 * 60 * 1000) returns a clean 504.

Inside Pyodide, an ASGI bridge handles the request (ASGI is the standard interface used by Datasette, FastAPI, and Django to call Python apps in memory without sockets or TCP). Because Service Workers can't read cookies, Python keeps its own cookie jar, runs SQL against SQLite, and streams a Response back into the <iframe>. CSRF protection, SSE/CSV streaming, and request cancellation all work, while the iframe path mirrors into #fragment for bookmarks and the Back button.

You configure the environment via URL parameters: ?url=, ?csv=, and ?json= load data files, ?ref=1.0a43 pulls a GitHub release, ?install=datasette-maplibre installs a PyPI plugin, and ?sqlite=3.54.1 activates any of 163 WASM builds of SQLite (PEP 783 allowed WASM wheels on PyPI starting in April 2026).

Where This Pattern Wins and Where It Hits a Ceiling

For data exploration, sandboxes, and bug reports, the wins are immediate: zero hosting cost, local file privacy, and reproducible GitHub Issues via a "before/after fix" link bundling code, plugins, and data.

The trade-off is cold start: the browser downloads Pyodide with Python 3.14, packages, and data files (webworker.js fetches files in parallel with the runtime). All databases and cookies live in tab memory and disappear when you close it.

Browser limits apply too: you need Service Workers (disabled in Firefox Private Windows) and Module Web Workers (Chrome 80+, Safari 15+, Firefox 114+). In Firefox and Safari, Referrer-Policy: no-referrer on /-/... fallback routes turns POSTs into bodyless GETs. External files need open CORS headers, and wiring Python to PostgreSQL or MySQL in WASM is still far harder than using built-in SQLite.

Want a deep diveVotes help us choose what to break down in detail. This material has 0 votes.
Sources · 4