Skip to Content
Reference

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 Load result is {status, data?, error?}: 200 with data on success, 404 for tifa.ErrNotFound, and for other errors the status code plus the public message.

  • A route ID names one Load or Layout function: its folder plus /route for a page or an API route (the page.tsx and route.go of the folder) and /layout for a Layout, such as app/layout and app/projects/project+/route.

  • The VM input for a full-page render is {url, buildId, data: {route ID: result}, preload}, matching internal/ssr.Input. A result holds status, error and location, and for a successful Load the number text of its JSON text. The texts are not in the input. They are the arguments of startRender(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 each Load or Layout on the chain of the URL, from the root Layout down to the page (a Layout or page with no function has no place), and a 1 asks for the data of that one. The server answers 400 when the number of digits is not the number of Loads on the chain of the URL. The request has no header of Tifa, and fetch does 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 _tifa off 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, so OnRequest sees the same path for the page and for its data. A Load that reads a query parameter named _tifa fails the build. A middleware set with tifa.SetHTTPHandler sees the request as it came, with _tifa in it. There are three kinds of answers:

    • {"routes": {route ID: result}, "session": ...}: whatever the HTTP status code, the browser hands out each entry of routes. session is there when the app has sessions, see Sessions in answers.
    • {"redirect": "<URL>", "status": <3xx>} (with the session of the request when the app has sessions): when the whole request must redirect, such as OnRequest returning tifa.Redirect or 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's redirect().
    • Anything else: OnRequest rejected 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 in sessionStorage under tifa-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.gen module (see handlers.md) sends the request header X-Tifa-Call: 1 and the build ID of its page in X-Tifa-Build, and does not let fetch follow redirects. After OnRequest and before the handler, a server with pages answers 409 with its own build ID in the response header X-Tifa-Build when the two differ, and the browser reloads the page once, as for a data request. The function throws TifaError and does not send the call again. The handler of the app can return 409 too, and that answer has no X-Tifa-Build header, so the function reloads nothing for it. A request without X-Tifa-Build is not checked. When the handler or OnRequest returns tifa.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 without X-Tifa-Call, from curl or another program, still gets the 3xx. Both answers carry Vary: 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 successful Load to the render as the JSON text it wrote, as an argument of startRender of its own (the text of its result in the input numbers it, RenderResult in js/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\ude00 for 😀, which stand for the same character: seroval, which writes the text into the page, takes JSON.stringify only 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 of examples/shop makes 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 (key tifa.data), and the browser reads it back with JSON.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, and Pool.Render fills 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 \x3C so that no </script or <!-- 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.Render is 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 that Pool.Render draws 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 an ssr.ErrSlot too. 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 -0 and __proto__ keys included, and text formatted from them, such as -¥0.00 from 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 of examples/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), so TestGeneratedRouterBehaves checks that the page uses tifa.data and that the data survives hydration, and tifa build checks 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 TifaError a loader throws for a failed Load is written into the page through TanStack's serialization adapter (key tifa.TifaError). After hydration it is still a TifaError with a status, so the ErrorBoundary component sees the same error on the server and in the browser.