Skip to Content
Reference

Route tree

The TanStack route tree tifa build generates from app/

  • The framework's own root route has no component. Its errorComponent is the framework's default component, and its notFoundComponent shows that component with a 404. When the root Layout exports documentHead, the root route's head returns that constant. React only renders <div id="tifa-root"> inside <body>. After rendering, the SSR entry builds the document as a string: <html> carries the htmlAttributes of the root Layout's documentHead with the htmlAttributes of the matched routes' head results merged over them (see head.md). The head holds, in order, the charset, the head of build.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 the head results of the matched routes (which tags are picked follows TanStack's HeadContent, 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 root Layout fails. The browser subscribes to the router's matches (TanStack's HeadContent reads them too). When a navigation finishes or router.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's nonce property, 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's RouterClient renders, with that step after the hydration of the router. For pages without SSR, Go writes a document with only the charset, the head of build.json and the module preloads of the page, and <html> without attributes. The browser sets the htmlAttributes of the root Layout's documentHead at once, then those of the routes once they have loaded, and writes the head.
  • The root Layout (app/layout.tsx, and Layout in app/route.go) is the pathless route _app under 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 is projects/$project, with the route ID /_app/projects/$project. A Layout in layout_.tsx has no parent Layout: it is a route right under the root route, whose path is its whole folder below app/, so app/store is store with the route ID /store, and its errorComponent is the framework's default component.
  • Each Layout route has a notFoundComponent that shows its ErrorBoundary, or the nearest ancestor's, with a TifaError whose status is 404 (missing in the runtime). The router runs with notFoundMode: 'fuzzy': for a URL no route matches, it loads the routes that match the start of the URL and shows the notFoundComponent of the deepest of them that has one, in its Outlet. 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, and name++ 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 Load get a loader and 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. loaderDeps holds only the query parameters in Query, the fields of the query struct of the Load. remountDeps is the path parameters of the folder of the route and of the folders above it, whatever the Load reads, 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 a Load remounts the same way.
  • When Load fails, 404 included, the loader throws a TifaError with the status and the public message, which the route's errorComponent shows. A page route's errorComponent is its own ErrorBoundary or that of the nearest ancestor Layout. A Layout route's is that of its parent Layout, so a failed Layout shows the ErrorBoundary of 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 Load reads 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's url.ParseQuery would read differently (;&=+%#) are always percent-encoded, so Go gets the same value on reload.
  • A route whose Load or Layout reads query parameters gets a validateSearch made by searchCodec of the runtime from the kind of each field ("page": "int", "color": "string[]"), and PageSearch (or LayoutSearch) typed by the same kinds, for Link, navigate and useSearch, 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-level onError turns what TanStack makes of that (a SearchParamError) into a TifaError with status 400 and the message Go answers with, so the ErrorBoundary sees 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 as undefined, 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 of app/ (such as app/lang+) also matches a path like /favicon.svg. So routes.tsx holds publicPaths, the URL paths of the files in the public folder (tifa build and tifa dev read the build.public folder), 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.navigate and router.preloadRoute (a preload does nothing). Every route with a page.tsx carries staticData: { page: true }, and isPage(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 whose params.parse (outside) takes only a URL that no route below the root Layout, or below a layout_.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 no Load, and Go has no route for them.
  • page.tsx, layout.tsx and layout_.tsx must have a default export. The optional exports head and ErrorBoundary map to head and errorComponent, and a Layout's ErrorBoundary to its notFoundComponent too. TanStack keeps only meta, links, scripts and styles of what head returns, so each head goes through routeHead of the runtime, which carries the htmlAttributes it returns as one more entry of meta that the head leaves out. A head without htmlAttributes returns what it returned before. The root Layout may also export documentHead, with the htmlAttributes of <html> in it. A route that does not export ErrorBoundary explicitly uses the ErrorBoundary of 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 own errorComponent, and in the browser the nearest error boundary catches it, so both sides show the same component. TanStack wraps a route whose errorComponent can preload in Suspense. In the browser that is every ErrorBoundary of a module that the browser bundle loads lazily but routes.tsx imports statically (one that exports head, see Route modules in the browser), while the SSR bundle has the component itself. Such routes get wrapInSuspense: false, so the browser hydrates the tree the server rendered. The router loads the chunk of the ErrorBoundary before 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 through export * do not count).