@noverax/courier-tanstack-start serves the proxy that createProxyCaller and createProxyQueryCaller call from two server routes of a TanStack Start app: the calls go to one, reads over GET and writes over POST, and the streams of server-sent events to the other. Every call your server code makes — a server function, a server route, a page while the server renders it — carries the user's session. It works with TanStack Start for React and for Solid.
In one command
npx @noverax/create-courierIn a TanStack Start app, for React or for Solid, it adds what this page sets up by hand — the proxy route, the server entry that registers your APIs, Courier's plugin and Studio's in vite.config — with a typed example API, its mock, and a page at /courier. It builds the app once before it checks the types of what it added: TanStack Start writes the types of the app's routes as it builds. An app Rsbuild builds gets Courier's plugin in rsbuild.config instead — see Rsbuild. See Create Courier.
Install
npm install @noverax/courier-tanstack-start @noverax/courierThe proxy routes
A proxy client sends its calls to /api/request and opens its streams at /api/sse, unless its proxyPath names others. Each is a server route:
import { createFileRoute } from "@tanstack/react-router"
import { apiRoute } from "@noverax/courier-tanstack-start"
export const Route = createFileRoute("/api/request")({ server: { handlers: apiRoute() } })import { createFileRoute } from "@tanstack/react-router"
import { sseRoute } from "@noverax/courier-tanstack-start"
export const Route = createFileRoute("/api/sse")({ server: { handlers: sseRoute() } })A Solid app imports createFileRoute from @tanstack/solid-router; the routes are the same.
The handlers read the APIs the server registered. Register them once, in a module only the server loads, such as the server entry:
import handler from "@tanstack/react-start/server-entry"
import { registerSources } from "@noverax/courier/server"
import { sources } from "./api"
registerSources(sources)
export default handlerA Solid app takes the handler from @tanstack/solid-start/server-entry. A route can be given its APIs itself instead: apiRoute({ sources }).
TanStack Start drops a route's server from the code it sends the browser, so neither the routes nor your APIs' definitions reach a page.
apiRoute()answers a read overGET, a write overPOST, and the preflight of a page on another origin thatallowedOriginslists overOPTIONS:apiRoute({ allowedOrigins: ["https://admin.example.com"] }).sseRoute()answers a stream overGETand a preflight overOPTIONS.- Any other method is answered with
405and anAllowheader. TanStack Start would otherwise render your app's page for a method a route has no handler for. - The proxy reads a JSON call up to its
maxBodyBytes, 1 MB, and an upload (multipart/form-data) up to itsmaxUploadBytes, 10 MB; pass others to raise them:apiRoute({ maxUploadBytes: 50 * 1024 * 1024 }).
The user's address that a call forwards is the address of the connection the request came on. The servers TanStack Start runs on — its dev server, vite preview, Nitro — keep Node's request with it, and the chain a local proxy recorded in X-Forwarded-For is kept before it.
The client
Courier's plugin gives the page its routes, built from the endpoints you mark .expose(). It runs on @noverax/courier-build, which only the build needs:
npm install --save-dev @noverax/courier-buildimport { courierRoutes } from "@noverax/courier-tanstack-start/plugin"
import { tanstackStart } from "@tanstack/react-start/plugin/vite"
import viteReact from "@vitejs/plugin-react"
import { defineConfig } from "vite"
export default defineConfig({ plugins: [tanstackStart(), viteReact(), courierRoutes()] })An app TanStack Start builds with Rsbuild takes the plugin from /rsbuild:
import { courierRoutes } from "@noverax/courier-tanstack-start/rsbuild"
import { defineConfig } from "@rsbuild/core"
import { pluginReact } from "@rsbuild/plugin-react"
import { tanstackStart } from "@tanstack/react-start/plugin/rsbuild"
export default defineConfig({ plugins: [pluginReact(), tanstackStart(), courierRoutes()] })Pair with createProxyCaller on the client, given those routes, in a module that registers the config, so they are typed from it — see Exposing Endpoints:
import { createProxyCaller } from "@noverax/courier/client"
import { routes } from "@noverax/courier/routes"
import type config from "../courier.config"
declare module "@noverax/courier/routes" {
interface Register {
config: typeof config
}
}
// The routes name the key this source has in your `sources` map —
// it is what the proxy addresses, and it travels as `?_s=<name>`.
export const caller = createProxyCaller(routes.api)The caller sends to /api/request and /api/sse on the page's own origin, the routes above.
Cancelled calls
When the browser cancels a call — an aborted upload, a query it no longer needs, a stream it closes — the proxy stops its own upstream request. TanStack Start's servers end the request's signal when the client leaves, in development and in production alike.
The user's session
An API that keeps a session gets the user's session with every call server code makes for a request. TanStack Start keeps the request it is serving itself, and the entry for your framework finds it: @noverax/courier-tanstack-start/react, or /solid for Solid.
In server functions
import { createServerCaller } from "@noverax/courier-tanstack-start/react"
import { api } from "./api"
export const server = createServerCaller(api)import { createServerFn } from "@tanstack/react-start"
import { server } from "./api.server"
export const getMe = createServerFn().handler(() => server.users.me())createServerCaller(api) registers where to find the request as it is made, so nothing else needs setting up. Keep it where only the server loads it. TanStack Start keeps a *.server.ts module out of the browser's code — a build that would ship one fails — and drops a server function's handler from it, with what only the handler imports. A loader is no such place: it runs in the browser too, on every navigation after the first.
/react and /solid carry TanStack Start's server-only marker, so a build whose page takes either stops — under Vite with TanStack Start's own trace of the imports that led there — rather than ship Courier's server code to the browser.
In code the server renders
A call made through @noverax/courier/client — in a loader, or a component the server renders — takes the session while the server renders the page once every request is served with it at hand. Add Courier's request middleware in src/start.ts:
import { createCsrfMiddleware, createMiddleware, createStart } from "@tanstack/react-start"
import { requestContext } from "@noverax/courier-tanstack-start/react"
export const startInstance = createStart(() => ({
requestMiddleware: [
createCsrfMiddleware({ filter: (ctx) => ctx.handlerType === "serverFn" }),
createMiddleware().server(requestContext()),
],
}))requestContext()is the middleware's server part. The browser loadssrc/start.tstoo, and TanStack Start drops what.server()is given from the browser's code, Courier's package with it.- Keep TanStack Start's own
createCsrfMiddlewarein the list. Without asrc/start.ts, TanStack Start protects your server functions with it by itself; once the file is there, it adds none of its own.
The cookies the API sets
The cookies the API sets reach the browser on the response TanStack Start prepares — a page's, a server function's, a server route's, a redirect's and an error's too — merged with the ones your own code sets, one for each name, domain and path. The calls the same request makes after them send the renewed session at once.
A page streams, and its headers leave with its first part. A cookie the API sets after that — for data a loader returns without waiting for it — cannot reach the browser:
- with
requestContext()insrc/start.ts, the log says so; - without it, the cookie is lost as quietly as one set with TanStack Start's own
setCookie.
Await in the loader what may renew the session, so the page waits for it. The user's address a call forwards is the connection's, after the chain a local proxy recorded. See User Sessions.
The double-submit token
An API whose session.csrf names where it hands its token out is asked for it before a write that finds none. A page whose writes come later — from the browser, through the proxy — can have the token handed to the browser with the page instead, as Django's ensure_csrf_cookie does. Give the page's route the middleware:
import { createFileRoute } from "@tanstack/react-router"
import { createMiddleware } from "@tanstack/react-start"
import { csrfTokenMiddleware } from "@noverax/courier-tanstack-start/react"
import { api } from "../api"
const csrfTokens = createMiddleware().server(csrfTokenMiddleware(api))
export const Route = createFileRoute("/account")({
server: { middleware: [csrfTokens] },
component: Account,
})- A request that carries the token asks nothing. Otherwise the token is asked for once, with the user's session and through the API's middleware, and reaches the browser with the page; the calls the same request makes after it send it at once.
- Each API is asked for its own:
csrfTokenMiddleware(api, billing). - TanStack Start drops a route's
serverfrom the browser's code, and the middleware with it. - A write goes through without the middleware too, since one that finds no token asks for it first: the middleware is for the browser to hold the token before its first write, and for a write the browser sends past the proxy.
See When there is no token yet.
Server functions
A server function is TanStack Start's own typed call from the browser to the server, its input validated with .inputValidator(). Call your API from its handler through createServerCaller, as above.
Courier adds no wrapper for them. TanStack Start's compiler turns each createServerFn() into a call where your module declares it, so a function a package declared for you would not be one.
Rsbuild
TanStack Start builds with Rsbuild as well as with Vite, and all of this page holds for an app Rsbuild builds, with Courier's plugin from /rsbuild, as The client shows. A module the browser loads that takes /react or /solid stops Rsbuild's build too, which refuses the Node modules of TanStack Start's server code in a page.
Under Rsbuild's dev server, a page's scripts keep the names the dev server gives them, and each edit reaches them under those names. A TanStack Start page preloads its scripts as modules, and WebKit runs a preloaded module again from its memory when the same tab loads the page again, whatever the server answers: in Safari, once you mark an endpoint .expose(), a new tab runs the routes the server renders, while a tab that showed the page before runs those from before when it loads it again.
npx @noverax/create-courier tells such an app by its rsbuild.config, or by @rsbuild/core where the app lists no Vite:
- it adds Courier's plugin to the config's
plugins, and builds the app withrsbuild buildbefore it checks the types; - its example API reads the API's address from
PUBLIC_API_URL, which Rsbuild builds in from.env; - Studio has no plugin for Rsbuild, so only the app's server connects to it, from
src/server.ts: the calls your pages make through the proxy show there as the server's, and the browser itself does not connect.
TanStack Start for Solid
A Solid app takes the same steps with Solid's packages:
@noverax/courier-tanstack-start/solidin place of/react;createServerFn,createMiddlewareandcreateCsrfMiddlewarefrom@tanstack/solid-start;createFileRoutefrom@tanstack/solid-router;- the server entry from
@tanstack/solid-start/server-entry.