Skip to content

Latest commit

 

History

History

tanstack-start

README.md

@lunora-example/tanstack-start

Server-side rendering with TanStack Start: the route loader fetches the data on the server, the HTML ships with the list already in it, and the same query goes live over a socket after hydration.

Deploy it

Warning

This example has no authentication. Deployed as-is, anyone with the URL can post to the board. It exists to demonstrate the SSR handover, not access control.

Deploy to Cloudflare

One click clones the repo, provisions the Durable Object namespace, prompts for the secrets in .dev.vars.example, and deploys. Or from a checkout:

pnpm --filter @lunora-example/tanstack-start run deploy

What it demonstrates

  • One worker, both jobs. wrangler.jsonc's main is virtual:lunora/worker, which Lunora's compose plugin resolves into a worker that routes /_lunora/* to Lunora and everything else to the TanStack Start SSR handler. One deploy, one origin, no CORS.
  • A loader that fills the cache the hooks read. lunoraQueryOptions is keyed identically to useQuery from @lunora/react, so the value the loader puts in the TanStack cache is the value the component finds on its first client render — no loading flash and no refetch on hydration.
  • Per-request clients. Both the QueryClient and the LunoraClient are built inside getRouter(). A module-level client on the server would leak one visitor's data into the next request's render.
  • A client that only opens a socket when it needs one. LunoraClient connects lazily on .subscribe(). Loaders only call .query(), so the server-side instance is plain HTTP; the browser instance goes live.

Run it

pnpm install
pnpm --filter @lunora-example/tanstack-start dev

Open the URL the dev server prints (http://localhost:5173 in the main checkout), then View Source — the messages are in the served HTML, not fetched afterwards. Open a second tab and post: both update over the socket.

About "consistent SSR snapshots"

Lunora has no "read every query at timestamp T" API. What it guarantees is that one query is a consistent read of its shard at the moment it runs.

So the way to render a coherent page is to make it one query against one shard. messages.board returns the list and its summary together for exactly that reason: splitting it into list + stats would be two reads with nothing tying them to the same instant, and the summary could describe a state the list never saw.

Two related rules follow, and the schema comments say so at the point they matter:

  • A query handler must not call Date.now(). A live query is re-evaluated whenever its shard pokes it, so a wall-clock value changes on every push and churns every subscriber. Everything a query returns should be a function of the data.
  • If a page genuinely needs reads from different shards, it is asking for a guarantee no shard can give it on its own. Fetch them, and design the UI to tolerate them being a moment apart.

Key snippets

The loader (src/routes/index.tsx)

export const Route = createFileRoute("/")({
    component: Home,
    loader: async ({ context }) => context.queryClient.ensureQueryData(lunoraQueryOptions(context.lunora, api.messages.board, BOARD_ARGS)),
});

function Home() {
    // Same key as the loader used → the server value, then live pushes.
    const board = useQuery(api.messages.board, BOARD_ARGS);
}

Plugin order (vite.config.ts)

plugins: [cloudflare({ viteEnvironment: { name: "ssr" } }), tanstackStart(), react(), lunora({ cloudflare: false })];

cloudflare() must come first so it owns the ssr environment before tanstackStart() configures it; lunora({ cloudflare: false }) then adds codegen, wrangler validation and the studio overlay without re-adding the Cloudflare plugin.

src/routeTree.gen.ts is generated by tanstackStart() and committed, so tsc --noEmit works without a build first.