@tanstack/react-router 1.133.3 → 1.133.8

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.
@@ -0,0 +1,968 @@
1
+ export default `# Manual Setup
2
+
3
+ To set up TanStack Router manually in a React project, follow the steps below. This gives you a bare minimum setup to get going with TanStack Router using both file-based route generation and code-based route configuration:
4
+
5
+ ## Using File-Based Route Generation
6
+
7
+ #### Install TanStack Router, Vite Plugin, and the Router Devtools
8
+
9
+ \`\`\`sh
10
+ npm install @tanstack/react-router @tanstack/react-router-devtools
11
+ npm install -D @tanstack/router-plugin
12
+ # or
13
+ pnpm add @tanstack/react-router @tanstack/react-router-devtools
14
+ pnpm add -D @tanstack/router-plugin
15
+ # or
16
+ yarn add @tanstack/react-router @tanstack/react-router-devtools
17
+ yarn add -D @tanstack/router-plugin
18
+ # or
19
+ bun add @tanstack/react-router @tanstack/react-router-devtools
20
+ bun add -D @tanstack/router-plugin
21
+ # or
22
+ deno add npm:@tanstack/react-router npm:@tanstack/router-plugin npm:@tanstack/react-router-devtools
23
+ \`\`\`
24
+
25
+ #### Configure the Vite Plugin
26
+
27
+ \`\`\`tsx
28
+ // vite.config.ts
29
+ import { defineConfig } from 'vite'
30
+ import react from '@vitejs/plugin-react'
31
+ import { tanstackRouter } from '@tanstack/router-plugin/vite'
32
+
33
+ // https://vitejs.dev/config/
34
+ export default defineConfig({
35
+ plugins: [
36
+ // Please make sure that '@tanstack/router-plugin' is passed before '@vitejs/plugin-react'
37
+ tanstackRouter({
38
+ target: 'react',
39
+ autoCodeSplitting: true,
40
+ }),
41
+ react(),
42
+ // ...,
43
+ ],
44
+ })
45
+ \`\`\`
46
+
47
+ > [!TIP]
48
+ > If you are not using Vite, or any of the supported bundlers, you can check out the [TanStack Router CLI](../with-router-cli) guide for more info.
49
+
50
+ Create the following files:
51
+
52
+ - \`src/routes/__root.tsx\` (with two '\`_\`' characters)
53
+ - \`src/routes/index.tsx\`
54
+ - \`src/routes/about.tsx\`
55
+ - \`src/main.tsx\`
56
+
57
+ #### \`src/routes/__root.tsx\`
58
+
59
+ \`\`\`tsx
60
+ import { createRootRoute, Link, Outlet } from '@tanstack/react-router'
61
+ import { TanStackRouterDevtools } from '@tanstack/react-router-devtools'
62
+
63
+ const RootLayout = () => (
64
+ <>
65
+ <div className="p-2 flex gap-2">
66
+ <Link to="/" className="[&.active]:font-bold">
67
+ Home
68
+ </Link>{' '}
69
+ <Link to="/about" className="[&.active]:font-bold">
70
+ About
71
+ </Link>
72
+ </div>
73
+ <hr />
74
+ <Outlet />
75
+ <TanStackRouterDevtools />
76
+ </>
77
+ )
78
+
79
+ export const Route = createRootRoute({ component: RootLayout })
80
+ \`\`\`
81
+
82
+ #### \`src/routes/index.tsx\`
83
+
84
+ \`\`\`tsx
85
+ import { createFileRoute } from '@tanstack/react-router'
86
+
87
+ export const Route = createFileRoute('/')({
88
+ component: Index,
89
+ })
90
+
91
+ function Index() {
92
+ return (
93
+ <div className="p-2">
94
+ <h3>Welcome Home!</h3>
95
+ </div>
96
+ )
97
+ }
98
+ \`\`\`
99
+
100
+ #### \`src/routes/about.tsx\`
101
+
102
+ \`\`\`tsx
103
+ import { createFileRoute } from '@tanstack/react-router'
104
+
105
+ export const Route = createFileRoute('/about')({
106
+ component: About,
107
+ })
108
+
109
+ function About() {
110
+ return <div className="p-2">Hello from About!</div>
111
+ }
112
+ \`\`\`
113
+
114
+ #### \`src/main.tsx\`
115
+
116
+ Regardless of whether you are using the \`@tanstack/router-plugin\` package and running the \`npm run dev\`/\`npm run build\` scripts, or manually running the \`tsr watch\`/\`tsr generate\` commands from your package scripts, the route tree file will be generated at \`src/routeTree.gen.ts\`.
117
+
118
+ Import the generated route tree and create a new router instance:
119
+
120
+ \`\`\`tsx
121
+ import { StrictMode } from 'react'
122
+ import ReactDOM from 'react-dom/client'
123
+ import { RouterProvider, createRouter } from '@tanstack/react-router'
124
+
125
+ // Import the generated route tree
126
+ import { routeTree } from './routeTree.gen'
127
+
128
+ // Create a new router instance
129
+ const router = createRouter({ routeTree })
130
+
131
+ // Register the router instance for type safety
132
+ declare module '@tanstack/react-router' {
133
+ interface Register {
134
+ router: typeof router
135
+ }
136
+ }
137
+
138
+ // Render the app
139
+ const rootElement = document.getElementById('root')!
140
+ if (!rootElement.innerHTML) {
141
+ const root = ReactDOM.createRoot(rootElement)
142
+ root.render(
143
+ <StrictMode>
144
+ <RouterProvider router={router} />
145
+ </StrictMode>,
146
+ )
147
+ }
148
+ \`\`\`
149
+
150
+ If you are working with this pattern you should change the \`id\` of the root \`<div>\` on your \`index.html\` file to \`<div id='root'></div>\`
151
+
152
+ ## Using Code-Based Route Configuration
153
+
154
+ > [!IMPORTANT]
155
+ > The following example shows how to configure routes using code, and for simplicity's sake is in a single file for this demo. While code-based generation allows you to declare many routes and even the router instance in a single file, we recommend splitting your routes into separate files for better organization and performance as your application grows.
156
+
157
+ \`\`\`tsx
158
+ import { StrictMode } from 'react'
159
+ import ReactDOM from 'react-dom/client'
160
+ import {
161
+ Outlet,
162
+ RouterProvider,
163
+ Link,
164
+ createRouter,
165
+ createRoute,
166
+ createRootRoute,
167
+ } from '@tanstack/react-router'
168
+ import { TanStackRouterDevtools } from '@tanstack/react-router-devtools'
169
+
170
+ const rootRoute = createRootRoute({
171
+ component: () => (
172
+ <>
173
+ <div className="p-2 flex gap-2">
174
+ <Link to="/" className="[&.active]:font-bold">
175
+ Home
176
+ </Link>{' '}
177
+ <Link to="/about" className="[&.active]:font-bold">
178
+ About
179
+ </Link>
180
+ </div>
181
+ <hr />
182
+ <Outlet />
183
+ <TanStackRouterDevtools />
184
+ </>
185
+ ),
186
+ })
187
+
188
+ const indexRoute = createRoute({
189
+ getParentRoute: () => rootRoute,
190
+ path: '/',
191
+ component: function Index() {
192
+ return (
193
+ <div className="p-2">
194
+ <h3>Welcome Home!</h3>
195
+ </div>
196
+ )
197
+ },
198
+ })
199
+
200
+ const aboutRoute = createRoute({
201
+ getParentRoute: () => rootRoute,
202
+ path: '/about',
203
+ component: function About() {
204
+ return <div className="p-2">Hello from About!</div>
205
+ },
206
+ })
207
+
208
+ const routeTree = rootRoute.addChildren([indexRoute, aboutRoute])
209
+
210
+ const router = createRouter({ routeTree })
211
+
212
+ declare module '@tanstack/react-router' {
213
+ interface Register {
214
+ router: typeof router
215
+ }
216
+ }
217
+
218
+ const rootElement = document.getElementById('app')!
219
+ if (!rootElement.innerHTML) {
220
+ const root = ReactDOM.createRoot(rootElement)
221
+ root.render(
222
+ <StrictMode>
223
+ <RouterProvider router={router} />
224
+ </StrictMode>,
225
+ )
226
+ }
227
+ \`\`\`
228
+
229
+ If you glossed over these examples or didn't understand something, we don't blame you, because there's so much more to learn to really take advantage of TanStack Router! Let's move on.
230
+
231
+ # Migration from React Location
232
+
233
+ Before you begin your journey in migrating from React Location, it's important that you have a good understanding of the [Routing Concepts](../routing/routing-concepts.md) and [Design Decisions](../decisions-on-dx.md) used by TanStack Router.
234
+
235
+ ## Differences between React Location and TanStack Router
236
+
237
+ React Location and TanStack Router share much of same design decisions concepts, but there are some key differences that you should be aware of.
238
+
239
+ - React Location uses _generics_ to infer types for routes, while TanStack Router uses _module declaration merging_ to infer types.
240
+ - Route configuration in React Location is done using a single array of route definitions, while in TanStack Router, route configuration is done using a tree of route definitions starting with the [root route](../routing/routing-concepts.md#the-root-route).
241
+ - [File-based routing](../routing/file-based-routing.md) is the recommended way to define routes in TanStack Router, while React Location only allows you to define routes in a single file using a code-based approach.
242
+ - TanStack Router does support a [code-based approach](../routing/code-based-routing.md) to defining routes, but it is not recommended for most use cases. You can read more about why, over here: [why is file-based routing the preferred way to define routes?](../decisions-on-dx.md#3-why-is-file-based-routing-the-preferred-way-to-define-routes)
243
+
244
+ ## Migration guide
245
+
246
+ In this guide we'll go over the process of migrating the [React Location Basic example](https://github.com/TanStack/router/tree/react-location/examples/basic) over to TanStack Router using file-based routing, with the end goal of having the same functionality as the original example (styling and other non-routing related code will be omitted).
247
+
248
+ > [!TIP]
249
+ > To use a code-based approach for defining your routes, you can read the [code-based Routing](../routing/code-based-routing.md) guide.
250
+
251
+ ### Step 1: Swap over to TanStack Router's dependencies
252
+
253
+ First, we need to install the dependencies for TanStack Router. For detailed installation instructions, see our [How to Install TanStack Router](../how-to/install.md) guide.
254
+
255
+ \`\`\`sh
256
+ npm install @tanstack/react-router @tanstack/router-devtools
257
+ \`\`\`
258
+
259
+ And remove the React Location dependencies.
260
+
261
+ \`\`\`sh
262
+ npm uninstall @tanstack/react-location @tanstack/react-location-devtools
263
+ \`\`\`
264
+
265
+ ### Step 2: Use the file-based routing watcher
266
+
267
+ If your project uses Vite (or one of the supported bundlers), you can use the TanStack Router plugin to watch for changes in your routes files and automatically update the routes configuration.
268
+
269
+ Installation of the Vite plugin:
270
+
271
+ \`\`\`sh
272
+ npm install -D @tanstack/router-plugin
273
+ \`\`\`
274
+
275
+ And add it to your \`vite.config.js\`:
276
+
277
+ \`\`\`js
278
+ import { defineConfig } from 'vite'
279
+ import react from '@vitejs/plugin-react'
280
+ import { tanstackRouter } from '@tanstack/router-plugin/vite'
281
+
282
+ export default defineConfig({
283
+ // ...
284
+ plugins: [tanstackRouter(), react()],
285
+ })
286
+ \`\`\`
287
+
288
+ However, if your application does not use Vite, you use one of our other [supported bundlers](../routing/file-based-routing.md#getting-started-with-file-based-routing), or you can use the \`@tanstack/router-cli\` package to watch for changes in your routes files and automatically update the routes configuration.
289
+
290
+ ### Step 3: Add the file-based configuration file to your project
291
+
292
+ Create a \`tsr.config.json\` file in the root of your project with the following content:
293
+
294
+ \`\`\`json
295
+ {
296
+ "routesDirectory": "./src/routes",
297
+ "generatedRouteTree": "./src/routeTree.gen.ts"
298
+ }
299
+ \`\`\`
300
+
301
+ You can find the full list of options for the \`tsr.config.json\` file [here](../../../api/file-based-routing.md).
302
+
303
+ ### Step 4: Create the routes directory
304
+
305
+ Create a \`routes\` directory in the \`src\` directory of your project.
306
+
307
+ \`\`\`sh
308
+ mkdir src/routes
309
+ \`\`\`
310
+
311
+ ### Step 5: Create the root route file
312
+
313
+ \`\`\`tsx
314
+ // src/routes/__root.tsx
315
+ import { createRootRoute, Outlet, Link } from '@tanstack/react-router'
316
+ import { TanStackRouterDevtools } from '@tanstack/router-devtools'
317
+
318
+ export const Route = createRootRoute({
319
+ component: () => {
320
+ return (
321
+ <>
322
+ <div>
323
+ <Link to="/" activeOptions={{ exact: true }}>
324
+ Home
325
+ </Link>
326
+ <Link to="/posts">Posts</Link>
327
+ </div>
328
+ <hr />
329
+ <Outlet />
330
+ <TanStackRouterDevtools />
331
+ </>
332
+ )
333
+ },
334
+ })
335
+ \`\`\`
336
+
337
+ ### Step 6: Create the index route file
338
+
339
+ \`\`\`tsx
340
+ // src/routes/index.tsx
341
+ import { createFileRoute } from '@tanstack/react-router'
342
+
343
+ export const Route = createFileRoute('/')({
344
+ component: Index,
345
+ })
346
+ \`\`\`
347
+
348
+ > You will need to move any related components and logic needed for the index route from the \`src/index.tsx\` file to the \`src/routes/index.tsx\` file.
349
+
350
+ ### Step 7: Create the posts route file
351
+
352
+ \`\`\`tsx
353
+ // src/routes/posts.tsx
354
+ import { createFileRoute, Link, Outlet } from '@tanstack/react-router'
355
+
356
+ export const Route = createFileRoute('/posts')({
357
+ component: Posts,
358
+ loader: async () => {
359
+ const posts = await fetchPosts()
360
+ return {
361
+ posts,
362
+ }
363
+ },
364
+ })
365
+
366
+ function Posts() {
367
+ const { posts } = Route.useLoaderData()
368
+ return (
369
+ <div>
370
+ <nav>
371
+ {posts.map((post) => (
372
+ <Link
373
+ key={post.id}
374
+ to={\`/posts/$postId\`}
375
+ params={{ postId: post.id }}
376
+ >
377
+ {post.title}
378
+ </Link>
379
+ ))}
380
+ </nav>
381
+ <Outlet />
382
+ </div>
383
+ )
384
+ }
385
+ \`\`\`
386
+
387
+ > You will need to move any related components and logic needed for the posts route from the \`src/index.tsx\` file to the \`src/routes/posts.tsx\` file.
388
+
389
+ ### Step 8: Create the posts index route file
390
+
391
+ \`\`\`tsx
392
+ // src/routes/posts.index.tsx
393
+ import { createFileRoute } from '@tanstack/react-router'
394
+
395
+ export const Route = createFileRoute('/posts/')({
396
+ component: PostsIndex,
397
+ })
398
+ \`\`\`
399
+
400
+ > You will need to move any related components and logic needed for the posts index route from the \`src/index.tsx\` file to the \`src/routes/posts.index.tsx\` file.
401
+
402
+ ### Step 9: Create the posts id route file
403
+
404
+ \`\`\`tsx
405
+ // src/routes/posts.$postId.tsx
406
+ import { createFileRoute } from '@tanstack/react-router'
407
+
408
+ export const Route = createFileRoute('/posts/$postId')({
409
+ component: PostsId,
410
+ loader: async ({ params: { postId } }) => {
411
+ const post = await fetchPost(postId)
412
+ return {
413
+ post,
414
+ }
415
+ },
416
+ })
417
+
418
+ function PostsId() {
419
+ const { post } = Route.useLoaderData()
420
+ // ...
421
+ }
422
+ \`\`\`
423
+
424
+ > You will need to move any related components and logic needed for the posts id route from the \`src/index.tsx\` file to the \`src/routes/posts.$postId.tsx\` file.
425
+
426
+ ### Step 10: Generate the route tree
427
+
428
+ If you are using one of the supported bundlers, the route tree will be generated automatically when you run the dev script.
429
+
430
+ If you are not using one of the supported bundlers, you can generate the route tree by running the following command:
431
+
432
+ \`\`\`sh
433
+ npx tsr generate
434
+ \`\`\`
435
+
436
+ ### Step 11: Update the main entry file to render the Router
437
+
438
+ Once you've generated the route-tree, you can then update the \`src/index.tsx\` file to create the router instance and render it.
439
+
440
+ \`\`\`tsx
441
+ // src/index.tsx
442
+ import React from 'react'
443
+ import ReactDOM from 'react-dom'
444
+ import { createRouter, RouterProvider } from '@tanstack/react-router'
445
+
446
+ // Import the generated route tree
447
+ import { routeTree } from './routeTree.gen'
448
+
449
+ // Create a new router instance
450
+ const router = createRouter({ routeTree })
451
+
452
+ // Register the router instance for type safety
453
+ declare module '@tanstack/react-router' {
454
+ interface Register {
455
+ router: typeof router
456
+ }
457
+ }
458
+
459
+ const domElementId = 'root' // Assuming you have a root element with the id 'root'
460
+
461
+ // Render the app
462
+ const rootElement = document.getElementById(domElementId)
463
+ if (!rootElement) {
464
+ throw new Error(\`Element with id \${domElementId} not found\`)
465
+ }
466
+
467
+ ReactDOM.createRoot(rootElement).render(
468
+ <React.StrictMode>
469
+ <RouterProvider router={router} />
470
+ </React.StrictMode>,
471
+ )
472
+ \`\`\`
473
+
474
+ ### Finished!
475
+
476
+ You should now have successfully migrated your application from React Location to TanStack Router using file-based routing.
477
+
478
+ React Location also has a few more features that you might be using in your application. Here are some guides to help you migrate those features:
479
+
480
+ - [Search params](../guide/search-params.md)
481
+ - [Data loading](../guide/data-loading.md)
482
+ - [History types](../guide/history-types.md)
483
+ - [Wildcard / Splat / Catch-all routes](../routing/routing-concepts.md#splat--catch-all-routes)
484
+ - [Authenticated routes](../guide/authenticated-routes.md)
485
+
486
+ TanStack Router also has a few more features that you might want to explore:
487
+
488
+ - [Router Context](../guide/router-context.md)
489
+ - [Preloading](../guide/preloading.md)
490
+ - [Pathless Layout Routes](../routing/routing-concepts.md#pathless-layout-routes)
491
+ - [Route masking](../guide/route-masking.md)
492
+ - [SSR](../guide/ssr.md)
493
+ - ... and more!
494
+
495
+ If you are facing any issues or have any questions, feel free to ask for help in the TanStack Discord.
496
+
497
+ # Migration from React Router Checklist
498
+
499
+ **_If your UI is blank, open the console, and you will probably have some errors that read something along the lines of \`cannot use 'useNavigate' outside of context\` . This means there are React Router api’s that are still imported and referenced that you need to find and remove. The easiest way to make sure you find all React Router imports is to uninstall \`react-router-dom\` and then you should get typescript errors in your files. Then you will know what to change to a \`@tanstack/react-router\` import._**
500
+
501
+ Here is the [example repo](https://github.com/Benanna2019/SickFitsForEveryone/tree/migrate-to-tanstack/router/React-Router)
502
+
503
+ - [ ] Install Router - \`npm i @tanstack/react-router\` (see [detailed installation guide](../how-to/install.md))
504
+ - [ ] **Optional:** Uninstall React Router to get TypeScript errors on imports.
505
+ - At this point I don’t know if you can do a gradual migration, but it seems likely you could have multiple router providers, not desirable.
506
+ - The api’s between React Router and TanStack Router are very similar and could most likely be handled in a sprint cycle or two if that is your companies way of doing things.
507
+ - [ ] Create Routes for each existing React Router route we have
508
+ - [ ] Create root route
509
+ - [ ] Create router instance
510
+ - [ ] Add global module in main.tsx
511
+ - [ ] Remove any React Router (\`createBrowserRouter\` or \`BrowserRouter\`), \`Routes\`, and \`Route\` Components from main.tsx
512
+ - [ ] **Optional:** Refactor \`render\` function for custom setup/providers - The repo referenced above has an example - This was necessary in the case of Supertokens. Supertoken has a specific setup with React Router and a different setup with all other React implementations
513
+ - [ ] Set RouterProvider and pass it the router as the prop
514
+ - [ ] Replace all instances of React Router \`Link\` component with \`@tanstack/react-router\` \`Link\` component
515
+ - [ ] Add \`to\` prop with literal path
516
+ - [ ] Add \`params\` prop, where necessary with params like so \`params={{ orderId: order.id }}\`
517
+ - [ ] Replace all instances of React Router \`useNavigate\` hook with \`@tanstack/react-router\` \`useNavigate\` hook
518
+ - [ ] Set \`to\` property and \`params\` property where needed
519
+ - [ ] Replace any React Router \`Outlet\`'s with the \`@tanstack/react-router\` equivalent
520
+ - [ ] If you are using \`useSearchParams\` hook from React Router, move the search params default value to the validateSearch property on a Route definition.
521
+ - [ ] Instead of using the \`useSearchParams\` hook, use \`@tanstack/react-router\` \`Link\`'s search property to update the search params state
522
+ - [ ] To read search params you can do something like the following
523
+ - \`const { page } = useSearch({ from: productPage.fullPath })\`
524
+ - [ ] If using React Router’s \`useParams\` hook, update the import to be from \`@tanstack/react-router\` and set the \`from\` property to the literal path name where you want to read the params object from
525
+ - So say we have a route with the path name \`orders/$orderid\`.
526
+ - In the \`useParams\` hook we would set up our hook like so: \`const params = useParams({ from: "/orders/$orderId" })\`
527
+ - Then wherever we wanted to access the order id we would get it off of the params object \`params.orderId\`
528
+
529
+ # Installation with Esbuild
530
+
531
+ [//]: # 'BundlerConfiguration'
532
+
533
+ To use file-based routing with **Esbuild**, you'll need to install the \`@tanstack/router-plugin\` package.
534
+
535
+ \`\`\`sh
536
+ npm install -D @tanstack/router-plugin
537
+ \`\`\`
538
+
539
+ Once installed, you'll need to add the plugin to your configuration.
540
+
541
+ \`\`\`tsx
542
+ // esbuild.config.js
543
+ import { tanstackRouter } from '@tanstack/router-plugin/esbuild'
544
+
545
+ export default {
546
+ // ...
547
+ plugins: [
548
+ tanstackRouter({
549
+ target: 'react',
550
+ autoCodeSplitting: true,
551
+ }),
552
+ ],
553
+ }
554
+ \`\`\`
555
+
556
+ Or, you can clone our [Quickstart Esbuild example](https://github.com/TanStack/router/tree/main/examples/react/quickstart-esbuild-file-based) and get started.
557
+
558
+ Now that you've added the plugin to your Esbuild configuration, you're all set to start using file-based routing with TanStack Router.
559
+
560
+ [//]: # 'BundlerConfiguration'
561
+
562
+ ## Ignoring the generated route tree file
563
+
564
+ 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.
565
+
566
+ Here are some resources to help you ignore the generated route tree file:
567
+
568
+ - Prettier - [https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore](https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore)
569
+ - ESLint - [https://eslint.org/docs/latest/use/configure/ignore#ignoring-files](https://eslint.org/docs/latest/use/configure/ignore#ignoring-files)
570
+ - Biome - [https://biomejs.dev/reference/configuration/#filesignore](https://biomejs.dev/reference/configuration/#filesignore)
571
+
572
+ > [!WARNING]
573
+ > If you are using VSCode, you may experience the route tree file unexpectedly open (with errors) after renaming a route.
574
+
575
+ 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:
576
+
577
+ \`\`\`json
578
+ {
579
+ "files.readonlyInclude": {
580
+ "**/routeTree.gen.ts": true
581
+ },
582
+ "files.watcherExclude": {
583
+ "**/routeTree.gen.ts": true
584
+ },
585
+ "search.exclude": {
586
+ "**/routeTree.gen.ts": true
587
+ }
588
+ }
589
+ \`\`\`
590
+
591
+ 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.
592
+
593
+ ## Configuration
594
+
595
+ When using the TanStack Router Plugin with Esbuild for File-based routing, it comes with some sane defaults that should work for most projects:
596
+
597
+ \`\`\`json
598
+ {
599
+ "routesDirectory": "./src/routes",
600
+ "generatedRouteTree": "./src/routeTree.gen.ts",
601
+ "routeFileIgnorePrefix": "-",
602
+ "quoteStyle": "single"
603
+ }
604
+ \`\`\`
605
+
606
+ 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.
607
+
608
+ You can find all the available configuration options in the [File-based Routing API Reference](../../../../api/file-based-routing.md).
609
+
610
+ # Installation with Router CLI
611
+
612
+ > [!WARNING]
613
+ > 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.
614
+
615
+ To use file-based routing with the TanStack Router CLI, you'll need to install the \`@tanstack/router-cli\` package.
616
+
617
+ \`\`\`sh
618
+ npm install -D @tanstack/router-cli
619
+ \`\`\`
620
+
621
+ Once installed, you'll need to amend your scripts in your \`package.json\` for the CLI to \`watch\` and \`generate\` files.
622
+
623
+ \`\`\`json
624
+ {
625
+ "scripts": {
626
+ "generate-routes": "tsr generate",
627
+ "watch-routes": "tsr watch",
628
+ "build": "npm run generate-routes && ...",
629
+ "dev": "npm run watch-routes && ..."
630
+ }
631
+ }
632
+ \`\`\`
633
+
634
+ [//]: # 'AfterScripts'
635
+ [//]: # 'AfterScripts'
636
+
637
+ 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.
638
+
639
+ With the CLI installed, the following commands are made available via the \`tsr\` command
640
+
641
+ ## Using the \`generate\` command
642
+
643
+ Generates the routes for a project based on the provided configuration.
644
+
645
+ \`\`\`sh
646
+ tsr generate
647
+ \`\`\`
648
+
649
+ ## Using the \`watch\` command
650
+
651
+ Continuously watches the specified directories and regenerates routes as needed.
652
+
653
+ **Usage:**
654
+
655
+ \`\`\`sh
656
+ tsr watch
657
+ \`\`\`
658
+
659
+ 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.
660
+
661
+ ## Ignoring the generated route tree file
662
+
663
+ 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.
664
+
665
+ Here are some resources to help you ignore the generated route tree file:
666
+
667
+ - Prettier - [https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore](https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore)
668
+ - ESLint - [https://eslint.org/docs/latest/use/configure/ignore#ignoring-files](https://eslint.org/docs/latest/use/configure/ignore#ignoring-files)
669
+ - Biome - [https://biomejs.dev/reference/configuration/#filesignore](https://biomejs.dev/reference/configuration/#filesignore)
670
+
671
+ > [!WARNING]
672
+ > If you are using VSCode, you may experience the route tree file unexpectedly open (with errors) after renaming a route.
673
+
674
+ 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:
675
+
676
+ \`\`\`json
677
+ {
678
+ "files.readonlyInclude": {
679
+ "**/routeTree.gen.ts": true
680
+ },
681
+ "files.watcherExclude": {
682
+ "**/routeTree.gen.ts": true
683
+ },
684
+ "search.exclude": {
685
+ "**/routeTree.gen.ts": true
686
+ }
687
+ }
688
+ \`\`\`
689
+
690
+ 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.
691
+
692
+ ## Configuration
693
+
694
+ When using the TanStack Router CLI for File-based routing, it comes with some sane defaults that should work for most projects:
695
+
696
+ \`\`\`json
697
+ {
698
+ "routesDirectory": "./src/routes",
699
+ "generatedRouteTree": "./src/routeTree.gen.ts",
700
+ "routeFileIgnorePrefix": "-",
701
+ "quoteStyle": "single"
702
+ }
703
+ \`\`\`
704
+
705
+ 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.
706
+
707
+ [//]: # 'TargetConfiguration'
708
+ [//]: # 'TargetConfiguration'
709
+
710
+ You can find all the available configuration options in the [File-based Routing API Reference](../../../../api/file-based-routing.md).
711
+
712
+ # Installation with Rspack
713
+
714
+ [//]: # 'BundlerConfiguration'
715
+
716
+ To use file-based routing with **Rspack** or **Rsbuild**, you'll need to install the \`@tanstack/router-plugin\` package.
717
+
718
+ \`\`\`sh
719
+ npm install -D @tanstack/router-plugin
720
+ \`\`\`
721
+
722
+ Once installed, you'll need to add the plugin to your configuration.
723
+
724
+ \`\`\`tsx
725
+ // rsbuild.config.ts
726
+ import { defineConfig } from '@rsbuild/core'
727
+ import { pluginReact } from '@rsbuild/plugin-react'
728
+ import { tanstackRouter } from '@tanstack/router-plugin/rspack'
729
+
730
+ export default defineConfig({
731
+ plugins: [pluginReact()],
732
+ tools: {
733
+ rspack: {
734
+ plugins: [
735
+ tanstackRouter({
736
+ target: 'react',
737
+ autoCodeSplitting: true,
738
+ }),
739
+ ],
740
+ },
741
+ },
742
+ })
743
+ \`\`\`
744
+
745
+ 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.
746
+
747
+ 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.
748
+
749
+ [//]: # 'BundlerConfiguration'
750
+
751
+ ## Ignoring the generated route tree file
752
+
753
+ 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.
754
+
755
+ Here are some resources to help you ignore the generated route tree file:
756
+
757
+ - Prettier - [https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore](https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore)
758
+ - ESLint - [https://eslint.org/docs/latest/use/configure/ignore#ignoring-files](https://eslint.org/docs/latest/use/configure/ignore#ignoring-files)
759
+ - Biome - [https://biomejs.dev/reference/configuration/#filesignore](https://biomejs.dev/reference/configuration/#filesignore)
760
+
761
+ > [!WARNING]
762
+ > If you are using VSCode, you may experience the route tree file unexpectedly open (with errors) after renaming a route.
763
+
764
+ 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:
765
+
766
+ \`\`\`json
767
+ {
768
+ "files.readonlyInclude": {
769
+ "**/routeTree.gen.ts": true
770
+ },
771
+ "files.watcherExclude": {
772
+ "**/routeTree.gen.ts": true
773
+ },
774
+ "search.exclude": {
775
+ "**/routeTree.gen.ts": true
776
+ }
777
+ }
778
+ \`\`\`
779
+
780
+ 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.
781
+
782
+ ## Configuration
783
+
784
+ 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:
785
+
786
+ \`\`\`json
787
+ {
788
+ "routesDirectory": "./src/routes",
789
+ "generatedRouteTree": "./src/routeTree.gen.ts",
790
+ "routeFileIgnorePrefix": "-",
791
+ "quoteStyle": "single"
792
+ }
793
+ \`\`\`
794
+
795
+ 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.
796
+
797
+ You can find all the available configuration options in the [File-based Routing API Reference](../../../../api/file-based-routing.md).
798
+
799
+ # Installation with Vite
800
+
801
+ [//]: # 'BundlerConfiguration'
802
+
803
+ To use file-based routing with **Vite**, you'll need to install the \`@tanstack/router-plugin\` package.
804
+
805
+ \`\`\`sh
806
+ npm install -D @tanstack/router-plugin
807
+ \`\`\`
808
+
809
+ Once installed, you'll need to add the plugin to your Vite configuration.
810
+
811
+ \`\`\`ts
812
+ // vite.config.ts
813
+ import { defineConfig } from 'vite'
814
+ import react from '@vitejs/plugin-react'
815
+ import { tanstackRouter } from '@tanstack/router-plugin/vite'
816
+
817
+ // https://vitejs.dev/config/
818
+ export default defineConfig({
819
+ plugins: [
820
+ // Please make sure that '@tanstack/router-plugin' is passed before '@vitejs/plugin-react'
821
+ tanstackRouter({
822
+ target: 'react',
823
+ autoCodeSplitting: true,
824
+ }),
825
+ react(),
826
+ // ...
827
+ ],
828
+ })
829
+ \`\`\`
830
+
831
+ Or, you can clone our [Quickstart Vite example](https://github.com/TanStack/router/tree/main/examples/react/quickstart-file-based) and get started.
832
+
833
+ > [!WARNING]
834
+ > 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.
835
+
836
+ Now that you've added the plugin to your Vite configuration, you're all set to start using file-based routing with TanStack Router.
837
+
838
+ [//]: # 'BundlerConfiguration'
839
+
840
+ ## Ignoring the generated route tree file
841
+
842
+ 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.
843
+
844
+ Here are some resources to help you ignore the generated route tree file:
845
+
846
+ - Prettier - [https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore](https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore)
847
+ - ESLint - [https://eslint.org/docs/latest/use/configure/ignore#ignoring-files](https://eslint.org/docs/latest/use/configure/ignore#ignoring-files)
848
+ - Biome - [https://biomejs.dev/reference/configuration/#filesignore](https://biomejs.dev/reference/configuration/#filesignore)
849
+
850
+ > [!WARNING]
851
+ > If you are using VSCode, you may experience the route tree file unexpectedly open (with errors) after renaming a route.
852
+
853
+ 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:
854
+
855
+ \`\`\`json
856
+ {
857
+ "files.readonlyInclude": {
858
+ "**/routeTree.gen.ts": true
859
+ },
860
+ "files.watcherExclude": {
861
+ "**/routeTree.gen.ts": true
862
+ },
863
+ "search.exclude": {
864
+ "**/routeTree.gen.ts": true
865
+ }
866
+ }
867
+ \`\`\`
868
+
869
+ 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.
870
+
871
+ ## Configuration
872
+
873
+ When using the TanStack Router Plugin with Vite for File-based routing, it comes with some sane defaults that should work for most projects:
874
+
875
+ \`\`\`json
876
+ {
877
+ "routesDirectory": "./src/routes",
878
+ "generatedRouteTree": "./src/routeTree.gen.ts",
879
+ "routeFileIgnorePrefix": "-",
880
+ "quoteStyle": "single"
881
+ }
882
+ \`\`\`
883
+
884
+ 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.
885
+
886
+ You can find all the available configuration options in the [File-based Routing API Reference](../../../../api/file-based-routing.md).
887
+
888
+ # Installation with Webpack
889
+
890
+ [//]: # 'BundlerConfiguration'
891
+
892
+ To use file-based routing with **Webpack**, you'll need to install the \`@tanstack/router-plugin\` package.
893
+
894
+ \`\`\`sh
895
+ npm install -D @tanstack/router-plugin
896
+ \`\`\`
897
+
898
+ Once installed, you'll need to add the plugin to your configuration.
899
+
900
+ \`\`\`tsx
901
+ // webpack.config.ts
902
+ import { tanstackRouter } from '@tanstack/router-plugin/webpack'
903
+
904
+ export default {
905
+ plugins: [
906
+ tanstackRouter({
907
+ target: 'react',
908
+ autoCodeSplitting: true,
909
+ }),
910
+ ],
911
+ }
912
+ \`\`\`
913
+
914
+ Or, you can clone our [Quickstart Webpack example](https://github.com/TanStack/router/tree/main/examples/react/quickstart-webpack-file-based) and get started.
915
+
916
+ Now that you've added the plugin to your Webpack configuration, you're all set to start using file-based routing with TanStack Router.
917
+
918
+ [//]: # 'BundlerConfiguration'
919
+
920
+ ## Ignoring the generated route tree file
921
+
922
+ 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.
923
+
924
+ Here are some resources to help you ignore the generated route tree file:
925
+
926
+ - Prettier - [https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore](https://prettier.io/docs/en/ignore.html#ignoring-files-prettierignore)
927
+ - ESLint - [https://eslint.org/docs/latest/use/configure/ignore#ignoring-files](https://eslint.org/docs/latest/use/configure/ignore#ignoring-files)
928
+ - Biome - [https://biomejs.dev/reference/configuration/#filesignore](https://biomejs.dev/reference/configuration/#filesignore)
929
+
930
+ > [!WARNING]
931
+ > If you are using VSCode, you may experience the route tree file unexpectedly open (with errors) after renaming a route.
932
+
933
+ 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:
934
+
935
+ \`\`\`json
936
+ {
937
+ "files.readonlyInclude": {
938
+ "**/routeTree.gen.ts": true
939
+ },
940
+ "files.watcherExclude": {
941
+ "**/routeTree.gen.ts": true
942
+ },
943
+ "search.exclude": {
944
+ "**/routeTree.gen.ts": true
945
+ }
946
+ }
947
+ \`\`\`
948
+
949
+ 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.
950
+
951
+ ## Configuration
952
+
953
+ When using the TanStack Router Plugin with Webpack for File-based routing, it comes with some sane defaults that should work for most projects:
954
+
955
+ \`\`\`json
956
+ {
957
+ "routesDirectory": "./src/routes",
958
+ "generatedRouteTree": "./src/routeTree.gen.ts",
959
+ "routeFileIgnorePrefix": "-",
960
+ "quoteStyle": "single"
961
+ }
962
+ \`\`\`
963
+
964
+ 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.
965
+
966
+ You can find all the available configuration options in the [File-based Routing API Reference](../../../../api/file-based-routing.md).
967
+
968
+ `;