Reference
Route tree
The TanStack route tree tifa build generates from app/
- The framework's own root route has no component. Its
errorComponentis the framework's default component, and itsnotFoundComponentshows that component with a 404. When the root Layout exportsdocumentHead, the root route'sheadreturns that constant. React only renders<div id="tifa-root">inside<body>. After rendering, the SSR entry builds the document as a string:<html>carries thehtmlAttributesof the root Layout'sdocumentHeadwith thehtmlAttributesof the matched routes'headresults merged over them (see head.md). The head holds, in order, the charset, theheadofbuild.json, the module preloads of the matched routes (see Preloading the chunks of a page), and between<!--tifa-head-->and<!--/tifa-head-->the tags written from theheadresults of the matched routes (which tags are picked follows TanStack'sHeadContent, and attributes are written by React DOM's rules, see head.md). The body is the app root plus the router's dehydration script. So the page is still a complete document when any level fails, returns 404, or the rootLayoutfails. The browser subscribes to the router's matches (TanStack'sHeadContentreads them too). When a navigation finishes orrouter.invalidate()finishes loading data, and the matches changed, it compares the elements between the two comments by the same rules: unchanged tags are kept (nonces are compared by reading the element'snonceproperty, because on a page with CSP the nonce HTML attribute reads as empty). When the order changes, it moves only the elements that are out of place, the way React moves keyed children (moving a style rebuilds its stylesheet, moving a stylesheet link processes it again). New tags are created with DOM methods (no HTML parsing) and inserted in document order. Hydration does not touch the head the server wrote. The browser also sets the attributes of<html>from the same matches. It changes only the ones whose value changed, and takes away only those it set before, so attributes set by anything else stay. Once the router has hydrated, the browser syncs the head and the attributes with the matches the server rendered, which changes nothing but notes which attributes the server wrote, and only then follows the matches. The browser entry renders what TanStack'sRouterClientrenders, with that step after the hydration of the router. For pages without SSR, Go writes a document with only the charset, theheadofbuild.jsonand the module preloads of the page, and<html>without attributes. The browser sets thehtmlAttributesof the root Layout'sdocumentHeadat once, then those of the routes once they have loaded, and writes the head. - The root Layout (
app/layout.tsx, andLayoutinapp/route.go) is the pathless route_appunder the root route. - Every other Layout is a route whose path is its folder below the folder of its parent Layout, so its path parameters are part of its match identity.
app/projects/project+under the root Layout isprojects/$project, with the route ID/_app/projects/$project. A Layout inlayout_.tsxhas no parent Layout: it is a route right under the root route, whose path is its whole folder belowapp/, soapp/storeisstorewith the route ID/store, and itserrorComponentis the framework's default component. - Each Layout route has a
notFoundComponentthat shows itsErrorBoundary, or the nearest ancestor's, with aTifaErrorwhose status is 404 (missingin the runtime). The router runs withnotFoundMode: 'fuzzy': for a URL no route matches, it loads the routes that match the start of the URL and shows thenotFoundComponentof the deepest of them that has one, in itsOutlet. So the 404 shows inside the deepest Layout the URL matches, the one the server finds too (see 404). - A page path is its folder below the folder of its Layout. A page in the Layout's own folder is that Layout's index route
/, with the route ID/_app/projects/$project/.name+becomes$name, andname++becomes$(parameter name_splat). Literal folder names hold only characters that ServeMux, the router and a URL read the same way, so they are written as they are. - If any two generated routes end up with the same TanStack ID, the build fails.
- Routes with a
Loadget aloaderand use TanStack's defaults for how long its data counts as fresh: stale at once (staleTime: 0), preloaded data reused for 30 seconds, and a revisit shows the cached data while it loads again in the background (defaultStaleReloadMode: 'background'). Nothing in the runtime reloads data after a write, see handlers.md.loaderDepsholds only the query parameters inQuery, the fields of the query struct of theLoad.remountDepsis the path parameters of the folder of the route and of the folders above it, whatever theLoadreads, and no query parameter: another query loads the data again and keeps the component and its state, as TanStack Router does by default. A route without aLoadremounts the same way. - When
Loadfails, 404 included, the loader throws aTifaErrorwith the status and the public message, which the route'serrorComponentshows. A page route'serrorComponentis its ownErrorBoundaryor that of the nearest ancestor Layout. A Layout route's is that of its parent Layout, so a failed Layout shows theErrorBoundaryof the level above. - The router treats paths as case-sensitive, like ServeMux. Query parameters are parsed the way Go reads a query string: values are strings, repeated keys are string arrays, and nothing is parsed as JSON. The codec of the route then converts the parameters the
Loadreads by their types, see the next point. When written back to the URL, they follow the browser and form style (space becomes+), and common URLs stay as they are. Characters that Go'surl.ParseQuerywould read differently (;&=+%#) are always percent-encoded, so Go gets the same value on reload. - A route whose
LoadorLayoutreads query parameters gets avalidateSearchmade bysearchCodecof the runtime from the kind of each field ("page": "int","color": "string[]"), andPageSearch(orLayoutSearch) typed by the same kinds, forLink,navigateanduseSearch, see Query parameters in Link and navigate. The codec converts a value by the rules of the Go decoder and throws for a value that is not valid, and a route-levelonErrorturns what TanStack makes of that (aSearchParamError) into aTifaErrorwith status 400 and the message Go answers with, so theErrorBoundarysees one error from either side. The parameters Go does not read stay as the address has them. TanStack lays the result over the parsed search, so a parameter that was not passed, an empty value among them, comes back asundefined, and a route below gets the converted values of the route above. Before the page is taken over, the browser writes the address as the router reads it, without the empty numbers and bools, and with the numbers written the shortest way, as the server rendered the page, so the router does not load the page again. - The server answers a file of the public folder, and anything under
/assets/or/__tifa/, before it routes, but a parameter folder at the root ofapp/(such asapp/lang+) also matches a path like/favicon.svg. Soroutes.tsxholdspublicPaths, the URL paths of the files in the public folder (tifa buildandtifa devread thebuild.publicfolder), and the root route turns every navigation to one of them, or to a path under/assets/or/__tifa/, into a load of the document, with no request for navigation data. This covers<Link>,router.navigateandrouter.preloadRoute(a preload does nothing). Every route with apage.tsxcarriesstaticData: { page: true }, andisPage(router, pathname)in the runtime is true only for a path that a page matches whole, and false for the paths above, API routes and URLs that show a 404. - Two kinds of route are added for URLs that match no page. Below a Layout with no page at its own URL, an index route whose loader (
unmatched) throws a 404, so that URL shows the 404 inside the Layout. Below the root Layout, a route$for the rest of the path whoseparams.parse(outside) takes only a URL that no route below the root Layout, or below alayout_.tsx, starts with: the root Layout has no path, so the router would match such a URL with the root route alone, without the root Layout. Its loader throws the 404 as well. These routes have no component and noLoad, and Go has no route for them. page.tsx,layout.tsxandlayout_.tsxmust have a default export. The optional exportsheadandErrorBoundarymap toheadanderrorComponent, and a Layout'sErrorBoundaryto itsnotFoundComponenttoo. TanStack keeps onlymeta,links,scriptsandstylesof whatheadreturns, so eachheadgoes throughrouteHeadof the runtime, which carries thehtmlAttributesit returns as one more entry ofmetathat the head leaves out. AheadwithouthtmlAttributesreturns what it returned before. The root Layout may also exportdocumentHead, with thehtmlAttributesof<html>in it. A route that does not exportErrorBoundaryexplicitly uses theErrorBoundaryof the nearest ancestor that exports one, or the framework's default component when none does. When the server renders an error, it uses the route's ownerrorComponent, and in the browser the nearest error boundary catches it, so both sides show the same component. TanStack wraps a route whoseerrorComponentcan preload in Suspense. In the browser that is everyErrorBoundaryof a module that the browser bundle loads lazily butroutes.tsximports statically (one that exportshead, see Route modules in the browser), while the SSR bundle has the component itself. Such routes getwrapInSuspense: false, so the browser hydrates the tree the server rendered. The router loads the chunk of theErrorBoundarybefore it renders the route, as it does for the route's component. These exports must be declared or exported by name directly in the file (re-exports throughexport *do not count).