@tanstack/react-router 1.121.20 → 1.121.24

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (242) hide show
  1. package/dist/cjs/Asset.cjs +108 -0
  2. package/dist/cjs/Asset.cjs.map +1 -0
  3. package/dist/cjs/Asset.d.cts +3 -0
  4. package/dist/cjs/CatchBoundary.cjs +114 -0
  5. package/dist/cjs/CatchBoundary.cjs.map +1 -0
  6. package/dist/cjs/CatchBoundary.d.cts +12 -0
  7. package/dist/cjs/ClientOnly.cjs +20 -0
  8. package/dist/cjs/ClientOnly.cjs.map +1 -0
  9. package/dist/cjs/ClientOnly.d.cts +29 -0
  10. package/dist/cjs/HeadContent.cjs +155 -0
  11. package/dist/cjs/HeadContent.cjs.map +1 -0
  12. package/dist/cjs/HeadContent.d.cts +7 -0
  13. package/dist/cjs/Match.cjs +257 -0
  14. package/dist/cjs/Match.cjs.map +1 -0
  15. package/dist/cjs/Match.d.cts +8 -0
  16. package/dist/cjs/Matches.cjs +136 -0
  17. package/dist/cjs/Matches.cjs.map +1 -0
  18. package/dist/cjs/Matches.d.cts +26 -0
  19. package/dist/cjs/RouterProvider.cjs +33 -0
  20. package/dist/cjs/RouterProvider.cjs.map +1 -0
  21. package/dist/cjs/RouterProvider.d.cts +10 -0
  22. package/dist/cjs/SafeFragment.cjs +8 -0
  23. package/dist/cjs/SafeFragment.cjs.map +1 -0
  24. package/dist/cjs/SafeFragment.d.cts +1 -0
  25. package/dist/cjs/ScriptOnce.cjs +28 -0
  26. package/dist/cjs/ScriptOnce.cjs.map +1 -0
  27. package/dist/cjs/ScriptOnce.d.cts +5 -0
  28. package/dist/cjs/Scripts.cjs +51 -0
  29. package/dist/cjs/Scripts.cjs.map +1 -0
  30. package/dist/cjs/Scripts.d.cts +1 -0
  31. package/dist/cjs/ScrollRestoration.cjs +39 -0
  32. package/dist/cjs/ScrollRestoration.cjs.map +1 -0
  33. package/dist/cjs/ScrollRestoration.d.cts +14 -0
  34. package/dist/cjs/Transitioner.cjs +115 -0
  35. package/dist/cjs/Transitioner.cjs.map +1 -0
  36. package/dist/cjs/Transitioner.d.cts +1 -0
  37. package/dist/cjs/awaited.cjs +48 -0
  38. package/dist/cjs/awaited.cjs.map +1 -0
  39. package/dist/cjs/awaited.d.cts +10 -0
  40. package/dist/cjs/fileRoute.cjs +107 -0
  41. package/dist/cjs/fileRoute.cjs.map +1 -0
  42. package/dist/cjs/fileRoute.d.cts +54 -0
  43. package/dist/cjs/history.d.cts +8 -0
  44. package/dist/cjs/index.cjs +289 -0
  45. package/dist/cjs/index.cjs.map +1 -0
  46. package/dist/cjs/index.d.cts +55 -0
  47. package/dist/cjs/lazyRouteComponent.cjs +78 -0
  48. package/dist/cjs/lazyRouteComponent.cjs.map +1 -0
  49. package/dist/cjs/lazyRouteComponent.d.cts +2 -0
  50. package/dist/cjs/link.cjs +324 -0
  51. package/dist/cjs/link.cjs.map +1 -0
  52. package/dist/cjs/link.d.cts +48 -0
  53. package/dist/cjs/matchContext.cjs +27 -0
  54. package/dist/cjs/matchContext.cjs.map +1 -0
  55. package/dist/cjs/matchContext.d.cts +3 -0
  56. package/dist/cjs/not-found.cjs +40 -0
  57. package/dist/cjs/not-found.cjs.map +1 -0
  58. package/dist/cjs/not-found.d.cts +9 -0
  59. package/dist/cjs/renderRouteNotFound.cjs +22 -0
  60. package/dist/cjs/renderRouteNotFound.cjs.map +1 -0
  61. package/dist/cjs/renderRouteNotFound.d.cts +2 -0
  62. package/dist/cjs/route.cjs +205 -0
  63. package/dist/cjs/route.cjs.map +1 -0
  64. package/dist/cjs/route.d.cts +95 -0
  65. package/dist/cjs/router.cjs +22 -0
  66. package/dist/cjs/router.cjs.map +1 -0
  67. package/dist/cjs/router.d.cts +72 -0
  68. package/dist/cjs/routerContext.cjs +33 -0
  69. package/dist/cjs/routerContext.cjs.map +1 -0
  70. package/dist/cjs/routerContext.d.cts +8 -0
  71. package/dist/cjs/scroll-restoration.cjs +24 -0
  72. package/dist/cjs/scroll-restoration.cjs.map +1 -0
  73. package/dist/cjs/scroll-restoration.d.cts +1 -0
  74. package/dist/cjs/serializer.d.cts +6 -0
  75. package/dist/cjs/structuralSharing.d.cts +8 -0
  76. package/dist/cjs/typePrimitives.d.cts +16 -0
  77. package/dist/cjs/useBlocker.cjs +165 -0
  78. package/dist/cjs/useBlocker.cjs.map +1 -0
  79. package/dist/cjs/useBlocker.d.cts +66 -0
  80. package/dist/cjs/useCanGoBack.cjs +8 -0
  81. package/dist/cjs/useCanGoBack.cjs.map +1 -0
  82. package/dist/cjs/useCanGoBack.d.cts +1 -0
  83. package/dist/cjs/useLoaderData.cjs +15 -0
  84. package/dist/cjs/useLoaderData.cjs.map +1 -0
  85. package/dist/cjs/useLoaderData.d.cts +8 -0
  86. package/dist/cjs/useLoaderDeps.cjs +14 -0
  87. package/dist/cjs/useLoaderDeps.cjs.map +1 -0
  88. package/dist/cjs/useLoaderDeps.d.cts +8 -0
  89. package/dist/cjs/useLocation.cjs +10 -0
  90. package/dist/cjs/useLocation.cjs.map +1 -0
  91. package/dist/cjs/useLocation.d.cts +7 -0
  92. package/dist/cjs/useMatch.cjs +47 -0
  93. package/dist/cjs/useMatch.cjs.map +1 -0
  94. package/dist/cjs/useMatch.d.cts +10 -0
  95. package/dist/cjs/useNavigate.cjs +55 -0
  96. package/dist/cjs/useNavigate.cjs.map +1 -0
  97. package/dist/cjs/useNavigate.d.cts +5 -0
  98. package/dist/cjs/useParams.cjs +16 -0
  99. package/dist/cjs/useParams.cjs.map +1 -0
  100. package/dist/cjs/useParams.d.cts +9 -0
  101. package/dist/cjs/useRouteContext.cjs +11 -0
  102. package/dist/cjs/useRouteContext.cjs.map +1 -0
  103. package/dist/cjs/useRouteContext.d.cts +3 -0
  104. package/dist/cjs/useRouter.cjs +32 -0
  105. package/dist/cjs/useRouter.cjs.map +1 -0
  106. package/dist/cjs/useRouter.d.cts +4 -0
  107. package/dist/cjs/useRouterState.cjs +29 -0
  108. package/dist/cjs/useRouterState.cjs.map +1 -0
  109. package/dist/cjs/useRouterState.d.cts +8 -0
  110. package/dist/cjs/useSearch.cjs +16 -0
  111. package/dist/cjs/useSearch.cjs.map +1 -0
  112. package/dist/cjs/useSearch.d.cts +9 -0
  113. package/dist/cjs/utils.cjs +72 -0
  114. package/dist/cjs/utils.cjs.map +1 -0
  115. package/dist/cjs/utils.d.cts +49 -0
  116. package/dist/esm/Asset.d.ts +3 -0
  117. package/dist/esm/Asset.js +91 -0
  118. package/dist/esm/Asset.js.map +1 -0
  119. package/dist/esm/CatchBoundary.d.ts +12 -0
  120. package/dist/esm/CatchBoundary.js +97 -0
  121. package/dist/esm/CatchBoundary.js.map +1 -0
  122. package/dist/esm/ClientOnly.d.ts +29 -0
  123. package/dist/esm/ClientOnly.js +20 -0
  124. package/dist/esm/ClientOnly.js.map +1 -0
  125. package/dist/esm/HeadContent.d.ts +7 -0
  126. package/dist/esm/HeadContent.js +139 -0
  127. package/dist/esm/HeadContent.js.map +1 -0
  128. package/dist/esm/Match.d.ts +8 -0
  129. package/dist/esm/Match.js +240 -0
  130. package/dist/esm/Match.js.map +1 -0
  131. package/dist/esm/Matches.d.ts +26 -0
  132. package/dist/esm/Matches.js +119 -0
  133. package/dist/esm/Matches.js.map +1 -0
  134. package/dist/esm/RouterProvider.d.ts +10 -0
  135. package/dist/esm/RouterProvider.js +33 -0
  136. package/dist/esm/RouterProvider.js.map +1 -0
  137. package/dist/esm/SafeFragment.d.ts +1 -0
  138. package/dist/esm/SafeFragment.js +8 -0
  139. package/dist/esm/SafeFragment.js.map +1 -0
  140. package/dist/esm/ScriptOnce.d.ts +5 -0
  141. package/dist/esm/ScriptOnce.js +28 -0
  142. package/dist/esm/ScriptOnce.js.map +1 -0
  143. package/dist/esm/Scripts.d.ts +1 -0
  144. package/dist/esm/Scripts.js +51 -0
  145. package/dist/esm/Scripts.js.map +1 -0
  146. package/dist/esm/ScrollRestoration.d.ts +14 -0
  147. package/dist/esm/ScrollRestoration.js +39 -0
  148. package/dist/esm/ScrollRestoration.js.map +1 -0
  149. package/dist/esm/Transitioner.d.ts +1 -0
  150. package/dist/esm/Transitioner.js +98 -0
  151. package/dist/esm/Transitioner.js.map +1 -0
  152. package/dist/esm/awaited.d.ts +10 -0
  153. package/dist/esm/awaited.js +31 -0
  154. package/dist/esm/awaited.js.map +1 -0
  155. package/dist/esm/fileRoute.d.ts +54 -0
  156. package/dist/esm/fileRoute.js +107 -0
  157. package/dist/esm/fileRoute.js.map +1 -0
  158. package/dist/esm/history.d.ts +8 -0
  159. package/dist/esm/index.d.ts +55 -0
  160. package/dist/esm/index.js +151 -0
  161. package/dist/esm/index.js.map +1 -0
  162. package/dist/esm/lazyRouteComponent.d.ts +2 -0
  163. package/dist/esm/lazyRouteComponent.js +61 -0
  164. package/dist/esm/lazyRouteComponent.js.map +1 -0
  165. package/dist/esm/link.d.ts +48 -0
  166. package/dist/esm/link.js +307 -0
  167. package/dist/esm/link.js.map +1 -0
  168. package/dist/esm/matchContext.d.ts +3 -0
  169. package/dist/esm/matchContext.js +10 -0
  170. package/dist/esm/matchContext.js.map +1 -0
  171. package/dist/esm/not-found.d.ts +9 -0
  172. package/dist/esm/not-found.js +40 -0
  173. package/dist/esm/not-found.js.map +1 -0
  174. package/dist/esm/renderRouteNotFound.d.ts +2 -0
  175. package/dist/esm/renderRouteNotFound.js +22 -0
  176. package/dist/esm/renderRouteNotFound.js.map +1 -0
  177. package/dist/esm/route.d.ts +95 -0
  178. package/dist/esm/route.js +205 -0
  179. package/dist/esm/route.js.map +1 -0
  180. package/dist/esm/router.d.ts +72 -0
  181. package/dist/esm/router.js +22 -0
  182. package/dist/esm/router.js.map +1 -0
  183. package/dist/esm/routerContext.d.ts +8 -0
  184. package/dist/esm/routerContext.js +16 -0
  185. package/dist/esm/routerContext.js.map +1 -0
  186. package/dist/esm/scroll-restoration.d.ts +1 -0
  187. package/dist/esm/scroll-restoration.js +24 -0
  188. package/dist/esm/scroll-restoration.js.map +1 -0
  189. package/dist/esm/serializer.d.ts +6 -0
  190. package/dist/esm/structuralSharing.d.ts +8 -0
  191. package/dist/esm/typePrimitives.d.ts +16 -0
  192. package/dist/esm/useBlocker.d.ts +66 -0
  193. package/dist/esm/useBlocker.js +148 -0
  194. package/dist/esm/useBlocker.js.map +1 -0
  195. package/dist/esm/useCanGoBack.d.ts +1 -0
  196. package/dist/esm/useCanGoBack.js +8 -0
  197. package/dist/esm/useCanGoBack.js.map +1 -0
  198. package/dist/esm/useLoaderData.d.ts +8 -0
  199. package/dist/esm/useLoaderData.js +15 -0
  200. package/dist/esm/useLoaderData.js.map +1 -0
  201. package/dist/esm/useLoaderDeps.d.ts +8 -0
  202. package/dist/esm/useLoaderDeps.js +14 -0
  203. package/dist/esm/useLoaderDeps.js.map +1 -0
  204. package/dist/esm/useLocation.d.ts +7 -0
  205. package/dist/esm/useLocation.js +10 -0
  206. package/dist/esm/useLocation.js.map +1 -0
  207. package/dist/esm/useMatch.d.ts +10 -0
  208. package/dist/esm/useMatch.js +30 -0
  209. package/dist/esm/useMatch.js.map +1 -0
  210. package/dist/esm/useNavigate.d.ts +5 -0
  211. package/dist/esm/useNavigate.js +38 -0
  212. package/dist/esm/useNavigate.js.map +1 -0
  213. package/dist/esm/useParams.d.ts +9 -0
  214. package/dist/esm/useParams.js +16 -0
  215. package/dist/esm/useParams.js.map +1 -0
  216. package/dist/esm/useRouteContext.d.ts +3 -0
  217. package/dist/esm/useRouteContext.js +11 -0
  218. package/dist/esm/useRouteContext.js.map +1 -0
  219. package/dist/esm/useRouter.d.ts +4 -0
  220. package/dist/esm/useRouter.js +15 -0
  221. package/dist/esm/useRouter.js.map +1 -0
  222. package/dist/esm/useRouterState.d.ts +8 -0
  223. package/dist/esm/useRouterState.js +29 -0
  224. package/dist/esm/useRouterState.js.map +1 -0
  225. package/dist/esm/useSearch.d.ts +9 -0
  226. package/dist/esm/useSearch.js +16 -0
  227. package/dist/esm/useSearch.js.map +1 -0
  228. package/dist/esm/utils.d.ts +49 -0
  229. package/dist/esm/utils.js +55 -0
  230. package/dist/esm/utils.js.map +1 -0
  231. package/dist/llms/index.d.ts +3 -0
  232. package/dist/llms/index.js +35 -0
  233. package/dist/llms/rules/api.d.ts +2 -0
  234. package/dist/llms/rules/api.js +4241 -0
  235. package/dist/llms/rules/guide.d.ts +2 -0
  236. package/dist/llms/rules/guide.js +6227 -0
  237. package/dist/llms/rules/routing.d.ts +2 -0
  238. package/dist/llms/rules/routing.js +1946 -0
  239. package/dist/llms/rules/setup-and-architecture.d.ts +2 -0
  240. package/dist/llms/rules/setup-and-architecture.js +932 -0
  241. package/package.json +3 -3
  242. package/src/Asset.tsx +95 -16
