Worker routes (service-worker handlers) + worker redirect & bundle fixes#3
Merged
Conversation
9f613bb to
a7dd059
Compare
38859eb to
a7dd059
Compare
Drop the inline sourcemap from the worker build — its base64 map roughly doubled the served script and could push it past Chromium's ~6MB service-worker script-size limit (registration fails with 'Failed to access storage'). Also resolve react / react-dom / @tanstack/react-router / @tanstack/router-core from the app root so the worker bundles a SINGLE copy; a duplicate React/router instance makes SSR hooks read null.
Worker routes are the mirror of TanStack server routes, but the handlers
run in the service worker. Declare them with the `worker` route option
(createFileRoute('/api/x')({ worker: { handlers: { GET, POST, … } } })) —
the route's PATH is the identity (no name, no codegen; the router is the
dispatch table). A single stateful client can live in the worker so reads
and writes share one store. Call them with workerFetch(path, init): runs
the handler directly in the worker (e.g. a wsr loader), fetches the path
on the main thread (the worker intercepts it), 503 on the origin so callers
fall back. broadcastToClients posts invalidations to pages.
Also honor a redirect() thrown in beforeLoad/loader: it was captured on
router state but never acted on, so the worker rendered the original route
instead of redirecting. renderRequest now returns a real redirect Response.
Unit tests for both (47 passing) + a README 'Worker routes' section.
a7dd059 to
3e8d18b
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Adds worker routes — the mirror of TanStack server routes, but the handlers run in the service worker. Declare them with the
workerroute option; the route's path is the identity (no name, no codegen — the router is the dispatch table). This lets a single stateful client (a local-first / sync-engine client like Zero or Replicache) live in the worker so reads and writes both go through it, and a hard-load WSR render reflects exactly what the user just did — one client, one store, no cross-client round-trip, no stale flash.Call them with
workerFetch(path, init), which resolves in the right place:wsrloader on hard load) → runs the matched handler directly (a worker can't fetch itself);fetches the path, which the worker intercepts;503, so the caller falls back (e.g. empty shell).broadcastToClients(message)pushes invalidations to pages.Also in here (fixes surfaced building on WSR)
redirect()thrown inbeforeLoad/loaderwas captured on router state but never acted on, so the worker rendered the original route instead of redirecting (e.g. an auth gate →/login).renderRequestnow returns a real redirectResponse.null.Tests & docs
worker-fetch.test.ts(dispatch, request gating, the worker/main/server paths) andworker-redirect.test.ts. 47 tests passing.(Branch name is
feat/worker-functionsfor history reasons; the feature is worker routes.)