Data protocol
The data protocol between the Go server, the render VM and the browser
Defined in js/runtime/data.ts. The Go side implements it:
-
Each
Loadresult is{status, data?, error?}: 200 withdataon success, 404 fortifa.ErrNotFound, and for other errors the status code plus the public message. -
A route ID names one
LoadorLayoutfunction: its folder plus/routefor a page or an API route (thepage.tsxandroute.goof the folder) and/layoutfor a Layout, such asapp/layoutandapp/projects/project+/route. -
The VM input for a full-page render is
{url, buildId, data: {route ID: result}, preload}, matchinginternal/ssr.Input. A result holdsstatus,errorandlocation, and for a successfulLoadthe numbertextof its JSON text. The texts are not in the input. They are the arguments ofstartRender(json, ...texts)after it, in the order of the route IDs, so Go does not escape them into the JSON of the input and the VM does not parse them out of it. -
On client navigation, loader calls for the same URL in the same event loop turn are merged into one GET of the page URL with the reserved query parameter
_tifa=<build ID>.<digits>, such as/projects/studio?_tifa=1f3a9c.011. The digits have one place for eachLoadorLayouton the chain of the URL, from the root Layout down to the page (a Layout or page with no function has no place), and a1asks for the data of that one. The server answers 400 when the number of digits is not the number ofLoads on the chain of the URL. The request has no header of Tifa, andfetchdoes not follow redirects on its own (redirect: "manual"). When the router preloads a page (a link under the pointer,defaultPreload: 'intent'), the value ends in.p, as in_tifa=1f3a9c.011.p. The server answers it like any other, only the URL tells it apart (a log can show which requests were preloads). The mark does not make requests a page view count: a preload result can later serve the navigation without another request, and a reload after an invalidation sends another one. To count page views exactly, count the client navigations the browser finishes (a router event), and the full page requests. The framework takes_tifaoff the request before any of your code runs:c.Query,c.QueryValues,c.Request().URL, the URL the page is rendered for, the ETag and the render memo key never have it, soOnRequestsees the same path for the page and for its data. ALoadthat reads a query parameter named_tifafails the build. A middleware set withtifa.SetHTTPHandlersees the request as it came, with_tifain it. There are three kinds of answers:{"routes": {route ID: result}, "session": ...}: whatever the HTTP status code, the browser hands out each entry ofroutes.sessionis there when the app has sessions, see Sessions in answers.{"redirect": "<URL>", "status": <3xx>}(with thesessionof the request when the app has sessions): when the whole request must redirect, such asOnRequestreturningtifa.Redirector a URL written in a non-canonical form (a ServeMux redirect, which carries status 308). The HTTP status code is 200. Every route in the request gets this redirect, and the loader throws TanStack'sredirect().- Anything else:
OnRequestrejected the whole request. The answer is{"error": message}with the error's status code, and all loaders in the batch fail with that status.
When the build IDs differ, the server answers 409 with its own build ID in the response header
X-Tifa-Build, and the browser reloads the current page once for that build (several 409s reload only once, and a late 409 from a preload does not jump to the preload target). A page that has reloaded for that build and gets the 409 again does not reload: every loader of the batch fails with the 409. The browser keeps this insessionStorageundertifa-reload:<build ID>, see Pages open while you deploy. If the browser still gets a 3xx (from a proxy, for example), it loads that URL as a full page. -
A handler function of a
route.genmodule (see handlers.md) sends the request headerX-Tifa-Call: 1and the build ID of its page inX-Tifa-Build, and does not letfetchfollow redirects. AfterOnRequestand before the handler, a server with pages answers 409 with its own build ID in the response headerX-Tifa-Buildwhen the two differ, and the browser reloads the page once, as for a data request. The function throwsTifaErrorand does not send the call again. The handler of the app can return 409 too, and that answer has noX-Tifa-Buildheader, so the function reloads nothing for it. A request withoutX-Tifa-Buildis not checked. When the handler orOnRequestreturnstifa.Redirect, such a request is answered with 200,Cache-Control: no-store,X-Tifa-Redirect: <3xx>and the same{"redirect": "<URL>", "status": <3xx>}as a data request. The function reads it as a redirect only with that header, since the result of a handler may have the same shape. Every other answer to a call that succeeds is{"result"?: ..., "session"?: ...}(see Sessions in answers), and a handler that writes its own response is passed on as it is. A request withoutX-Tifa-Call, from curl or another program, still gets the 3xx. Both answers carryVary: X-Tifa-Call. -
A full page carries each
Load's data to the browser in the router's dehydration script. Go gives the data of each successfulLoadto the render as the JSON text it wrote, as an argument ofstartRenderof its own (thetextof its result in the input numbers it,RenderResultinjs/runtime/server.tsx), and the render parses that text. In that text Go writes each character above U+FFFF as the escapes of its surrogate pair, such as\ud83d\ude00for 😀, which stand for the same character: seroval, which writes the text into the page, takesJSON.stringifyonly for a string without surrogates, and goes over any other one code unit at a time, which is much slower in beastjs. Without the escapes, one emoji in the data of an order list ofexamples/shopmakes its render 9% slower, and one in a table of 1,000 rows 2.3 times slower. An object or array goes into the page as that same text, one JSON string through TanStack's serialization adapter (keytifa.data), and the browser reads it back withJSON.parse. The VM does not write that string into the page itself. From 4,096 code units on, it writes a slot in its place, made of a~, the nonce of the render, a dot, the number of the text and a dot, andPool.Renderfills each slot once the VM is free again, with the text written as seroval writes a string in a script: the quote and the backslash escaped, line breaks, tab, backspace and form feed as letters,<as\x3Cso that no</scriptor<!--can end or hide the element, U+2028 and U+2029 as escapes, and each byte that is no UTF-8 as U+FFFD.Pool.Renderis the only way a page leaves the VM, so every path that sends, caches or compresses a page gets it filled. The nonce is 128 random bits thatPool.Renderdraws for each render after the Loads ran, so no data can spell a slot, and a page that holds a~and the nonce anywhere but in a whole slot is not served (ssr.ErrSlot). A slot is filled only where seroval writes it: as the whole string argument of the adapter's call,$_TSR.t.get("tifa.data")("<slot>"), inside a<script>element that holds JavaScript and has not ended. The escape of a JavaScript string is right nowhere else, so a real slot in an attribute or the text of the page that a component copied it into, or in another place of a script, is anssr.ErrSlottoo. This is the trust boundary: the code of the page can still write any HTML, as it always could, but it cannot get the text of the data written into a context of its choice. The check looks at the few KB before each slot and takes under 1 µs on a page of 221 KB. A bundle called without a nonce writes the text itself, as before. Writing the string in the VM took 100 to 180 instructions for each byte of data (a scan for characters to escape, a copy with the escapes, and one more copy of the page when it crossed back to Go). Go fills a slot by copying the page once more, about 60 µs for a page of 130 KB, which is why a text under 4,096 code units stays in the VM. The router starts a script tag of its own for a text over 16K code units, so a page with such a text has two script tags fewer than before, which a short slot no longer asks for: the page is 80 bytes smaller and the browser runs the same statements in the same order. So the browser reads exactly the values the server rendered, a-0and__proto__keys included, and text formatted from them, such as-¥0.00from Intl, hydrates as it was rendered. TanStack's default serializer would take several times as long in the VM for the same data. The cost is a slightly larger page, since the JSON string escapes its quotes: about 3% before compression on the pages ofexamples/shop. Strings, numbers and other single values go through the default serializer. The router's constructor type leaves adapters to TanStack Start, and setting them on a router of its own is not documented (TanStack/router#7796), soTestGeneratedRouterBehaveschecks that the page usestifa.dataand that the data survives hydration, andtifa buildchecks that the app installs the TanStack Router these tests ran with (see the app's tsconfig and Vite config). -
The browser gets the build ID from TanStack's dehydrated data.
-
The
TifaErrora loader throws for a failedLoadis written into the page through TanStack's serialization adapter (keytifa.TifaError). After hydration it is still aTifaErrorwith astatus, so theErrorBoundarycomponent sees the same error on the server and in the browser.