@@ -0,0 +1,1946 @@
1
+ export default `# Code-Based Routing
2
+
3
+ > [!TIP]
4
+ > Code-based routing is not recommended for most applications. It is recommended to use [File-Based Routing](../file-based-routing.md) instead.
5
+
6
+ ## ⚠️ Before You Start
7
+
8
+ - If you're using [File-Based Routing](../file-based-routing.md), **skip this guide**.
9
+ - If you still insist on using code-based routing, you must read the [Routing Concepts](../routing-concepts.md) guide first, as it also covers core concepts of the router.
10
+
11
+ ## Route Trees
12
+
13
+ Code-based routing is no different from file-based routing in that it uses the same route tree concept to organize, match and compose matching routes into a component tree. The only difference is that instead of using the filesystem to organize your routes, you use code.
14
+
15
+ Let's consider the same route tree from the [Route Trees & Nesting](../route-trees.md#route-trees) guide, and convert it to code-based routing:
16
+
17
+ Here is the file-based version:
18
+
19
+ \`\`\`
20
+ routes/
21
+ ├── __root.tsx
22
+ ├── index.tsx
23
+ ├── about.tsx
24
+ ├── posts/
25
+ │ ├── index.tsx
26
+ │ ├── $postId.tsx
27
+ ├── posts.$postId.edit.tsx
28
+ ├── settings/
29
+ │ ├── profile.tsx
30
+ │ ├── notifications.tsx
31
+ ├── _pathlessLayout.tsx
32
+ ├── _pathlessLayout/
33
+ │ ├── route-a.tsx
34
+ ├── ├── route-b.tsx
35
+ ├── files/
36
+ │ ├── $.tsx
37
+ \`\`\`
38
+
39
+ And here is a summarized code-based version:
40
+
41
+ \`\`\`tsx
42
+ import { createRootRoute, createRoute } from '@tanstack/react-router'
43
+
44
+ const rootRoute = createRootRoute()
45
+
46
+ const indexRoute = createRoute({
47
+ getParentRoute: () => rootRoute,
48
+ path: '/',
49
+ })
50
+
51
+ const aboutRoute = createRoute({
52
+ getParentRoute: () => rootRoute,
53
+ path: 'about',
54
+ })
55
+
56
+ const postsRoute = createRoute({
57
+ getParentRoute: () => rootRoute,
58
+ path: 'posts',
59
+ })
60
+
61
+ const postsIndexRoute = createRoute({
62
+ getParentRoute: () => postsRoute,
63
+ path: '/',
64
+ })
65
+
66
+ const postRoute = createRoute({
67
+ getParentRoute: () => postsRoute,
68
+ path: '$postId',
69
+ })
70
+
71
+ const postEditorRoute = createRoute({
72
+ getParentRoute: () => rootRoute,
73
+ path: 'posts/$postId/edit',
74
+ })
75
+
76
+ const settingsRoute = createRoute({
77
+ getParentRoute: () => rootRoute,
78
+ path: 'settings',
79
+ })
80
+
81
+ const profileRoute = createRoute({
82
+ getParentRoute: () => settingsRoute,
83
+ path: 'profile',
84
+ })
85
+
86
+ const notificationsRoute = createRoute({
87
+ getParentRoute: () => settingsRoute,
88
+ path: 'notifications',
89
+ })
90
+
91
+ const pathlessLayoutRoute = createRoute({
92
+ getParentRoute: () => rootRoute,
93
+ id: 'pathlessLayout',
94
+ })
95
+
96
+ const pathlessLayoutARoute = createRoute({
97
+ getParentRoute: () => pathlessLayoutRoute,
98
+ path: 'route-a',
99
+ })
100
+
101
+ const pathlessLayoutBRoute = createRoute({
102
+ getParentRoute: () => pathlessLayoutRoute,
103
+ path: 'route-b',
104
+ })
105
+
106
+ const filesRoute = createRoute({
107
+ getParentRoute: () => rootRoute,
108
+ path: 'files/$',
109
+ })
110
+ \`\`\`
111
+
112
+ ## Anatomy of a Route
113
+
114
+ All other routes other than the root route are configured using the \`createRoute\` function:
115
+
116
+ \`\`\`tsx
117
+ const route = createRoute({
118
+ getParentRoute: () => rootRoute,
119
+ path: '/posts',
120
+ component: PostsComponent,
121
+ })
122
+ \`\`\`
123
+
124
+ The \`getParentRoute\` option is a function that returns the parent route of the route you're creating.
125
+
126
+ **❓❓❓ "Wait, you're making me pass the parent route for every route I make?"**
127
+
128
+ Absolutely! The reason for passing the parent route has **everything to do with the magical type safety** of TanStack Router. Without the parent route, TypeScript would have no idea what types to supply your route with!
129
+
130
+ > [!IMPORTANT]
131
+ > For every route that **NOT** the **Root Route** or a **Pathless Layout Route**, a \`path\` option is required. This is the path that will be matched against the URL pathname to determine if the route is a match.
132
+
133
+ When configuring route \`path\` option on a route, it ignores leading and trailing slashes (this does not include "index" route paths \`/\`). You can include them if you want, but they will be normalized internally by TanStack Router. Here is a table of valid paths and what they will be normalized to:
134
+
135
+ | Path | Normalized Path |
136
+ | -------- | --------------- |
137
+ | \`/\` | \`/\` |
138
+ | \`/about\` | \`about\` |
139
+ | \`about/\` | \`about\` |
140
+ | \`about\` | \`about\` |
141
+ | \`$\` | \`$\` |
142
+ | \`/$\` | \`$\` |
143
+ | \`/$/\` | \`$\` |
144
+
145
+ ## Manually building the route tree
146
+
147
+ When building a route tree in code, it's not enough to define the parent route of each route. You must also construct the final route tree by adding each route to its parent route's \`children\` array. This is because the route tree is not built automatically for you like it is in file-based routing.
148
+
149
+ \`\`\`tsx
150
+ /* prettier-ignore */
151
+ const routeTree = rootRoute.addChildren([
152
+ indexRoute,
153
+ aboutRoute,
154
+ postsRoute.addChildren([
155
+ postsIndexRoute,
156
+ postRoute,
157
+ ]),
158
+ postEditorRoute,
159
+ settingsRoute.addChildren([
160
+ profileRoute,
161
+ notificationsRoute,
162
+ ]),
163
+ pathlessLayoutRoute.addChildren([
164
+ pathlessLayoutARoute,
165
+ pathlessLayoutBRoute,
166
+ ]),
167
+ filesRoute.addChildren([
168
+ fileRoute,
169
+ ]),
170
+ ])
171
+ /* prettier-ignore-end */
172
+ \`\`\`
173
+
174
+ But before you can go ahead and build the route tree, you need to understand how the Routing Concepts for Code-Based Routing work.
175
+
176
+ ## Routing Concepts for Code-Based Routing
177
+
178
+ Believe it or not, file-based routing is really a superset of code-based routing and uses the filesystem and a bit of code-generation abstraction on top of it to generate this structure you see above automatically.
179
+
180
+ We're going to assume you've read the [Routing Concepts](../routing-concepts.md) guide and are familiar with each of these main concepts:
181
+
182
+ - The Root Route
183
+ - Basic Routes
184
+ - Index Routes
185
+ - Dynamic Route Segments
186
+ - Splat / Catch-All Routes
187
+ - Layout Routes
188
+ - Pathless Routes
189
+ - Non-Nested Routes
190
+
191
+ Now, let's take a look at how to create each of these route types in code.
192
+
193
+ ## The Root Route
194
+
195
+ Creating a root route in code-based routing is thankfully the same as doing so in file-based routing. Call the \`createRootRoute()\` function.
196
+
197
+ Unlike file-based routing however, you do not need to export the root route if you don't want to. It's certainly not recommended to build an entire route tree and application in a single file (although you can and we do this in the examples to demonstrate routing concepts in brevity).
198
+
199
+ \`\`\`tsx
200
+ // Standard root route
201
+ import { createRootRoute } from '@tanstack/react-router'
202
+
203
+ const rootRoute = createRootRoute()
204
+
205
+ // Root route with Context
206
+ import { createRootRouteWithContext } from '@tanstack/react-router'
207
+ import type { QueryClient } from '@tanstack/react-query'
208
+
209
+ export interface MyRouterContext {
210
+ queryClient: QueryClient
211
+ }
212
+ const rootRoute = createRootRouteWithContext<MyRouterContext>()
213
+ \`\`\`
214
+
215
+ To learn more about Context in TanStack Router, see the [Router Context](../../guide/router-context.md) guide.
216
+
217
+ ## Basic Routes
218
+
219
+ To create a basic route, simply provide a normal \`path\` string to the \`createRoute\` function:
220
+
221
+ \`\`\`tsx
222
+ const aboutRoute = createRoute({
223
+ getParentRoute: () => rootRoute,
224
+ path: 'about',
225
+ })
226
+ \`\`\`
227
+
228
+ See, it's that simple! The \`aboutRoute\` will match the URL \`/about\`.
229
+
230
+ ## Index Routes
231
+
232
+ Unlike file-based routing, which uses the \`index\` filename to denote an index route, code-based routing uses a single slash \`/\` to denote an index route. For example, the \`posts.index.tsx\` file from our example route tree above would be represented in code-based routing like this:
233
+
234
+ \`\`\`tsx
235
+ const postsRoute = createRoute({
236
+ getParentRoute: () => rootRoute,
237
+ path: 'posts',
238
+ })
239
+
240
+ const postsIndexRoute = createRoute({
241
+ getParentRoute: () => postsRoute,
242
+ // Notice the single slash \`/\` here
243
+ path: '/',
244
+ })
245
+ \`\`\`
246
+
247
+ So, the \`postsIndexRoute\` will match the URL \`/posts/\` (or \`/posts\`).
248
+
249
+ ## Dynamic Route Segments
250
+
251
+ Dynamic route segments work exactly the same in code-based routing as they do in file-based routing. Simply prefix a segment of the path with a \`$\` and it will be captured into the \`params\` object of the route's \`loader\` or \`component\`:
252
+
253
+ \`\`\`tsx
254
+ const postIdRoute = createRoute({
255
+ getParentRoute: () => postsRoute,
256
+ path: '$postId',
257
+ // In a loader
258
+ loader: ({ params }) => fetchPost(params.postId),
259
+ // Or in a component
260
+ component: PostComponent,
261
+ })
262
+
263
+ function PostComponent() {
264
+ const { postId } = postIdRoute.useParams()
265
+ return <div>Post ID: {postId}</div>
266
+ }
267
+ \`\`\`
268
+
269
+ > [!TIP]
270
+ > If your component is code-split, you can use the [getRouteApi function](../../guide/code-splitting.md#manually-accessing-route-apis-in-other-files-with-the-getrouteapi-helper) to avoid having to import the \`postIdRoute\` configuration to get access to the typed \`useParams()\` hook.
271
+
272
+ ## Splat / Catch-All Routes
273
+
274
+ As expected, splat/catch-all routes also work the same in code-based routing as they do in file-based routing. Simply prefix a segment of the path with a \`$\` and it will be captured into the \`params\` object under the \`_splat\` key:
275
+
276
+ \`\`\`tsx
277
+ const filesRoute = createRoute({
278
+ getParentRoute: () => rootRoute,
279
+ path: 'files',
280
+ })
281
+
282
+ const fileRoute = createRoute({
283
+ getParentRoute: () => filesRoute,
284
+ path: '$',
285
+ })
286
+ \`\`\`
287
+
288
+ For the URL \`/documents/hello-world\`, the \`params\` object will look like this:
289
+
290
+ \`\`\`js
291
+ {
292
+ '_splat': 'documents/hello-world'
293
+ }
294
+ \`\`\`
295
+
296
+ ## Layout Routes
297
+
298
+ Layout routes are routes that wrap their children in a layout component. In code-based routing, you can create a layout route by simply nesting a route under another route:
299
+
300
+ \`\`\`tsx
301
+ const postsRoute = createRoute({
302
+ getParentRoute: () => rootRoute,
303
+ path: 'posts',
304
+ component: PostsLayoutComponent, // The layout component
305
+ })
306
+
307
+ function PostsLayoutComponent() {
308
+ return (
309
+ <div>
310
+ <h1>Posts</h1>
311
+ <Outlet />
312
+ </div>
313
+ )
314
+ }
315
+
316
+ const postsIndexRoute = createRoute({
317
+ getParentRoute: () => postsRoute,
318
+ path: '/',
319
+ })
320
+
321
+ const postsCreateRoute = createRoute({
322
+ getParentRoute: () => postsRoute,
323
+ path: 'create',
324
+ })
325
+
326
+ const routeTree = rootRoute.addChildren([
327
+ // The postsRoute is the layout route
328
+ // Its children will be nested under the PostsLayoutComponent
329
+ postsRoute.addChildren([postsIndexRoute, postsCreateRoute]),
330
+ ])
331
+ \`\`\`
332
+
333
+ Now, both the \`postsIndexRoute\` and \`postsCreateRoute\` will render their contents inside of the \`PostsLayoutComponent\`:
334
+
335
+ \`\`\`tsx
336
+ // URL: /posts
337
+ <PostsLayoutComponent>
338
+ <PostsIndexComponent />
339
+ </PostsLayoutComponent>
340
+
341
+ // URL: /posts/create
342
+ <PostsLayoutComponent>
343
+ <PostsCreateComponent />
344
+ </PostsLayoutComponent>
345
+ \`\`\`
346
+
347
+ ## Pathless Layout Routes
348
+
349
+ In file-based routing a pathless layout route is prefixed with a \`_\`, but in code-based routing, this is simply a route with an \`id\` instead of a \`path\` option. This is because code-based routing does not use the filesystem to organize routes, so there is no need to prefix a route with a \`_\` to denote that it has no path.
350
+
351
+ \`\`\`tsx
352
+ const pathlessLayoutRoute = createRoute({
353
+ getParentRoute: () => rootRoute,
354
+ id: 'pathlessLayout',
355
+ component: PathlessLayoutComponent,
356
+ })
357
+
358
+ function PathlessLayoutComponent() {
359
+ return (
360
+ <div>
361
+ <h1>Pathless Layout</h1>
362
+ <Outlet />
363
+ </div>
364
+ )
365
+ }
366
+
367
+ const pathlessLayoutARoute = createRoute({
368
+ getParentRoute: () => pathlessLayoutRoute,
369
+ path: 'route-a',
370
+ })
371
+
372
+ const pathlessLayoutBRoute = createRoute({
373
+ getParentRoute: () => pathlessLayoutRoute,
374
+ path: 'route-b',
375
+ })
376
+
377
+ const routeTree = rootRoute.addChildren([
378
+ // The pathless layout route has no path, only an id
379
+ // So its children will be nested under the pathless layout route
380
+ pathlessLayoutRoute.addChildren([pathlessLayoutARoute, pathlessLayoutBRoute]),
381
+ ])
382
+ \`\`\`
383
+
384
+ Now both \`/route-a\` and \`/route-b\` will render their contents inside of the \`PathlessLayoutComponent\`:
385
+
386
+ \`\`\`tsx
387
+ // URL: /route-a
388
+ <PathlessLayoutComponent>
389
+ <RouteAComponent />
390
+ </PathlessLayoutComponent>
391
+
392
+ // URL: /route-b
393
+ <PathlessLayoutComponent>
394
+ <RouteBComponent />
395
+ </PathlessLayoutComponent>
396
+ \`\`\`
397
+
398
+ ## Non-Nested Routes
399
+
400
+ Building non-nested routes in code-based routing does not require using a trailing \`_\` in the path, but does require you to build your route and route tree with the right paths and nesting. Let's consider the route tree where we want the post editor to **not** be nested under the posts route:
401
+
402
+ - \`/posts_/$postId/edit\`
403
+ - \`/posts\`
404
+ - \`$postId\`
405
+
406
+ To do this we need to build a separate route for the post editor and include the entire path in the \`path\` option from the root of where we want the route to be nested (in this case, the root):
407
+
408
+ \`\`\`tsx
409
+ // The posts editor route is nested under the root route
410
+ const postEditorRoute = createRoute({
411
+ getParentRoute: () => rootRoute,
412
+ // The path includes the entire path we need to match
413
+ path: 'posts/$postId/edit',
414
+ })
415
+
416
+ const postsRoute = createRoute({
417
+ getParentRoute: () => rootRoute,
418
+ path: 'posts',
419
+ })
420
+
421
+ const postRoute = createRoute({
422
+ getParentRoute: () => postsRoute,
423
+ path: '$postId',
424
+ })
425
+
426
+ const routeTree = rootRoute.addChildren([
427
+ // The post editor route is nested under the root route
428
+ postEditorRoute,
429
+ postsRoute.addChildren([postRoute]),
430
+ ])
431
+ \`\`\`
432
+
433
+ # File-Based Routing
434
+
435
+ Most of the TanStack Router documentation is written for file-based routing and is intended to help you understand in more detail how to configure file-based routing and the technical details behind how it works. While file-based routing is the preferred and recommended way to configure TanStack Router, you can also use [code-based routing](../code-based-routing.md) if you prefer.
436
+
437
+ ## What is File-Based Routing?
438
+
439
+ File-based routing is a way to configure your routes using the filesystem. Instead of defining your route structure via code, you can define your routes using a series of files and directories that represent the route hierarchy of your application. This brings a number of benefits:
440
+
441
+ - **Simplicity**: File-based routing is visually intuitive and easy to understand for both new and experienced developers.
442
+ - **Organization**: Routes are organized in a way that mirrors the URL structure of your application.
443
+ - **Scalability**: As your application grows, file-based routing makes it easy to add new routes and maintain existing ones.
444
+ - **Code-Splitting**: File-based routing allows TanStack Router to automatically code-split your routes for better performance.
445
+ - **Type-Safety**: File-based routing raises the ceiling on type-safety by generating managing type linkages for your routes, which can otherwise be a tedious process via code-based routing.
446
+ - **Consistency**: File-based routing enforces a consistent structure for your routes, making it easier to maintain and update your application and move from one project to another.
447
+
448
+ ## \`/\`s or \`.\`s?
449
+
450
+ While directories have long been used to represent route hierarchy, file-based routing introduces an additional concept of using the \`.\` character in the file-name to denote a route nesting. This allows you to avoid creating directories for few deeply nested routes and continue to use directories for wider route hierarchies. Let's take a look at some examples!
451
+
452
+ ## Directory Routes
453
+
454
+ Directories can be used to denote route hierarchy, which can be useful for organizing multiple routes into logical groups and also cutting down on the filename length for large groups of deeply nested routes.
455
+
456
+ See the example below:
457
+
458
+ | Filename | Route Path | Component Output |
459
+ | ----------------------- | ------------------------- | --------------------------------- |
460
+ | ʦ \`__root.tsx\` | | \`<Root>\` |
461
+ | ʦ \`index.tsx\` | \`/\` (exact) | \`<Root><RootIndex>\` |
462
+ | ʦ \`about.tsx\` | \`/about\` | \`<Root><About>\` |
463
+ | ʦ \`posts.tsx\` | \`/posts\` | \`<Root><Posts>\` |
464
+ | 📂 \`posts\` | | |
465
+ | ┄ ʦ \`index.tsx\` | \`/posts\` (exact) | \`<Root><Posts><PostsIndex>\` |
466
+ | ┄ ʦ \`$postId.tsx\` | \`/posts/$postId\` | \`<Root><Posts><Post>\` |
467
+ | 📂 \`posts_\` | | |
468
+ | ┄ 📂 \`$postId\` | | |
469
+ | ┄ ┄ ʦ \`edit.tsx\` | \`/posts/$postId/edit\` | \`<Root><EditPost>\` |
470
+ | ʦ \`settings.tsx\` | \`/settings\` | \`<Root><Settings>\` |
471
+ | 📂 \`settings\` | | \`<Root><Settings>\` |
472
+ | ┄ ʦ \`profile.tsx\` | \`/settings/profile\` | \`<Root><Settings><Profile>\` |
473
+ | ┄ ʦ \`notifications.tsx\` | \`/settings/notifications\` | \`<Root><Settings><Notifications>\` |
474
+ | ʦ \`_pathlessLayout.tsx\` | | \`<Root><PathlessLayout>\` |
475
+ | 📂 \`_pathlessLayout\` | | |
476
+ | ┄ ʦ \`route-a.tsx\` | \`/route-a\` | \`<Root><PathlessLayout><RouteA>\` |
477
+ | ┄ ʦ \`route-b.tsx\` | \`/route-b\` | \`<Root><PathlessLayout><RouteB>\` |
478
+ | 📂 \`files\` | | |
479
+ | ┄ ʦ \`$.tsx\` | \`/files/$\` | \`<Root><Files>\` |
480
+ | 📂 \`account\` | | |
481
+ | ┄ ʦ \`route.tsx\` | \`/account\` | \`<Root><Account>\` |
482
+ | ┄ ʦ \`overview.tsx\` | \`/account/overview\` | \`<Root><Account><Overview>\` |
483
+
484
+ ## Flat Routes
485
+
486
+ Flat routing gives you the ability to use \`.\`s to denote route nesting levels.
487
+
488
+ This can be useful when you have a large number of uniquely deeply nested routes and want to avoid creating directories for each one:
489
+
490
+ See the example below:
491
+
492
+ | Filename | Route Path | Component Output |
493
+ | ------------------------------- | ------------------------- | --------------------------------- |
494
+ | ʦ \`__root.tsx\` | | \`<Root>\` |
495
+ | ʦ \`index.tsx\` | \`/\` (exact) | \`<Root><RootIndex>\` |
496
+ | ʦ \`about.tsx\` | \`/about\` | \`<Root><About>\` |
497
+ | ʦ \`posts.tsx\` | \`/posts\` | \`<Root><Posts>\` |
498
+ | ʦ \`posts.index.tsx\` | \`/posts\` (exact) | \`<Root><Posts><PostsIndex>\` |
499
+ | ʦ \`posts.$postId.tsx\` | \`/posts/$postId\` | \`<Root><Posts><Post>\` |
500
+ | ʦ \`posts_.$postId.edit.tsx\` | \`/posts/$postId/edit\` | \`<Root><EditPost>\` |
501
+ | ʦ \`settings.tsx\` | \`/settings\` | \`<Root><Settings>\` |
502
+ | ʦ \`settings.profile.tsx\` | \`/settings/profile\` | \`<Root><Settings><Profile>\` |
503
+ | ʦ \`settings.notifications.tsx\` | \`/settings/notifications\` | \`<Root><Settings><Notifications>\` |
504
+ | ʦ \`_pathlessLayout.tsx\` | | \`<Root><PathlessLayout>\` |
505
+ | ʦ \`_pathlessLayout.route-a.tsx\` | \`/route-a\` | \`<Root><PathlessLayout><RouteA>\` |
506
+ | ʦ \`_pathlessLayout.route-b.tsx\` | \`/route-b\` | \`<Root><PathlessLayout><RouteB>\` |
507
+ | ʦ \`files.$.tsx\` | \`/files/$\` | \`<Root><Files>\` |
508
+ | ʦ \`account.tsx\` | \`/account\` | \`<Root><Account>\` |
509
+ | ʦ \`account.overview.tsx\` | \`/account/overview\` | \`<Root><Account><Overview>\` |
510
+
511
+ ## Mixed Flat and Directory Routes
512
+
513
+ It's extremely likely that a 100% directory or flat route structure won't be the best fit for your project, which is why TanStack Router allows you to mix both flat and directory routes together to create a route tree that uses the best of both worlds where it makes sense:
514
+
515
+ See the example below:
516
+
517
+ | Filename | Route Path | Component Output |
518
+ | ------------------------------ | ------------------------- | --------------------------------- |
519
+ | ʦ \`__root.tsx\` | | \`<Root>\` |
520
+ | ʦ \`index.tsx\` | \`/\` (exact) | \`<Root><RootIndex>\` |
521
+ | ʦ \`about.tsx\` | \`/about\` | \`<Root><About>\` |
522
+ | ʦ \`posts.tsx\` | \`/posts\` | \`<Root><Posts>\` |
523
+ | 📂 \`posts\` | | |
524
+ | ┄ ʦ \`index.tsx\` | \`/posts\` (exact) | \`<Root><Posts><PostsIndex>\` |
525
+ | ┄ ʦ \`$postId.tsx\` | \`/posts/$postId\` | \`<Root><Posts><Post>\` |
526
+ | ┄ ʦ \`$postId.edit.tsx\` | \`/posts/$postId/edit\` | \`<Root><Posts><Post><EditPost>\` |
527
+ | ʦ \`settings.tsx\` | \`/settings\` | \`<Root><Settings>\` |
528
+ | ʦ \`settings.profile.tsx\` | \`/settings/profile\` | \`<Root><Settings><Profile>\` |
529
+ | ʦ \`settings.notifications.tsx\` | \`/settings/notifications\` | \`<Root><Settings><Notifications>\` |
530
+ | ʦ \`account.tsx\` | \`/account\` | \`<Root><Account>\` |
531
+ | ʦ \`account.overview.tsx\` | \`/account/overview\` | \`<Root><Account><Overview>\` |
532
+
533
+ Both flat and directory routes can be mixed together to create a route tree that uses the best of both worlds where it makes sense.
534
+
535
+ > [!TIP]
536
+ > If you find that the default file-based routing structure doesn't fit your needs, you can always use [Virtual File Routes](../virtual-file-routes.md) to control the source of your routes whilst still getting the awesome performance benefits of file-based routing.
537
+
538
+ ## Getting started with File-Based Routing
539
+
540
+ To get started with file-based routing, you'll need to configure your project's bundler to use the TanStack Router Plugin or the TanStack Router CLI.
541
+
542
+ To enable file-based routing, you'll need to be using React with a supported bundler. See if your bundler is listed in the configuration guides below.
543
+
544
+ [//]: # 'SupportedBundlersList'
545
+
546
+ - [Installation with Vite](../installation-with-vite.md)
547
+ - [Installation with Rspack/Rsbuild](../installation-with-rspack.md)
548
+ - [Installation with Webpack](../installation-with-webpack.md)
549
+ - [Installation with Esbuild](../installation-with-esbuild.md)
550
+
551
+ [//]: # 'SupportedBundlersList'
552
+
553
+ When using TanStack Router's file-based routing through one of the supported bundlers, our plugin will **automatically generate your route configuration through your bundler's dev and build processes**. It is the easiest way to use TanStack Router's route generation features.
554
+
555
+ If your bundler is not yet supported, you can reach out to us on Discord or GitHub to let us know. Till then, fear not! You can still use the [\`@tanstack/router-cli\`](../installation-with-router-cli.md) package to generate your route tree file.
556
+
557
+ # File Naming Conventions
558
+
559
+ File-based routing requires that you follow a few simple file naming conventions to ensure that your routes are generated correctly. The concepts these conventions enable are covered in detail in the [Route Trees & Nesting](../route-trees.md) guide.
560
+
561
+ | Feature | Description |
562
+ | ---------------------------------- | ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
563
+ | **\`__root.tsx\`** | The root route file must be named \`__root.tsx\` and must be placed in the root of the configured \`routesDirectory\`. |
564
+ | **\`.\` Separator** | Routes can use the \`.\` character to denote a nested route. For example, \`blog.post\` will be generated as a child of \`blog\`. |
565
+ | **\`$\` Token** | Route segments with the \`$\` token are parameterized and will extract the value from the URL pathname as a route \`param\`. |
566
+ | **\`_\` Prefix** | Route segments with the \`_\` prefix are considered to be pathless layout routes and will not be used when matching its child routes against the URL pathname. |
567
+ | **\`_\` Suffix** | Route segments with the \`_\` suffix exclude the route from being nested under any parent routes. |
568
+ | **\`-\` Prefix** | Files and folders with the \`-\` prefix are excluded from the route tree. They will not be added to the \`routeTree.gen.ts\` file and can be used to colocate logic in route folders. |
569
+ | **\`(folder)\` folder name pattern** | A folder that matches this pattern is treated as a **route group**, preventing the folder from being included in the route's URL path. |
570
+ | **\`index\` Token** | Route segments ending with the \`index\` token (before any file extensions) will match the parent route when the URL pathname matches the parent route exactly. This can be configured via the \`indexToken\` configuration option, see [options](../../../../api/file-based-routing.md#indextoken). |
571
+ | **\`.route.tsx\` File Type** | When using directories to organise routes, the \`route\` suffix can be used to create a route file at the directory's path. For example, \`blog.post.route.tsx\` or \`blog/post/route.tsx\` can be used as the route file for the \`/blog/post\` route. This can be configured via the \`routeToken\` configuration option, see [options](../../../../api/file-based-routing.md#routetoken). |
572
+
573
+ > **💡 Remember:** The file-naming conventions for your project could be affected by what [options](../../../../api/file-based-routing.md) are configured.
574
+
575
+ ## Dynamic Path Params
576
+
577
+ Dynamic path params can be used in both flat and directory routes to create routes that can match a dynamic segment of the URL path. Dynamic path params are denoted by the \`$\` character in the filename:
578
+
579
+ | Filename | Route Path | Component Output |
580
+ | --------------------- | ---------------- | --------------------- |
581
+ | ... | ... | ... |
582
+ | ʦ \`posts.$postId.tsx\` | \`/posts/$postId\` | \`<Root><Posts><Post>\` |
583
+
584
+ We'll learn more about dynamic path params in the [Path Params](../../guide/path-params.md) guide.
585
+
586
+ ## Pathless Routes
587
+
588
+ Pathless routes wrap child routes with either logic or a component without requiring a URL path. Non-path routes are denoted by the \`_\` character in the filename:
589
+
590
+ | Filename | Route Path | Component Output |
591
+ | -------------- | ---------- | ---------------- |
592
+ | ʦ \`_app.tsx\` | | |
593
+ | ʦ \`_app.a.tsx\` | /a | \`<Root><App><A>\` |
594
+ | ʦ \`_app.b.tsx\` | /b | \`<Root><App><B>\` |
595
+
596
+ To learn more about pathless routes, see the [Routing Concepts - Pathless Routes](../routing-concepts.md#pathless-layout-routes) guide.
597
+
598
+ # Installation with Vite
599
+
600
+ [//]: # 'BundlerConfiguration'
601
+
602
+ To use file-based routing with **Esbuild**, you'll need to install the \`@tanstack/router-plugin\` package.
603
+
604
+ \`\`\`sh
605
+ npm install -D @tanstack/router-plugin
606
+ \`\`\`
607
+
608
+ Once installed, you'll need to add the plugin to your configuration.
609
+
610
+ \`\`\`tsx
611
+ // esbuild.config.js
612
+ import { tanstackRouter } from '@tanstack/router-plugin/esbuild'
613
+
614
+ export default {
615
+ // ...
616
+ plugins: [
617
+ tanstackRouter({
618
+ target: 'react',
619
+ autoCodeSplitting: true,
620
+ }),
621
+ ],
622
+ }
623
+ \`\`\`
624
+
625
+ Or, you can clone our [Quickstart Esbuild example](https://github.com/TanStack/router/tree/main/examples/react/quickstart-esbuild-file-based) and get started.
626
+
627
+ Now that you've added the plugin to your Esbuild configuration, you're all set to start using file-based routing with TanStack Router.
628
+
629
+ [//]: # 'BundlerConfiguration'
630
+
631
+ ## Ignoring the generated route tree file
632
+
633
+ If your project is configured to use a linter and/or formatter, you may want to ignore the generated route tree file. This file is managed by TanStack Router and therefore shouldn't be changed by your linter or formatter.
634
+
635
+ Here are some resources to help you ignore the generated route tree file:
636
+
637
+ - Prettier - [https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore](https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore)
638
+ - ESLint - [https://eslint.org/docs/latest/use/configure/ignore#ignoring-files](https://eslint.org/docs/latest/use/configure/ignore#ignoring-files)
639
+ - Biome - [https://biomejs.dev/reference/configuration/#filesignore](https://biomejs.dev/reference/configuration/#filesignore)
640
+
641
+ > [!WARNING]
642
+ > If you are using VSCode, you may experience the route tree file unexpectedly open (with errors) after renaming a route.
643
+
644
+ You can prevent that from the VSCode settings by marking the file as readonly. Our recommendation is to also exclude it from search results and file watcher with the following settings:
645
+
646
+ \`\`\`json
647
+ {
648
+ "files.readonlyInclude": {
649
+ "**/routeTree.gen.ts": true
650
+ },
651
+ "files.watcherExclude": {
652
+ "**/routeTree.gen.ts": true
653
+ },
654
+ "search.exclude": {
655
+ "**/routeTree.gen.ts": true
656
+ }
657
+ }
658
+ \`\`\`
659
+
660
+ You can use those settings either at a user level or only for a single workspace by creating the file \`.vscode/settings.json\` at the root of your project.
661
+
662
+ ## Configuration
663
+
664
+ When using the TanStack Router Plugin with Esbuild for File-based routing, it comes with some sane defaults that should work for most projects:
665
+
666
+ \`\`\`json
667
+ {
668
+ "routesDirectory": "./src/routes",
669
+ "generatedRouteTree": "./src/routeTree.gen.ts",
670
+ "routeFileIgnorePrefix": "-",
671
+ "quoteStyle": "single"
672
+ }
673
+ \`\`\`
674
+
675
+ If these defaults work for your project, you don't need to configure anything at all! However, if you need to customize the configuration, you can do so by editing the configuration object passed into the \`tanstackRouter\` function.
676
+
677
+ You can find all the available configuration options in the [File-based Routing API Reference](../../../../api/file-based-routing.md).
678
+
679
+ # Installation with Router CLI
680
+
681
+ > [!WARNING]
682
+ > You should only use the TanStack Router CLI if you are not using a supported bundler. The CLI only supports the generation of the route tree file and does not provide any other features.
683
+
684
+ To use file-based routing with the TanStack Router CLI, you'll need to install the \`@tanstack/router-cli\` package.
685
+
686
+ \`\`\`sh
687
+ npm install -D @tanstack/router-cli
688
+ \`\`\`
689
+
690
+ Once installed, you'll need to amend your your scripts in your \`package.json\` for the CLI to \`watch\` and \`generate\` files.
691
+
692
+ \`\`\`json
693
+ {
694
+ "scripts": {
695
+ "generate-routes": "tsr generate",
696
+ "watch-routes": "tsr watch",
697
+ "build": "npm run generate-routes && ...",
698
+ "dev": "npm run watch-routes && ..."
699
+ }
700
+ }
701
+ \`\`\`
702
+
703
+ [//]: # 'AfterScripts'
704
+ [//]: # 'AfterScripts'
705
+
706
+ You shouldn't forget to _ignore_ the generated route tree file. Head over to the [Ignoring the generated route tree file](#ignoring-the-generated-route-tree-file) section to learn more.
707
+
708
+ With the CLI installed, the following commands are made available via the \`tsr\` command
709
+
710
+ ## Using the \`generate\` command
711
+
712
+ Generates the routes for a project based on the provided configuration.
713
+
714
+ \`\`\`sh
715
+ tsr generate
716
+ \`\`\`
717
+
718
+ ## Using the \`watch\` command
719
+
720
+ Continuously watches the specified directories and regenerates routes as needed.
721
+
722
+ **Usage:**
723
+
724
+ \`\`\`sh
725
+ tsr watch
726
+ \`\`\`
727
+
728
+ With file-based routing enabled, whenever you start your application in development mode, TanStack Router will watch your configured \`routesDirectory\` and generate your route tree whenever a file is added, removed, or changed.
729
+
730
+ ## Ignoring the generated route tree file
731
+
732
+ If your project is configured to use a linter and/or formatter, you may want to ignore the generated route tree file. This file is managed by TanStack Router and therefore shouldn't be changed by your linter or formatter.
733
+
734
+ Here are some resources to help you ignore the generated route tree file:
735
+
736
+ - Prettier - [https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore](https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore)
737
+ - ESLint - [https://eslint.org/docs/latest/use/configure/ignore#ignoring-files](https://eslint.org/docs/latest/use/configure/ignore#ignoring-files)
738
+ - Biome - [https://biomejs.dev/reference/configuration/#filesignore](https://biomejs.dev/reference/configuration/#filesignore)
739
+
740
+ > [!WARNING]
741
+ > If you are using VSCode, you may experience the route tree file unexpectedly open (with errors) after renaming a route.
742
+
743
+ You can prevent that from the VSCode settings by marking the file as readonly. Our recommendation is to also exclude it from search results and file watcher with the following settings:
744
+
745
+ \`\`\`json
746
+ {
747
+ "files.readonlyInclude": {
748
+ "**/routeTree.gen.ts": true
749
+ },
750
+ "files.watcherExclude": {
751
+ "**/routeTree.gen.ts": true
752
+ },
753
+ "search.exclude": {
754
+ "**/routeTree.gen.ts": true
755
+ }
756
+ }
757
+ \`\`\`
758
+
759
+ You can use those settings either at a user level or only for a single workspace by creating the file \`.vscode/settings.json\` at the root of your project.
760
+
761
+ ## Configuration
762
+
763
+ When using the TanStack Router CLI for File-based routing, it comes with some sane defaults that should work for most projects:
764
+
765
+ \`\`\`json
766
+ {
767
+ "routesDirectory": "./src/routes",
768
+ "generatedRouteTree": "./src/routeTree.gen.ts",
769
+ "routeFileIgnorePrefix": "-",
770
+ "quoteStyle": "single"
771
+ }
772
+ \`\`\`
773
+
774
+ If these defaults work for your project, you don't need to configure anything at all! However, if you need to customize the configuration, you can do so by creating a \`tsr.config.json\` file in the root of your project directory.
775
+
776
+ [//]: # 'TargetConfiguration'
777
+ [//]: # 'TargetConfiguration'
778
+
779
+ You can find all the available configuration options in the [File-based Routing API Reference](../../../../api/file-based-routing.md).
780
+
781
+ # Installation with Rspack
782
+
783
+ [//]: # 'BundlerConfiguration'
784
+
785
+ To use file-based routing with **Rspack** or **Rsbuild**, you'll need to install the \`@tanstack/router-plugin\` package.
786
+
787
+ \`\`\`sh
788
+ npm install -D @tanstack/router-plugin
789
+ \`\`\`
790
+
791
+ Once installed, you'll need to add the plugin to your configuration.
792
+
793
+ \`\`\`tsx
794
+ // rsbuild.config.ts
795
+ import { defineConfig } from '@rsbuild/core'
796
+ import { pluginReact } from '@rsbuild/plugin-react'
797
+ import { tanstackRouter } from '@tanstack/router-plugin/rspack'
798
+
799
+ export default defineConfig({
800
+ plugins: [pluginReact()],
801
+ tools: {
802
+ rspack: {
803
+ plugins: [
804
+ tanstackRouter({
805
+ target: 'react',
806
+ autoCodeSplitting: true,
807
+ }),
808
+ ],
809
+ },
810
+ },
811
+ })
812
+ \`\`\`
813
+
814
+ Or, you can clone our [Quickstart Rspack/Rsbuild example](https://github.com/TanStack/router/tree/main/examples/react/quickstart-rspack-file-based) and get started.
815
+
816
+ Now that you've added the plugin to your Rspack/Rsbuild configuration, you're all set to start using file-based routing with TanStack Router.
817
+
818
+ [//]: # 'BundlerConfiguration'
819
+
820
+ ## Ignoring the generated route tree file
821
+
822
+ If your project is configured to use a linter and/or formatter, you may want to ignore the generated route tree file. This file is managed by TanStack Router and therefore shouldn't be changed by your linter or formatter.
823
+
824
+ Here are some resources to help you ignore the generated route tree file:
825
+
826
+ - Prettier - [https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore](https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore)
827
+ - ESLint - [https://eslint.org/docs/latest/use/configure/ignore#ignoring-files](https://eslint.org/docs/latest/use/configure/ignore#ignoring-files)
828
+ - Biome - [https://biomejs.dev/reference/configuration/#filesignore](https://biomejs.dev/reference/configuration/#filesignore)
829
+
830
+ > [!WARNING]
831
+ > If you are using VSCode, you may experience the route tree file unexpectedly open (with errors) after renaming a route.
832
+
833
+ You can prevent that from the VSCode settings by marking the file as readonly. Our recommendation is to also exclude it from search results and file watcher with the following settings:
834
+
835
+ \`\`\`json
836
+ {
837
+ "files.readonlyInclude": {
838
+ "**/routeTree.gen.ts": true
839
+ },
840
+ "files.watcherExclude": {
841
+ "**/routeTree.gen.ts": true
842
+ },
843
+ "search.exclude": {
844
+ "**/routeTree.gen.ts": true
845
+ }
846
+ }
847
+ \`\`\`
848
+
849
+ You can use those settings either at a user level or only for a single workspace by creating the file \`.vscode/settings.json\` at the root of your project.
850
+
851
+ ## Configuration
852
+
853
+ When using the TanStack Router Plugin with Rspack (or Rsbuild) for File-based routing, it comes with some sane defaults that should work for most projects:
854
+
855
+ \`\`\`json
856
+ {
857
+ "routesDirectory": "./src/routes",
858
+ "generatedRouteTree": "./src/routeTree.gen.ts",
859
+ "routeFileIgnorePrefix": "-",
860
+ "quoteStyle": "single"
861
+ }
862
+ \`\`\`
863
+
864
+ If these defaults work for your project, you don't need to configure anything at all! However, if you need to customize the configuration, you can do so by editing the configuration object passed into the \`tanstackRouter\` function.
865
+
866
+ You can find all the available configuration options in the [File-based Routing API Reference](../../../../api/file-based-routing.md).
867
+
868
+ # Installation with Vite
869
+
870
+ [//]: # 'BundlerConfiguration'
871
+
872
+ To use file-based routing with **Vite**, you'll need to install the \`@tanstack/router-plugin\` package.
873
+
874
+ \`\`\`sh
875
+ npm install -D @tanstack/router-plugin
876
+ \`\`\`
877
+
878
+ Once installed, you'll need to add the plugin to your Vite configuration.
879
+
880
+ \`\`\`ts
881
+ // vite.config.ts
882
+ import { defineConfig } from 'vite'
883
+ import react from '@vitejs/plugin-react'
884
+ import { tanstackRouter } from '@tanstack/router-plugin/vite'
885
+
886
+ // https://vitejs.dev/config/
887
+ export default defineConfig({
888
+ plugins: [
889
+ // Please make sure that '@tanstack/router-plugin' is passed before '@vitejs/plugin-react'
890
+ tanstackRouter({
891
+ target: 'react',
892
+ autoCodeSplitting: true,
893
+ }),
894
+ react(),
895
+ // ...
896
+ ],
897
+ })
898
+ \`\`\`
899
+
900
+ Or, you can clone our [Quickstart Vite example](https://github.com/TanStack/router/tree/main/examples/react/quickstart-file-based) and get started.
901
+
902
+ > [!WARNING]
903
+ > If you are using the older \`@tanstack/router-vite-plugin\` package, you can still continue to use it, as it will be aliased to the \`@tanstack/router-plugin/vite\` package. However, we would recommend using the \`@tanstack/router-plugin\` package directly.
904
+
905
+ Now that you've added the plugin to your Vite configuration, you're all set to start using file-based routing with TanStack Router.
906
+
907
+ [//]: # 'BundlerConfiguration'
908
+
909
+ ## Ignoring the generated route tree file
910
+
911
+ If your project is configured to use a linter and/or formatter, you may want to ignore the generated route tree file. This file is managed by TanStack Router and therefore shouldn't be changed by your linter or formatter.
912
+
913
+ Here are some resources to help you ignore the generated route tree file:
914
+
915
+ - Prettier - [https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore](https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore)
916
+ - ESLint - [https://eslint.org/docs/latest/use/configure/ignore#ignoring-files](https://eslint.org/docs/latest/use/configure/ignore#ignoring-files)
917
+ - Biome - [https://biomejs.dev/reference/configuration/#filesignore](https://biomejs.dev/reference/configuration/#filesignore)
918
+
919
+ > [!WARNING]
920
+ > If you are using VSCode, you may experience the route tree file unexpectedly open (with errors) after renaming a route.
921
+
922
+ You can prevent that from the VSCode settings by marking the file as readonly. Our recommendation is to also exclude it from search results and file watcher with the following settings:
923
+
924
+ \`\`\`json
925
+ {
926
+ "files.readonlyInclude": {
927
+ "**/routeTree.gen.ts": true
928
+ },
929
+ "files.watcherExclude": {
930
+ "**/routeTree.gen.ts": true
931
+ },
932
+ "search.exclude": {
933
+ "**/routeTree.gen.ts": true
934
+ }
935
+ }
936
+ \`\`\`
937
+
938
+ You can use those settings either at a user level or only for a single workspace by creating the file \`.vscode/settings.json\` at the root of your project.
939
+
940
+ ## Configuration
941
+
942
+ When using the TanStack Router Plugin with Vite for File-based routing, it comes with some sane defaults that should work for most projects:
943
+
944
+ \`\`\`json
945
+ {
946
+ "routesDirectory": "./src/routes",
947
+ "generatedRouteTree": "./src/routeTree.gen.ts",
948
+ "routeFileIgnorePrefix": "-",
949
+ "quoteStyle": "single"
950
+ }
951
+ \`\`\`
952
+
953
+ If these defaults work for your project, you don't need to configure anything at all! However, if you need to customize the configuration, you can do so by editing the configuration object passed into the \`tanstackRouter\` function.
954
+
955
+ You can find all the available configuration options in the [File-based Routing API Reference](../../../../api/file-based-routing.md).
956
+
957
+ # Installation with Webpack
958
+
959
+ [//]: # 'BundlerConfiguration'
960
+
961
+ To use file-based routing with **Webpack**, you'll need to install the \`@tanstack/router-plugin\` package.
962
+
963
+ \`\`\`sh
964
+ npm install -D @tanstack/router-plugin
965
+ \`\`\`
966
+
967
+ Once installed, you'll need to add the plugin to your configuration.
968
+
969
+ \`\`\`tsx
970
+ // webpack.config.ts
971
+ import { tanstackRouter } from '@tanstack/router-plugin/webpack'
972
+
973
+ export default {
974
+ plugins: [
975
+ tanstackRouter({
976
+ target: 'react',
977
+ autoCodeSplitting: true,
978
+ }),
979
+ ],
980
+ }
981
+ \`\`\`
982
+
983
+ Or, you can clone our [Quickstart Webpack example](https://github.com/TanStack/router/tree/main/examples/react/quickstart-webpack-file-based) and get started.
984
+
985
+ Now that you've added the plugin to your Webpack configuration, you're all set to start using file-based routing with TanStack Router.
986
+
987
+ [//]: # 'BundlerConfiguration'
988
+
989
+ ## Ignoring the generated route tree file
990
+
991
+ If your project is configured to use a linter and/or formatter, you may want to ignore the generated route tree file. This file is managed by TanStack Router and therefore shouldn't be changed by your linter or formatter.
992
+
993
+ Here are some resources to help you ignore the generated route tree file:
994
+
995
+ - Prettier - [https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore](https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore)
996
+ - ESLint - [https://eslint.org/docs/latest/use/configure/ignore#ignoring-files](https://eslint.org/docs/latest/use/configure/ignore#ignoring-files)
997
+ - Biome - [https://biomejs.dev/reference/configuration/#filesignore](https://biomejs.dev/reference/configuration/#filesignore)
998
+
999
+ > [!WARNING]
1000
+ > If you are using VSCode, you may experience the route tree file unexpectedly open (with errors) after renaming a route.
1001
+
1002
+ You can prevent that from the VSCode settings by marking the file as readonly. Our recommendation is to also exclude it from search results and file watcher with the following settings:
1003
+
1004
+ \`\`\`json
1005
+ {
1006
+ "files.readonlyInclude": {
1007
+ "**/routeTree.gen.ts": true
1008
+ },
1009
+ "files.watcherExclude": {
1010
+ "**/routeTree.gen.ts": true
1011
+ },
1012
+ "search.exclude": {
1013
+ "**/routeTree.gen.ts": true
1014
+ }
1015
+ }
1016
+ \`\`\`
1017
+
1018
+ You can use those settings either at a user level or only for a single workspace by creating the file \`.vscode/settings.json\` at the root of your project.
1019
+
1020
+ ## Configuration
1021
+
1022
+ When using the TanStack Router Plugin with Webpack for File-based routing, it comes with some sane defaults that should work for most projects:
1023
+
1024
+ \`\`\`json
1025
+ {
1026
+ "routesDirectory": "./src/routes",
1027
+ "generatedRouteTree": "./src/routeTree.gen.ts",
1028
+ "routeFileIgnorePrefix": "-",
1029
+ "quoteStyle": "single"
1030
+ }
1031
+ \`\`\`
1032
+
1033
+ If these defaults work for your project, you don't need to configure anything at all! However, if you need to customize the configuration, you can do so by editing the configuration object passed into the \`tanstackRouter\` function.
1034
+
1035
+ You can find all the available configuration options in the [File-based Routing API Reference](../../../../api/file-based-routing.md).
1036
+
1037
+ # Route Matching
1038
+
1039
+ Route matching follows a consistent and predictable pattern. This guide will explain how route trees are matched.
1040
+
1041
+ When TanStack Router processes your route tree, all of your routes are automatically sorted to match the most specific routes first. This means that regardless of the order your route tree is defined, routes will always be sorted in this order:
1042
+
1043
+ - Index Route
1044
+ - Static Routes (most specific to least specific)
1045
+ - Dynamic Routes (longest to shortest)
1046
+ - Splat/Wildcard Routes
1047
+
1048
+ Consider the following pseudo route tree:
1049
+
1050
+ \`\`\`
1051
+ Root
1052
+ - blog
1053
+ - $postId
1054
+ - /
1055
+ - new
1056
+ - /
1057
+ - *
1058
+ - about
1059
+ - about/us
1060
+ \`\`\`
1061
+
1062
+ After sorting, this route tree will become:
1063
+
1064
+ \`\`\`
1065
+ Root
1066
+ - /
1067
+ - about/us
1068
+ - about
1069
+ - blog
1070
+ - /
1071
+ - new
1072
+ - $postId
1073
+ - *
1074
+ \`\`\`
1075
+
1076
+ This final order represents the order in which routes will be matched based on specificity.
1077
+
1078
+ Using that route tree, let's follow the matching process for a few different URLs:
1079
+
1080
+ - \`/blog\`
1081
+ \`\`\`
1082
+ Root
1083
+ ❌ /
1084
+ ❌ about/us
1085
+ ❌ about
1086
+ ⏩ blog
1087
+ ✅ /
1088
+ - new
1089
+ - $postId
1090
+ - *
1091
+ \`\`\`
1092
+ - \`/blog/my-post\`
1093
+ \`\`\`
1094
+ Root
1095
+ ❌ /
1096
+ ❌ about/us
1097
+ ❌ about
1098
+ ⏩ blog
1099
+ ❌ /
1100
+ ❌ new
1101
+ ✅ $postId
1102
+ - *
1103
+ \`\`\`
1104
+ - \`/\`
1105
+ \`\`\`
1106
+ Root
1107
+ ✅ /
1108
+ - about/us
1109
+ - about
1110
+ - blog
1111
+ - /
1112
+ - new
1113
+ - $postId
1114
+ - *
1115
+ \`\`\`
1116
+ - \`/not-a-route\`
1117
+ \`\`\`
1118
+ Root
1119
+ ❌ /
1120
+ ❌ about/us
1121
+ ❌ about
1122
+ ❌ blog
1123
+ - /
1124
+ - new
1125
+ - $postId
1126
+ ✅ *
1127
+ \`\`\`
1128
+
1129
+ # Route Trees
1130
+
1131
+ TanStack Router uses a nested route tree to match up the URL with the correct component tree to render.
1132
+
1133
+ To build a route tree, TanStack Router supports:
1134
+
1135
+ - [File-Based Routing](../file-based-routing.md)
1136
+ - [Code-Based Routing](../code-based-routing.md)
1137
+
1138
+ Both methods support the exact same core features and functionality, but **file-based routing requires less code for the same or better results**. For this reason, **file-based routing is the preferred and recommended way** to configure TanStack Router. Most of the documentation is written from the perspective of file-based routing.
1139
+
1140
+ ## Route Trees
1141
+
1142
+ Nested routing is a powerful concept that allows you to use a URL to render a nested component tree. For example, given the URL of \`/blog/posts/123\`, you could create a route hierarchy that looks like this:
1143
+
1144
+ \`\`\`tsx
1145
+ ├── blog
1146
+ │ ├── posts
1147
+ │ │ ├── $postId
1148
+ \`\`\`
1149
+
1150
+ And render a component tree that looks like this:
1151
+
1152
+ \`\`\`tsx
1153
+ <Blog>
1154
+ <Posts>
1155
+ <Post postId="123" />
1156
+ </Posts>
1157
+ </Blog>
1158
+ \`\`\`
1159
+
1160
+ Let's take that concept and expand it out to a larger site structure, but with file-names now:
1161
+
1162
+ \`\`\`
1163
+ /routes
1164
+ ├── __root.tsx
1165
+ ├── index.tsx
1166
+ ├── about.tsx
1167
+ ├── posts/
1168
+ │ ├── index.tsx
1169
+ │ ├── $postId.tsx
1170
+ ├── posts.$postId.edit.tsx
1171
+ ├── settings/
1172
+ │ ├── profile.tsx
1173
+ │ ├── notifications.tsx
1174
+ ├── _pathlessLayout/
1175
+ │ ├── route-a.tsx
1176
+ ├── ├── route-b.tsx
1177
+ ├── files/
1178
+ │ ├── $.tsx
1179
+ \`\`\`
1180
+
1181
+ The above is a valid route tree configuration that can be used with TanStack Router! There's a lot of power and convention to unpack with file-based routing, so let's break it down a bit.
1182
+
1183
+ ## Route Tree Configuration
1184
+
1185
+ Route trees can be configured using a few different ways:
1186
+
1187
+ - [Flat Routes](../file-based-routing.md#flat-routes)
1188
+ - [Directories](../file-based-routing.md#directory-routes)
1189
+ - [Mixed Flat Routes and Directories](../file-based-routing.md#mixed-flat-and-directory-routes)
1190
+ - [Virtual File Routes](../virtual-file-routes.md)
1191
+ - [Code-Based Routes](../code-based-routing.md)
1192
+
1193
+ Please be sure to check out the full documentation links above for each type of route tree, or just proceed to the next section to get started with file-based routing.
1194
+
1195
+ # Routing Concepts
1196
+
1197
+ TanStack Router supports a number of powerful routing concepts that allow you to build complex and dynamic routing systems with ease.
1198
+
1199
+ Each of these concepts is useful and powerful, and we'll dive into each of them in the following sections.
1200
+
1201
+ ## Anatomy of a Route
1202
+
1203
+ All other routes, other than the [Root Route](#the-root-route), are configured using the \`createFileRoute\` function, which provides type safety when using file-based routing:
1204
+
1205
+ \`\`\`tsx
1206
+ import { createFileRoute } from '@tanstack/react-router'
1207
+
1208
+ export const Route = createFileRoute({
1209
+ component: PostsComponent,
1210
+ })
1211
+ \`\`\`
1212
+
1213
+ The \`createFileRoute\` function takes a single argument, the file-route's path as a string.
1214
+
1215
+ **❓❓❓ "Wait, you're making me pass the path of the route file to \`createFileRoute\`?"**
1216
+
1217
+ Yes! But don't worry, this path is **automatically written and managed by the router for you via the TanStack Router Bundler Plugin or Router CLI.** So, as you create new routes, move routes around or rename routes, the path will be updated for you automatically.
1218
+
1219
+ The reason for this pathname has everything to do with the magical type safety of TanStack Router. Without this pathname, TypeScript would have no idea what file we're in! (We wish TypeScript had a built-in for this, but they don't yet 🤷‍♂️)
1220
+
1221
+ ## The Root Route
1222
+
1223
+ The root route is the top-most route in the entire tree and encapsulates all other routes as children.
1224
+
1225
+ - It has no path
1226
+ - It is **always** matched
1227
+ - Its \`component\` is **always** rendered
1228
+
1229
+ Even though it doesn't have a path, the root route has access to all of the same functionality as other routes including:
1230
+
1231
+ - components
1232
+ - loaders
1233
+ - search param validation
1234
+ - etc.
1235
+
1236
+ To create a root route, call the \`createRootRoute()\` function and export it as the \`Route\` variable in your route file:
1237
+
1238
+ \`\`\`tsx
1239
+ // Standard root route
1240
+ import { createRootRoute } from '@tanstack/react-router'
1241
+
1242
+ export const Route = createRootRoute()
1243
+
1244
+ // Root route with Context
1245
+ import { createRootRouteWithContext } from '@tanstack/react-router'
1246
+ import type { QueryClient } from '@tanstack/react-query'
1247
+
1248
+ export interface MyRouterContext {
1249
+ queryClient: QueryClient
1250
+ }
1251
+ export const Route = createRootRouteWithContext<MyRouterContext>()
1252
+ \`\`\`
1253
+
1254
+ To learn more about Context in TanStack Router, see the [Router Context](../../guide/router-context.md) guide.
1255
+
1256
+ ## Basic Routes
1257
+
1258
+ Basic routes match a specific path, for example \`/about\`, \`/settings\`, \`/settings/notifications\` are all basic routes, as they match the path exactly.
1259
+
1260
+ Let's take a look at an \`/about\` route:
1261
+
1262
+ \`\`\`tsx
1263
+ // about.tsx
1264
+ import { createFileRoute } from '@tanstack/react-router'
1265
+
1266
+ export const Route = createFileRoute({
1267
+ component: AboutComponent,
1268
+ })
1269
+
1270
+ function AboutComponent() {
1271
+ return <div>About</div>
1272
+ }
1273
+ \`\`\`
1274
+
1275
+ Basic routes are simple and straightforward. They match the path exactly and render the provided component.
1276
+
1277
+ ## Index Routes
1278
+
1279
+ Index routes specifically target their parent route when it is **matched exactly and no child route is matched**.
1280
+
1281
+ Let's take a look at an index route for a \`/posts\` URL:
1282
+
1283
+ \`\`\`tsx
1284
+ // posts.index.tsx
1285
+ import { createFileRoute } from '@tanstack/react-router'
1286
+
1287
+ // Note the trailing slash, which is used to target index routes
1288
+ export const Route = createFileRoute({
1289
+ component: PostsIndexComponent,
1290
+ })
1291
+
1292
+ function PostsIndexComponent() {
1293
+ return <div>Please select a post!</div>
1294
+ }
1295
+ \`\`\`
1296
+
1297
+ This route will be matched when the URL is \`/posts\` exactly.
1298
+
1299
+ ## Dynamic Route Segments
1300
+
1301
+ Route path segments that start with a \`$\` followed by a label are dynamic and capture that section of the URL into the \`params\` object for use in your application. For example, a pathname of \`/posts/123\` would match the \`/posts/$postId\` route, and the \`params\` object would be \`{ postId: '123' }\`.
1302
+
1303
+ These params are then usable in your route's configuration and components! Let's look at a \`posts.$postId.tsx\` route:
1304
+
1305
+ \`\`\`tsx
1306
+ import { createFileRoute } from '@tanstack/react-router'
1307
+
1308
+ export const Route = createFileRoute({
1309
+ // In a loader
1310
+ loader: ({ params }) => fetchPost(params.postId),
1311
+ // Or in a component
1312
+ component: PostComponent,
1313
+ })
1314
+
1315
+ function PostComponent() {
1316
+ // In a component!
1317
+ const { postId } = Route.useParams()
1318
+ return <div>Post ID: {postId}</div>
1319
+ }
1320
+ \`\`\`
1321
+
1322
+ > 🧠 Dynamic segments work at **each** segment of the path. For example, you could have a route with the path of \`/posts/$postId/$revisionId\` and each \`$\` segment would be captured into the \`params\` object.
1323
+
1324
+ ## Splat / Catch-All Routes
1325
+
1326
+ A route with a path of only \`$\` is called a "splat" route because it _always_ captures _any_ remaining section of the URL pathname from the \`$\` to the end. The captured pathname is then available in the \`params\` object under the special \`_splat\` property.
1327
+
1328
+ For example, a route targeting the \`files/$\` path is a splat route. If the URL pathname is \`/files/documents/hello-world\`, the \`params\` object would contain \`documents/hello-world\` under the special \`_splat\` property:
1329
+
1330
+ \`\`\`js
1331
+ {
1332
+ '_splat': 'documents/hello-world'
1333
+ }
1334
+ \`\`\`
1335
+
1336
+ > ⚠️ In v1 of the router, splat routes are also denoted with a \`*\` instead of a \`_splat\` key for backwards compatibility. This will be removed in v2.
1337
+
1338
+ > 🧠 Why use \`$\`? Thanks to tools like Remix, we know that despite \`*\`s being the most common character to represent a wildcard, they do not play nice with filenames or CLI tools, so just like them, we decided to use \`$\` instead.
1339
+
1340
+ ## Layout Routes
1341
+
1342
+ Layout routes are used to wrap child routes with additional components and logic. They are useful for:
1343
+
1344
+ - Wrapping child routes with a layout component
1345
+ - Enforcing a \`loader\` requirement before displaying any child routes
1346
+ - Validating and providing search params to child routes
1347
+ - Providing fallbacks for error components or pending elements to child routes
1348
+ - Providing shared context to all child routes
1349
+ - And more!
1350
+
1351
+ Let's take a look at an example layout route called \`app.tsx\`:
1352
+
1353
+ \`\`\`
1354
+ routes/
1355
+ ├── app.tsx
1356
+ ├── app.dashboard.tsx
1357
+ ├── app.settings.tsx
1358
+ \`\`\`
1359
+
1360
+ In the tree above, \`app.tsx\` is a layout route that wraps two child routes, \`app.dashboard.tsx\` and \`app.settings.tsx\`.
1361
+
1362
+ This tree structure is used to wrap the child routes with a layout component:
1363
+
1364
+ \`\`\`tsx
1365
+ import { Outlet, createFileRoute } from '@tanstack/react-router'
1366
+
1367
+ export const Route = createFileRoute({
1368
+ component: AppLayoutComponent,
1369
+ })
1370
+
1371
+ function AppLayoutComponent() {
1372
+ return (
1373
+ <div>
1374
+ <h1>App Layout</h1>
1375
+ <Outlet />
1376
+ </div>
1377
+ )
1378
+ }
1379
+ \`\`\`
1380
+
1381
+ The following table shows which component(s) will be rendered based on the URL:
1382
+
1383
+ | URL Path | Component |
1384
+ | ---------------- | ------------------------ |
1385
+ | \`/\` | \`<Index>\` |
1386
+ | \`/app/dashboard\` | \`<AppLayout><Dashboard>\` |
1387
+ | \`/app/settings\` | \`<AppLayout><Settings>\` |
1388
+
1389
+ Since TanStack Router supports mixed flat and directory routes, you can also express your application's routing using layout routes within directories:
1390
+
1391
+ \`\`\`
1392
+ routes/
1393
+ ├── app/
1394
+ │ ├── route.tsx
1395
+ │ ├── dashboard.tsx
1396
+ │ ├── settings.tsx
1397
+ \`\`\`
1398
+
1399
+ In this nested tree, the \`app/route.tsx\` file is a configuration for the layout route that wraps two child routes, \`app/dashboard.tsx\` and \`app/settings.tsx\`.
1400
+
1401
+ Layout Routes also let you enforce component and loader logic for Dynamic Route Segments:
1402
+
1403
+ \`\`\`
1404
+ routes/
1405
+ ├── app/users/
1406
+ │ ├── $userId/
1407
+ | | ├── route.tsx
1408
+ | | ├── index.tsx
1409
+ | | ├── edit.tsx
1410
+ \`\`\`
1411
+
1412
+ ## Pathless Layout Routes
1413
+
1414
+ Like [Layout Routes](#layout-routes), Pathless Layout Routes are used to wrap child routes with additional components and logic. However, pathless layout routes do not require a matching \`path\` in the URL and are used to wrap child routes with additional components and logic without requiring a matching \`path\` in the URL.
1415
+
1416
+ Pathless Layout Routes are prefixed with an underscore (\`_\`) to denote that they are "pathless".
1417
+
1418
+ > 🧠 The part of the path after the \`_\` prefix is used as the route's ID and is required because every route must be uniquely identifiable, especially when using TypeScript so as to avoid type errors and accomplish autocomplete effectively.
1419
+
1420
+ Let's take a look at an example route called \`_pathlessLayout.tsx\`:
1421
+
1422
+ \`\`\`
1423
+
1424
+ routes/
1425
+ ├── _pathlessLayout.tsx
1426
+ ├── _pathlessLayout.a.tsx
1427
+ ├── _pathlessLayout.b.tsx
1428
+
1429
+ \`\`\`
1430
+
1431
+ In the tree above, \`_pathlessLayout.tsx\` is a pathless layout route that wraps two child routes, \`_pathlessLayout.a.tsx\` and \`_pathlessLayout.b.tsx\`.
1432
+
1433
+ The \`_pathlessLayout.tsx\` route is used to wrap the child routes with a Pathless layout component:
1434
+
1435
+ \`\`\`tsx
1436
+ import { Outlet, createFileRoute } from '@tanstack/react-router'
1437
+
1438
+ export const Route = createFileRoute({
1439
+ component: PathlessLayoutComponent,
1440
+ })
1441
+
1442
+ function PathlessLayoutComponent() {
1443
+ return (
1444
+ <div>
1445
+ <h1>Pathless layout</h1>
1446
+ <Outlet />
1447
+ </div>
1448
+ )
1449
+ }
1450
+ \`\`\`
1451
+
1452
+ The following table shows which component will be rendered based on the URL:
1453
+
1454
+ | URL Path | Component |
1455
+ | -------- | --------------------- |
1456
+ | \`/\` | \`<Index>\` |
1457
+ | \`/a\` | \`<PathlessLayout><A>\` |
1458
+ | \`/b\` | \`<PathlessLayout><B>\` |
1459
+
1460
+ Since TanStack Router supports mixed flat and directory routes, you can also express your application's routing using pathless layout routes within directories:
1461
+
1462
+ \`\`\`
1463
+ routes/
1464
+ ├── _pathlessLayout/
1465
+ │ ├── route.tsx
1466
+ │ ├── a.tsx
1467
+ │ ├── b.tsx
1468
+ \`\`\`
1469
+
1470
+ However, unlike Layout Routes, since Pathless Layout Routes do match based on URL path segments, this means that these routes do not support [Dynamic Route Segments](#dynamic-route-segments) as part of their path and therefore cannot be matched in the URL.
1471
+
1472
+ This means that you cannot do this:
1473
+
1474
+ \`\`\`
1475
+ routes/
1476
+ ├── _$postId/ ❌
1477
+ │ ├── ...
1478
+ \`\`\`
1479
+
1480
+ Rather, you'd have to do this:
1481
+
1482
+ \`\`\`
1483
+ routes/
1484
+ ├── $postId/
1485
+ ├── _postPathlessLayout/ ✅
1486
+ │ ├── ...
1487
+ \`\`\`
1488
+
1489
+ ## Non-Nested Routes
1490
+
1491
+ Non-nested routes can be created by suffixing a parent file route segment with a \`_\` and are used to **un-nest** a route from its parents and render its own component tree.
1492
+
1493
+ Consider the following flat route tree:
1494
+
1495
+ \`\`\`
1496
+ routes/
1497
+ ├── posts.tsx
1498
+ ├── posts.$postId.tsx
1499
+ ├── posts_.$postId.edit.tsx
1500
+ \`\`\`
1501
+
1502
+ The following table shows which component will be rendered based on the URL:
1503
+
1504
+ | URL Path | Component |
1505
+ | ----------------- | ---------------------------- |
1506
+ | \`/posts\` | \`<Posts>\` |
1507
+ | \`/posts/123\` | \`<Posts><Post postId="123">\` |
1508
+ | \`/posts/123/edit\` | \`<PostEditor postId="123">\` |
1509
+
1510
+ - The \`posts.$postId.tsx\` route is nested as normal under the \`posts.tsx\` route and will render \`<Posts><Post>\`.
1511
+ - The \`posts_.$postId.edit.tsx\` route **does not share** the same \`posts\` prefix as the other routes and therefore will be treated as if it is a top-level route and will render \`<PostEditor>\`.
1512
+
1513
+ ## Excluding Files and Folders from Routes
1514
+
1515
+ Files and folders can be excluded from route generation with a \`-\` prefix attached to the file name. This gives you the ability to colocate logic in the route directories.
1516
+
1517
+ Consider the following route tree:
1518
+
1519
+ \`\`\`
1520
+ routes/
1521
+ ├── posts.tsx
1522
+ ├── -posts-table.tsx // 👈🏼 ignored
1523
+ ├── -components/ // 👈🏼 ignored
1524
+ │ ├── header.tsx // 👈🏼 ignored
1525
+ │ ├── footer.tsx // 👈🏼 ignored
1526
+ │ ├── ...
1527
+ \`\`\`
1528
+
1529
+ We can import from the excluded files into our posts route
1530
+
1531
+ \`\`\`tsx
1532
+ import { createFileRoute } from '@tanstack/react-router'
1533
+ import { PostsTable } from './-posts-table'
1534
+ import { PostsHeader } from './-components/header'
1535
+ import { PostsFooter } from './-components/footer'
1536
+
1537
+ export const Route = createFileRoute('/posts')({
1538
+ loader: () => fetchPosts(),
1539
+ component: PostComponent,
1540
+ })
1541
+
1542
+ function PostComponent() {
1543
+ const posts = Route.useLoaderData()
1544
+
1545
+ return (
1546
+ <div>
1547
+ <PostsHeader />
1548
+ <PostsTable posts={posts} />
1549
+ <PostsFooter />
1550
+ </div>
1551
+ )
1552
+ }
1553
+ \`\`\`
1554
+
1555
+ The excluded files will not be added to \`routeTree.gen.ts\`.
1556
+
1557
+ ## Pathless Route Group Directories
1558
+
1559
+ Pathless route group directories use \`()\` as a way to group routes files together regardless of their path. They are purely organizational and do not affect the route tree or component tree in any way.
1560
+
1561
+ \`\`\`
1562
+ routes/
1563
+ ├── index.tsx
1564
+ ├── (app)/
1565
+ │ ├── dashboard.tsx
1566
+ │ ├── settings.tsx
1567
+ │ ├── users.tsx
1568
+ ├── (auth)/
1569
+ │ ├── login.tsx
1570
+ │ ├── register.tsx
1571
+ \`\`\`
1572
+
1573
+ In the example above, the \`app\` and \`auth\` directories are purely organizational and do not affect the route tree or component tree in any way. They are used to group related routes together for easier navigation and organization.
1574
+
1575
+ The following table shows which component will be rendered based on the URL:
1576
+
1577
+ | URL Path | Component |
1578
+ | ------------ | ------------- |
1579
+ | \`/\` | \`<Index>\` |
1580
+ | \`/dashboard\` | \`<Dashboard>\` |
1581
+ | \`/settings\` | \`<Settings>\` |
1582
+ | \`/users\` | \`<Users>\` |
1583
+ | \`/login\` | \`<Login>\` |
1584
+ | \`/register\` | \`<Register>\` |
1585
+
1586
+ As you can see, the \`app\` and \`auth\` directories are purely organizational and do not affect the route tree or component tree in any way.
1587
+
1588
+ # Virtual File Routes
1589
+
1590
+ > We'd like to thank the Remix team for [pioneering the concept of virtual file routes](https://www.youtube.com/watch?v=fjTX8hQTlEc&t=730s). We've taken inspiration from their work and adapted it to work with TanStack Router's existing file-based route-tree generation.
1591
+
1592
+ Virtual file routes are a powerful concept that allows you to build a route tree programmatically using code that references real files in your project. This can be useful if:
1593
+
1594
+ - You have an existing route organization that you want to keep.
1595
+ - You want to customize the location of your route files.
1596
+ - You want to completely override TanStack Router's file-based route generation and build your own convention.
1597
+
1598
+ Here's a quick example of using virtual file routes to map a route tree to a set of real files in your project:
1599
+
1600
+ \`\`\`tsx
1601
+ // routes.ts
1602
+ import {
1603
+ rootRoute,
1604
+ route,
1605
+ index,
1606
+ layout,
1607
+ physical,
1608
+ } from '@tanstack/virtual-file-routes'
1609
+
1610
+ export const routes = rootRoute('root.tsx', [
1611
+ index('index.tsx'),
1612
+ layout('pathlessLayout.tsx', [
1613
+ route('/dashboard', 'app/dashboard.tsx', [
1614
+ index('app/dashboard-index.tsx'),
1615
+ route('/invoices', 'app/dashboard-invoices.tsx', [
1616
+ index('app/invoices-index.tsx'),
1617
+ route('$id', 'app/invoice-detail.tsx'),
1618
+ ]),
1619
+ ]),
1620
+ physical('/posts', 'posts'),
1621
+ ]),
1622
+ ])
1623
+ \`\`\`
1624
+
1625
+ ## Configuration
1626
+
1627
+ Virtual file routes can be configured either via:
1628
+
1629
+ - The \`TanStackRouter\` plugin for Vite/Rspack/Webpack
1630
+ - The \`tsr.config.json\` file for the TanStack Router CLI
1631
+
1632
+ ## Configuration via the TanStackRouter Plugin
1633
+
1634
+ If you're using the \`TanStackRouter\` plugin for Vite/Rspack/Webpack, you can configure virtual file routes by passing the path of your routes file to the \`virtualRoutesConfig\` option when setting up the plugin:
1635
+
1636
+ \`\`\`tsx
1637
+ // vite.config.ts
1638
+ import { defineConfig } from 'vite'
1639
+ import react from '@vitejs/plugin-react'
1640
+ import { tanstackRouter } from '@tanstack/router-plugin/vite'
1641
+
1642
+ export default defineConfig({
1643
+ plugins: [
1644
+ tanstackRouter({
1645
+ target: 'react',
1646
+ virtualRouteConfig: './routes.ts',
1647
+ }),
1648
+ react(),
1649
+ ],
1650
+ })
1651
+ \`\`\`
1652
+
1653
+ Or, you choose to define the virtual routes directly in the configuration:
1654
+
1655
+ \`\`\`tsx
1656
+ // vite.config.ts
1657
+ import { defineConfig } from 'vite'
1658
+ import react from '@vitejs/plugin-react'
1659
+ import { tanstackRouter } from '@tanstack/router-plugin/vite'
1660
+ import { rootRoute } from '@tanstack/virtual-file-routes'
1661
+
1662
+ const routes = rootRoute('root.tsx', [
1663
+ // ... the rest of your virtual route tree
1664
+ ])
1665
+
1666
+ export default defineConfig({
1667
+ plugins: [tanstackRouter({ virtualRouteConfig: routes }), react()],
1668
+ })
1669
+ \`\`\`
1670
+
1671
+ ## Creating Virtual File Routes
1672
+
1673
+ To create virtual file routes, you'll need to import the \`@tanstack/virtual-file-routes\` package. This package provides a set of functions that allow you to create virtual routes that reference real files in your project. A few utility functions are exported from the package:
1674
+
1675
+ - \`rootRoute\` - Creates a virtual root route.
1676
+ - \`route\` - Creates a virtual route.
1677
+ - \`index\` - Creates a virtual index route.
1678
+ - \`layout\` - Creates a virtual pathless layout route.
1679
+ - \`physical\` - Creates a physical virtual route (more on this later).
1680
+
1681
+ ## Virtual Root Route
1682
+
1683
+ The \`rootRoute\` function is used to create a virtual root route. It takes a file name and an array of children routes. Here's an example of a virtual root route:
1684
+
1685
+ \`\`\`tsx
1686
+ // routes.ts
1687
+ import { rootRoute } from '@tanstack/virtual-file-routes'
1688
+
1689
+ export const routes = rootRoute('root.tsx', [
1690
+ // ... children routes
1691
+ ])
1692
+ \`\`\`
1693
+
1694
+ ## Virtual Route
1695
+
1696
+ The \`route\` function is used to create a virtual route. It takes a path, a file name, and an array of children routes. Here's an example of a virtual route:
1697
+
1698
+ \`\`\`tsx
1699
+ // routes.ts
1700
+ import { route } from '@tanstack/virtual-file-routes'
1701
+
1702
+ export const routes = rootRoute('root.tsx', [
1703
+ route('/about', 'about.tsx', [
1704
+ // ... children routes
1705
+ ]),
1706
+ ])
1707
+ \`\`\`
1708
+
1709
+ You can also define a virtual route without a file name. This allows to set a common path prefix for its children:
1710
+
1711
+ \`\`\`tsx
1712
+ // routes.ts
1713
+ import { route } from '@tanstack/virtual-file-routes'
1714
+
1715
+ export const routes = rootRoute('root.tsx', [
1716
+ route('/hello', [
1717
+ route('/world', 'world.tsx'), // full path will be "/hello/world"
1718
+ route('/universe', 'universe.tsx'), // full path will be "/hello/universe"
1719
+ ]),
1720
+ ])
1721
+ \`\`\`
1722
+
1723
+ ## Virtual Index Route
1724
+
1725
+ The \`index\` function is used to create a virtual index route. It takes a file name. Here's an example of a virtual index route:
1726
+
1727
+ \`\`\`tsx
1728
+ import { index } from '@tanstack/virtual-file-routes'
1729
+
1730
+ const routes = rootRoute('root.tsx', [index('index.tsx')])
1731
+ \`\`\`
1732
+
1733
+ ## Virtual Pathless Route
1734
+
1735
+ The \`layout\` function is used to create a virtual pathless route. It takes a file name, an array of children routes, and an optional pathless ID. Here's an example of a virtual pathless route:
1736
+
1737
+ \`\`\`tsx
1738
+ // routes.ts
1739
+ import { layout } from '@tanstack/virtual-file-routes'
1740
+
1741
+ export const routes = rootRoute('root.tsx', [
1742
+ layout('pathlessLayout.tsx', [
1743
+ // ... children routes
1744
+ ]),
1745
+ ])
1746
+ \`\`\`
1747
+
1748
+ You can also specify a pathless ID to give the route a unique identifier that is different from the filename:
1749
+
1750
+ \`\`\`tsx
1751
+ // routes.ts
1752
+ import { layout } from '@tanstack/virtual-file-routes'
1753
+
1754
+ export const routes = rootRoute('root.tsx', [
1755
+ layout('my-pathless-layout-id', 'pathlessLayout.tsx', [
1756
+ // ... children routes
1757
+ ]),
1758
+ ])
1759
+ \`\`\`
1760
+
1761
+ ## Physical Virtual Routes
1762
+
1763
+ Physical virtual routes are a way to "mount" a directory of good ol' TanStack Router File Based routing convention under a specific URL path. This can be useful if you are using virtual routes to customize a small portion of your route tree high up in the hierarchy, but want to use the standard file-based routing convention for sub-routes and directories.
1764
+
1765
+ Consider the following file structure:
1766
+
1767
+ \`\`\`
1768
+ /routes
1769
+ ├── root.tsx
1770
+ ├── index.tsx
1771
+ ├── pathless.tsx
1772
+ ├── app
1773
+ │ ├── dashboard.tsx
1774
+ │ ├── dashboard-index.tsx
1775
+ │ ├── dashboard-invoices.tsx
1776
+ │ ├── invoices-index.tsx
1777
+ │ ├── invoice-detail.tsx
1778
+ └── posts
1779
+ ├── index.tsx
1780
+ ├── $postId.tsx
1781
+ ├── $postId.edit.tsx
1782
+ ├── comments/
1783
+ │ ├── index.tsx
1784
+ │ ├── $commentId.tsx
1785
+ └── likes/
1786
+ ├── index.tsx
1787
+ ├── $likeId.tsx
1788
+ \`\`\`
1789
+
1790
+ Let's use virtual routes to customize our route tree for everything but \`posts\`, then use physical virtual routes to mount the \`posts\` directory under the \`/posts\` path:
1791
+
1792
+ \`\`\`tsx
1793
+ // routes.ts
1794
+ export const routes = rootRoute('root.tsx', [
1795
+ // Set up your virtual routes as normal
1796
+ index('index.tsx'),
1797
+ layout('pathlessLayout.tsx', [
1798
+ route('/dashboard', 'app/dashboard.tsx', [
1799
+ index('app/dashboard-index.tsx'),
1800
+ route('/invoices', 'app/dashboard-invoices.tsx', [
1801
+ index('app/invoices-index.tsx'),
1802
+ route('$id', 'app/invoice-detail.tsx'),
1803
+ ]),
1804
+ ]),
1805
+ // Mount the \`posts\` directory under the \`/posts\` path
1806
+ physical('/posts', 'posts'),
1807
+ ]),
1808
+ ])
1809
+ \`\`\`
1810
+
1811
+ ## Virtual Routes inside of TanStack Router File Based routing
1812
+
1813
+ The previous section showed you how you can use TanStack Router's File Based routing convention inside of a virtual route configuration.
1814
+ However, the opposite is possible as well.
1815
+ You can configure the main part of your app's route tree using TanStack Router's File Based routing convention and opt into virtual route configuration for specific subtrees.
1816
+
1817
+ Consider the following file structure:
1818
+
1819
+ \`\`\`
1820
+ /routes
1821
+ ├── __root.tsx
1822
+ ├── foo
1823
+ │ ├── bar
1824
+ │ │ ├── __virtual.ts
1825
+ │ │ ├── details.tsx
1826
+ │ │ ├── home.tsx
1827
+ │ │ └── route.ts
1828
+ │ └── bar.tsx
1829
+ └── index.tsx
1830
+ \`\`\`
1831
+
1832
+ Let's look at the \`bar\` directory which contains a special file named \`__virtual.ts\`. This file instructs the generator to switch over to virtual file route configuration for this directory (and its child directories).
1833
+
1834
+ \`__virtual.ts\` configures the virtual routes for that particular subtree of the route tree. It uses the same API as explained above, with the only difference being that no \`rootRoute\` is defined for that subtree:
1835
+
1836
+ \`\`\`tsx
1837
+ // routes/foo/bar/__virtual.ts
1838
+ import {
1839
+ defineVirtualSubtreeConfig,
1840
+ index,
1841
+ route,
1842
+ } from '@tanstack/virtual-file-routes'
1843
+
1844
+ export default defineVirtualSubtreeConfig([
1845
+ index('home.tsx'),
1846
+ route('$id', 'details.tsx'),
1847
+ ])
1848
+ \`\`\`
1849
+
1850
+ The helper function \`defineVirtualSubtreeConfig\` is closely modeled after vite's \`defineConfig\` and allows you to define a subtree configuration via a default export. The default export can either be
1851
+
1852
+ - a subtree config object
1853
+ - a function returning a subtree config object
1854
+ - an async function returning a subtree config object
1855
+
1856
+ ## Inception
1857
+
1858
+ You can mix and match TanStack Router's File Based routing convention and virtual route configuration however you like.
1859
+ Let's go deeper!
1860
+ Check out the following example that starts off using File Based routing convention, switches over to virtual route configuration for \`/posts\`, switches back to File Based routing convention for \`/posts/lets-go\` only to switch over to virtual route configuration again for \`/posts/lets-go/deeper\`.
1861
+
1862
+ \`\`\`
1863
+ ├── __root.tsx
1864
+ ├── index.tsx
1865
+ ├── posts
1866
+ │ ├── __virtual.ts
1867
+ │ ├── details.tsx
1868
+ │ ├── home.tsx
1869
+ │ └── lets-go
1870
+ │ ├── deeper
1871
+ │ │ ├── __virtual.ts
1872
+ │ │ └── home.tsx
1873
+ │ └── index.tsx
1874
+ └── posts.tsx
1875
+ \`\`\`
1876
+
1877
+ ## Configuration via the TanStack Router CLI
1878
+
1879
+ If you're using the TanStack Router CLI, you can configure virtual file routes by defining the path to your routes file in the \`tsr.config.json\` file:
1880
+
1881
+ \`\`\`json
1882
+ // tsr.config.json
1883
+ {
1884
+ "virtualRouteConfig": "./routes.ts"
1885
+ }
1886
+ \`\`\`
1887
+
1888
+ Or you can define the virtual routes directly in the configuration, while much less common allows you to configure them via the TanStack Router CLI by adding a \`virtualRouteConfig\` object to your \`tsr.config.json\` file and defining your virtual routes and passing the resulting JSON that is generated by calling the actual \`rootRoute\`/\`route\`/\`index\`/etc functions from the \`@tanstack/virtual-file-routes\` package:
1889
+
1890
+ \`\`\`json
1891
+ // tsr.config.json
1892
+ {
1893
+ "virtualRouteConfig": {
1894
+ "type": "root",
1895
+ "file": "root.tsx",
1896
+ "children": [
1897
+ {
1898
+ "type": "index",
1899
+ "file": "home.tsx"
1900
+ },
1901
+ {
1902
+ "type": "route",
1903
+ "file": "posts/posts.tsx",
1904
+ "path": "/posts",
1905
+ "children": [
1906
+ {
1907
+ "type": "index",
1908
+ "file": "posts/posts-home.tsx"
1909
+ },
1910
+ {
1911
+ "type": "route",
1912
+ "file": "posts/posts-detail.tsx",
1913
+ "path": "$postId"
1914
+ }
1915
+ ]
1916
+ },
1917
+ {
1918
+ "type": "layout",
1919
+ "id": "first",
1920
+ "file": "layout/first-pathless-layout.tsx",
1921
+ "children": [
1922
+ {
1923
+ "type": "layout",
1924
+ "id": "second",
1925
+ "file": "layout/second-pathless-layout.tsx",
1926
+ "children": [
1927
+ {
1928
+ "type": "route",
1929
+ "file": "a.tsx",
1930
+ "path": "/route-a"
1931
+ },
1932
+ {
1933
+ "type": "route",
1934
+ "file": "b.tsx",
1935
+ "path": "/route-b"
1936
+ }
1937
+ ]
1938
+ }
1939
+ ]
1940
+ }
1941
+ ]
1942
+ }
1943
+ }
1944
+ \`\`\`
1945
+
1946
+ `;