@tanstack/react-router 1.160.0 → 1.160.2
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.
- package/dist/cjs/Asset.cjs +33 -19
- package/dist/cjs/Asset.cjs.map +1 -1
- package/dist/esm/Asset.js +33 -19
- package/dist/esm/Asset.js.map +1 -1
- package/dist/llms/rules/guide.d.ts +1 -1
- package/dist/llms/rules/guide.js +2821 -253
- package/dist/llms/rules/installation.d.ts +1 -1
- package/dist/llms/rules/installation.js +376 -357
- package/dist/llms/rules/routing.d.ts +1 -1
- package/dist/llms/rules/routing.js +436 -31
- package/dist/llms/rules/setup-and-architecture.d.ts +1 -1
- package/dist/llms/rules/setup-and-architecture.js +253 -90
- package/package.json +1 -1
- package/src/Asset.tsx +45 -17
|
@@ -38,6 +38,10 @@ routes/
|
|
|
38
38
|
|
|
39
39
|
And here is a summarized code-based version:
|
|
40
40
|
|
|
41
|
+
<!-- ::start:framework -->
|
|
42
|
+
|
|
43
|
+
# React
|
|
44
|
+
|
|
41
45
|
\`\`\`tsx
|
|
42
46
|
import { createRootRoute, createRoute } from '@tanstack/react-router'
|
|
43
47
|
|
|
@@ -109,6 +113,81 @@ const filesRoute = createRoute({
|
|
|
109
113
|
})
|
|
110
114
|
\`\`\`
|
|
111
115
|
|
|
116
|
+
# Solid
|
|
117
|
+
|
|
118
|
+
\`\`\`tsx
|
|
119
|
+
import { createRootRoute, createRoute } from '@tanstack/solid-router'
|
|
120
|
+
|
|
121
|
+
const rootRoute = createRootRoute()
|
|
122
|
+
|
|
123
|
+
const indexRoute = createRoute({
|
|
124
|
+
getParentRoute: () => rootRoute,
|
|
125
|
+
path: '/',
|
|
126
|
+
})
|
|
127
|
+
|
|
128
|
+
const aboutRoute = createRoute({
|
|
129
|
+
getParentRoute: () => rootRoute,
|
|
130
|
+
path: 'about',
|
|
131
|
+
})
|
|
132
|
+
|
|
133
|
+
const postsRoute = createRoute({
|
|
134
|
+
getParentRoute: () => rootRoute,
|
|
135
|
+
path: 'posts',
|
|
136
|
+
})
|
|
137
|
+
|
|
138
|
+
const postsIndexRoute = createRoute({
|
|
139
|
+
getParentRoute: () => postsRoute,
|
|
140
|
+
path: '/',
|
|
141
|
+
})
|
|
142
|
+
|
|
143
|
+
const postRoute = createRoute({
|
|
144
|
+
getParentRoute: () => postsRoute,
|
|
145
|
+
path: '$postId',
|
|
146
|
+
})
|
|
147
|
+
|
|
148
|
+
const postEditorRoute = createRoute({
|
|
149
|
+
getParentRoute: () => rootRoute,
|
|
150
|
+
path: 'posts/$postId/edit',
|
|
151
|
+
})
|
|
152
|
+
|
|
153
|
+
const settingsRoute = createRoute({
|
|
154
|
+
getParentRoute: () => rootRoute,
|
|
155
|
+
path: 'settings',
|
|
156
|
+
})
|
|
157
|
+
|
|
158
|
+
const profileRoute = createRoute({
|
|
159
|
+
getParentRoute: () => settingsRoute,
|
|
160
|
+
path: 'profile',
|
|
161
|
+
})
|
|
162
|
+
|
|
163
|
+
const notificationsRoute = createRoute({
|
|
164
|
+
getParentRoute: () => settingsRoute,
|
|
165
|
+
path: 'notifications',
|
|
166
|
+
})
|
|
167
|
+
|
|
168
|
+
const pathlessLayoutRoute = createRoute({
|
|
169
|
+
getParentRoute: () => rootRoute,
|
|
170
|
+
id: 'pathlessLayout',
|
|
171
|
+
})
|
|
172
|
+
|
|
173
|
+
const pathlessLayoutARoute = createRoute({
|
|
174
|
+
getParentRoute: () => pathlessLayoutRoute,
|
|
175
|
+
path: 'route-a',
|
|
176
|
+
})
|
|
177
|
+
|
|
178
|
+
const pathlessLayoutBRoute = createRoute({
|
|
179
|
+
getParentRoute: () => pathlessLayoutRoute,
|
|
180
|
+
path: 'route-b',
|
|
181
|
+
})
|
|
182
|
+
|
|
183
|
+
const filesRoute = createRoute({
|
|
184
|
+
getParentRoute: () => rootRoute,
|
|
185
|
+
path: 'files/$',
|
|
186
|
+
})
|
|
187
|
+
\`\`\`
|
|
188
|
+
|
|
189
|
+
<!-- ::end:framework -->
|
|
190
|
+
|
|
112
191
|
## Anatomy of a Route
|
|
113
192
|
|
|
114
193
|
All other routes other than the root route are configured using the \`createRoute\` function:
|
|
@@ -196,6 +275,10 @@ Creating a root route in code-based routing is thankfully the same as doing so i
|
|
|
196
275
|
|
|
197
276
|
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
277
|
|
|
278
|
+
<!-- ::start:framework -->
|
|
279
|
+
|
|
280
|
+
# React
|
|
281
|
+
|
|
199
282
|
\`\`\`tsx
|
|
200
283
|
// Standard root route
|
|
201
284
|
import { createRootRoute } from '@tanstack/react-router'
|
|
@@ -212,6 +295,26 @@ export interface MyRouterContext {
|
|
|
212
295
|
const rootRoute = createRootRouteWithContext<MyRouterContext>()
|
|
213
296
|
\`\`\`
|
|
214
297
|
|
|
298
|
+
# Solid
|
|
299
|
+
|
|
300
|
+
\`\`\`tsx
|
|
301
|
+
// Standard root route
|
|
302
|
+
import { createRootRoute } from '@tanstack/solid-router'
|
|
303
|
+
|
|
304
|
+
const rootRoute = createRootRoute()
|
|
305
|
+
|
|
306
|
+
// Root route with Context
|
|
307
|
+
import { createRootRouteWithContext } from '@tanstack/solid-router'
|
|
308
|
+
import type { QueryClient } from '@tanstack/solid-query'
|
|
309
|
+
|
|
310
|
+
export interface MyRouterContext {
|
|
311
|
+
queryClient: QueryClient
|
|
312
|
+
}
|
|
313
|
+
const rootRoute = createRootRouteWithContext<MyRouterContext>()
|
|
314
|
+
\`\`\`
|
|
315
|
+
|
|
316
|
+
<!-- ::end:framework -->
|
|
317
|
+
|
|
215
318
|
To learn more about Context in TanStack Router, see the [Router Context](../guide/router-context.md) guide.
|
|
216
319
|
|
|
217
320
|
## Basic Routes
|
|
@@ -541,14 +644,23 @@ To get started with file-based routing, you'll need to configure your project's
|
|
|
541
644
|
|
|
542
645
|
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
646
|
|
|
544
|
-
|
|
647
|
+
<!-- ::start:framework -->
|
|
648
|
+
|
|
649
|
+
# React
|
|
650
|
+
|
|
651
|
+
- [Installation with Vite](../installation/with-vite)
|
|
652
|
+
- [Installation with Rspack/Rsbuild](../installation/with-rspack)
|
|
653
|
+
- [Installation with Webpack](../installation/with-webpack)
|
|
654
|
+
- [Installation with Esbuild](../installation/with-esbuild)
|
|
655
|
+
|
|
656
|
+
# Solid
|
|
545
657
|
|
|
546
658
|
- [Installation with Vite](../installation/with-vite)
|
|
547
659
|
- [Installation with Rspack/Rsbuild](../installation/with-rspack)
|
|
548
660
|
- [Installation with Webpack](../installation/with-webpack)
|
|
549
661
|
- [Installation with Esbuild](../installation/with-esbuild)
|
|
550
662
|
|
|
551
|
-
|
|
663
|
+
<!-- ::end:framework -->
|
|
552
664
|
|
|
553
665
|
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
666
|
|
|
@@ -558,20 +670,20 @@ If your bundler is not yet supported, you can reach out to us on Discord or GitH
|
|
|
558
670
|
|
|
559
671
|
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
672
|
|
|
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
|
-
| **\`[x]\` Escaping** | Square brackets escape special characters in filenames that would otherwise have routing meaning. For example, \`script[.]js.tsx\` becomes \`/script.js\` and \`api[.]v1.tsx\` becomes \`/api.v1\`.
|
|
571
|
-
| **\`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 (supports both strings and regex patterns), see [options](
|
|
572
|
-
| **\`.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 (supports both strings and regex patterns), see [options](
|
|
573
|
-
|
|
574
|
-
> **💡 Remember:** The file-naming conventions for your project could be affected by what [options](
|
|
673
|
+
| Feature | Description |
|
|
674
|
+
| ---------------------------------- | -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- |
|
|
675
|
+
| **\`__root.tsx\`** | The root route file must be named \`__root.tsx\` and must be placed in the root of the configured \`routesDirectory\`. |
|
|
676
|
+
| **\`.\` Separator** | Routes can use the \`.\` character to denote a nested route. For example, \`blog.post\` will be generated as a child of \`blog\`. |
|
|
677
|
+
| **\`$\` Token** | Route segments with the \`$\` token are parameterized and will extract the value from the URL pathname as a route \`param\`. |
|
|
678
|
+
| **\`_\` 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. |
|
|
679
|
+
| **\`_\` Suffix** | Route segments with the \`_\` suffix exclude the route from being nested under any parent routes. |
|
|
680
|
+
| **\`-\` 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. |
|
|
681
|
+
| **\`(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. |
|
|
682
|
+
| **\`[x]\` Escaping** | Square brackets escape special characters in filenames that would otherwise have routing meaning. For example, \`script[.]js.tsx\` becomes \`/script.js\` and \`api[.]v1.tsx\` becomes \`/api.v1\`. |
|
|
683
|
+
| **\`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 (supports both strings and regex patterns), see [options](../api/file-based-routing.md#indextoken). |
|
|
684
|
+
| **\`.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 (supports both strings and regex patterns), see [options](../api/file-based-routing.md#routetoken). |
|
|
685
|
+
|
|
686
|
+
> **💡 Remember:** The file-naming conventions for your project could be affected by what [options](../api/file-based-routing.md) are configured.
|
|
575
687
|
|
|
576
688
|
## Dynamic Path Params
|
|
577
689
|
|
|
@@ -764,7 +876,11 @@ Each of these concepts is useful and powerful, and we'll dive into each of them
|
|
|
764
876
|
|
|
765
877
|
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:
|
|
766
878
|
|
|
767
|
-
|
|
879
|
+
<!-- ::start:framework -->
|
|
880
|
+
|
|
881
|
+
# React
|
|
882
|
+
|
|
883
|
+
\`\`\`tsx title="src/routes/index.tsx"
|
|
768
884
|
import { createFileRoute } from '@tanstack/react-router'
|
|
769
885
|
|
|
770
886
|
export const Route = createFileRoute('/')({
|
|
@@ -772,6 +888,18 @@ export const Route = createFileRoute('/')({
|
|
|
772
888
|
})
|
|
773
889
|
\`\`\`
|
|
774
890
|
|
|
891
|
+
# Solid
|
|
892
|
+
|
|
893
|
+
\`\`\`tsx title="src/routes/index.tsx"
|
|
894
|
+
import { createFileRoute } from '@tanstack/solid-router'
|
|
895
|
+
|
|
896
|
+
export const Route = createFileRoute('/')({
|
|
897
|
+
component: PostsComponent,
|
|
898
|
+
})
|
|
899
|
+
\`\`\`
|
|
900
|
+
|
|
901
|
+
<!-- ::end:framework -->
|
|
902
|
+
|
|
775
903
|
The \`createFileRoute\` function takes a single argument, the file-route's path as a string.
|
|
776
904
|
|
|
777
905
|
**❓❓❓ "Wait, you're making me pass the path of the route file to \`createFileRoute\`?"**
|
|
@@ -797,6 +925,10 @@ Even though it doesn't have a path, the root route has access to all of the same
|
|
|
797
925
|
|
|
798
926
|
To create a root route, call the \`createRootRoute()\` function and export it as the \`Route\` variable in your route file:
|
|
799
927
|
|
|
928
|
+
<!-- ::start:framework -->
|
|
929
|
+
|
|
930
|
+
# React
|
|
931
|
+
|
|
800
932
|
\`\`\`tsx
|
|
801
933
|
// Standard root route
|
|
802
934
|
import { createRootRoute } from '@tanstack/react-router'
|
|
@@ -813,6 +945,26 @@ export interface MyRouterContext {
|
|
|
813
945
|
export const Route = createRootRouteWithContext<MyRouterContext>()
|
|
814
946
|
\`\`\`
|
|
815
947
|
|
|
948
|
+
# Solid
|
|
949
|
+
|
|
950
|
+
\`\`\`tsx
|
|
951
|
+
// Standard root route
|
|
952
|
+
import { createRootRoute } from '@tanstack/solid-router'
|
|
953
|
+
|
|
954
|
+
export const Route = createRootRoute()
|
|
955
|
+
|
|
956
|
+
// Root route with Context
|
|
957
|
+
import { createRootRouteWithContext } from '@tanstack/solid-router'
|
|
958
|
+
import type { QueryClient } from '@tanstack/solid-query'
|
|
959
|
+
|
|
960
|
+
export interface MyRouterContext {
|
|
961
|
+
queryClient: QueryClient
|
|
962
|
+
}
|
|
963
|
+
export const Route = createRootRouteWithContext<MyRouterContext>()
|
|
964
|
+
\`\`\`
|
|
965
|
+
|
|
966
|
+
<!-- ::end:framework -->
|
|
967
|
+
|
|
816
968
|
To learn more about Context in TanStack Router, see the [Router Context](../guide/router-context.md) guide.
|
|
817
969
|
|
|
818
970
|
## Basic Routes
|
|
@@ -821,8 +973,11 @@ Basic routes match a specific path, for example \`/about\`, \`/settings\`, \`/se
|
|
|
821
973
|
|
|
822
974
|
Let's take a look at an \`/about\` route:
|
|
823
975
|
|
|
824
|
-
|
|
825
|
-
|
|
976
|
+
<!-- ::start:framework -->
|
|
977
|
+
|
|
978
|
+
# React
|
|
979
|
+
|
|
980
|
+
\`\`\`tsx title="src/routes/about.tsx"
|
|
826
981
|
import { createFileRoute } from '@tanstack/react-router'
|
|
827
982
|
|
|
828
983
|
export const Route = createFileRoute('/about')({
|
|
@@ -834,6 +989,22 @@ function AboutComponent() {
|
|
|
834
989
|
}
|
|
835
990
|
\`\`\`
|
|
836
991
|
|
|
992
|
+
# Solid
|
|
993
|
+
|
|
994
|
+
\`\`\`tsx title="src/routes/about.tsx"
|
|
995
|
+
import { createFileRoute } from '@tanstack/solid-router'
|
|
996
|
+
|
|
997
|
+
export const Route = createFileRoute('/about')({
|
|
998
|
+
component: AboutComponent,
|
|
999
|
+
})
|
|
1000
|
+
|
|
1001
|
+
function AboutComponent() {
|
|
1002
|
+
return <div>About</div>
|
|
1003
|
+
}
|
|
1004
|
+
\`\`\`
|
|
1005
|
+
|
|
1006
|
+
<!-- ::end:framework -->
|
|
1007
|
+
|
|
837
1008
|
Basic routes are simple and straightforward. They match the path exactly and render the provided component.
|
|
838
1009
|
|
|
839
1010
|
## Index Routes
|
|
@@ -842,8 +1013,11 @@ Index routes specifically target their parent route when it is **matched exactly
|
|
|
842
1013
|
|
|
843
1014
|
Let's take a look at an index route for a \`/posts\` URL:
|
|
844
1015
|
|
|
845
|
-
|
|
846
|
-
|
|
1016
|
+
<!-- ::start:framework -->
|
|
1017
|
+
|
|
1018
|
+
# React
|
|
1019
|
+
|
|
1020
|
+
\`\`\`tsx title="src/routes/posts.index.tsx"
|
|
847
1021
|
import { createFileRoute } from '@tanstack/react-router'
|
|
848
1022
|
|
|
849
1023
|
// Note the trailing slash, which is used to target index routes
|
|
@@ -856,6 +1030,23 @@ function PostsIndexComponent() {
|
|
|
856
1030
|
}
|
|
857
1031
|
\`\`\`
|
|
858
1032
|
|
|
1033
|
+
# Solid
|
|
1034
|
+
|
|
1035
|
+
\`\`\`tsx title="src/routes/posts.index.tsx"
|
|
1036
|
+
import { createFileRoute } from '@tanstack/solid-router'
|
|
1037
|
+
|
|
1038
|
+
// Note the trailing slash, which is used to target index routes
|
|
1039
|
+
export const Route = createFileRoute('/posts/')({
|
|
1040
|
+
component: PostsIndexComponent,
|
|
1041
|
+
})
|
|
1042
|
+
|
|
1043
|
+
function PostsIndexComponent() {
|
|
1044
|
+
return <div>Please select a post!</div>
|
|
1045
|
+
}
|
|
1046
|
+
\`\`\`
|
|
1047
|
+
|
|
1048
|
+
<!-- ::end:framework -->
|
|
1049
|
+
|
|
859
1050
|
This route will be matched when the URL is \`/posts\` exactly.
|
|
860
1051
|
|
|
861
1052
|
## Dynamic Route Segments
|
|
@@ -864,7 +1055,11 @@ Route path segments that start with a \`$\` followed by a label are dynamic and
|
|
|
864
1055
|
|
|
865
1056
|
These params are then usable in your route's configuration and components! Let's look at a \`posts.$postId.tsx\` route:
|
|
866
1057
|
|
|
867
|
-
|
|
1058
|
+
<!-- ::start:framework -->
|
|
1059
|
+
|
|
1060
|
+
# React
|
|
1061
|
+
|
|
1062
|
+
\`\`\`tsx title="src/routes/posts.tsx"
|
|
868
1063
|
import { createFileRoute } from '@tanstack/react-router'
|
|
869
1064
|
|
|
870
1065
|
export const Route = createFileRoute('/posts/$postId')({
|
|
@@ -881,6 +1076,27 @@ function PostComponent() {
|
|
|
881
1076
|
}
|
|
882
1077
|
\`\`\`
|
|
883
1078
|
|
|
1079
|
+
# Solid
|
|
1080
|
+
|
|
1081
|
+
\`\`\`tsx title="src/routes/posts.tsx"
|
|
1082
|
+
import { createFileRoute } from '@tanstack/solid-router'
|
|
1083
|
+
|
|
1084
|
+
export const Route = createFileRoute('/posts/$postId')({
|
|
1085
|
+
// In a loader
|
|
1086
|
+
loader: ({ params }) => fetchPost(params.postId),
|
|
1087
|
+
// Or in a component
|
|
1088
|
+
component: PostComponent,
|
|
1089
|
+
})
|
|
1090
|
+
|
|
1091
|
+
function PostComponent() {
|
|
1092
|
+
// In a component!
|
|
1093
|
+
const { postId } = Route.useParams()
|
|
1094
|
+
return <div>Post ID: {postId()}</div>
|
|
1095
|
+
}
|
|
1096
|
+
\`\`\`
|
|
1097
|
+
|
|
1098
|
+
<!-- ::end:framework -->
|
|
1099
|
+
|
|
884
1100
|
> 🧠 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.
|
|
885
1101
|
|
|
886
1102
|
## Splat / Catch-All Routes
|
|
@@ -903,8 +1119,12 @@ For example, a route targeting the \`files/$\` path is a splat route. If the URL
|
|
|
903
1119
|
|
|
904
1120
|
Optional path parameters allow you to define route segments that may or may not be present in the URL. They use the \`{-$paramName}\` syntax and provide flexible routing patterns where certain parameters are optional.
|
|
905
1121
|
|
|
906
|
-
|
|
907
|
-
|
|
1122
|
+
<!-- ::start:framework -->
|
|
1123
|
+
|
|
1124
|
+
# React
|
|
1125
|
+
|
|
1126
|
+
\`\`\`tsx title="src/routes/posts.{-$category}.tsx"
|
|
1127
|
+
// The \`-$category\` segment is optional, so this route matches both \`/posts\` and \`/posts/tech\`
|
|
908
1128
|
import { createFileRoute } from '@tanstack/react-router'
|
|
909
1129
|
|
|
910
1130
|
export const Route = createFileRoute('/posts/{-$category}')({
|
|
@@ -918,17 +1138,55 @@ function PostsComponent() {
|
|
|
918
1138
|
}
|
|
919
1139
|
\`\`\`
|
|
920
1140
|
|
|
1141
|
+
# Solid
|
|
1142
|
+
|
|
1143
|
+
\`\`\`tsx title="src/routes/posts.{-$category}.tsx"
|
|
1144
|
+
// The \`-$category\` segment is optional, so this route matches both \`/posts\` and \`/posts/tech\`
|
|
1145
|
+
import { createFileRoute } from '@tanstack/solid-router'
|
|
1146
|
+
|
|
1147
|
+
export const Route = createFileRoute('/posts/{-$category}')({
|
|
1148
|
+
component: PostsComponent,
|
|
1149
|
+
})
|
|
1150
|
+
|
|
1151
|
+
function PostsComponent() {
|
|
1152
|
+
const { category } = Route.useParams()
|
|
1153
|
+
|
|
1154
|
+
return <div>{category ? \`Posts in \${category()}\` : 'All Posts'}</div>
|
|
1155
|
+
}
|
|
1156
|
+
\`\`\`
|
|
1157
|
+
|
|
1158
|
+
<!-- ::end:framework -->
|
|
1159
|
+
|
|
921
1160
|
This route will match both \`/posts\` (category is \`undefined\`) and \`/posts/tech\` (category is \`"tech"\`).
|
|
922
1161
|
|
|
923
1162
|
You can also define multiple optional parameters in a single route:
|
|
924
1163
|
|
|
925
|
-
|
|
926
|
-
|
|
1164
|
+
<!-- ::start:framework -->
|
|
1165
|
+
|
|
1166
|
+
# React
|
|
1167
|
+
|
|
1168
|
+
\`\`\`tsx title="src/routes/posts.{-$category}.\${-$slug}.tsx"
|
|
1169
|
+
// The \`-$category\` segment is optional, so this route matches both \`/posts\` and \`/posts/tech\`
|
|
1170
|
+
import { createFileRoute } from '@tanstack/react-router'
|
|
1171
|
+
|
|
927
1172
|
export const Route = createFileRoute('/posts/{-$category}/{-$slug}')({
|
|
928
1173
|
component: PostsComponent,
|
|
929
1174
|
})
|
|
930
1175
|
\`\`\`
|
|
931
1176
|
|
|
1177
|
+
# Solid
|
|
1178
|
+
|
|
1179
|
+
\`\`\`tsx title="src/routes/posts.{-$category}.\${-$slug}.tsx"
|
|
1180
|
+
// The \`-$category\` segment is optional, so this route matches both \`/posts\` and \`/posts/tech\`
|
|
1181
|
+
import { createFileRoute } from '@tanstack/solid-router'
|
|
1182
|
+
|
|
1183
|
+
export const Route = createFileRoute('/posts/{-$category}/{-$slug}')({
|
|
1184
|
+
component: PostsComponent,
|
|
1185
|
+
})
|
|
1186
|
+
\`\`\`
|
|
1187
|
+
|
|
1188
|
+
<!-- ::end:framework -->
|
|
1189
|
+
|
|
932
1190
|
This route matches \`/posts\`, \`/posts/tech\`, and \`/posts/tech/hello-world\`.
|
|
933
1191
|
|
|
934
1192
|
> 🧠 Routes with optional parameters are ranked lower in priority than exact matches, ensuring that more specific routes like \`/posts/featured\` are matched before \`/posts/{-$category}\`.
|
|
@@ -957,7 +1215,11 @@ In the tree above, \`app.tsx\` is a layout route that wraps two child routes, \`
|
|
|
957
1215
|
|
|
958
1216
|
This tree structure is used to wrap the child routes with a layout component:
|
|
959
1217
|
|
|
960
|
-
|
|
1218
|
+
<!-- ::start:framework -->
|
|
1219
|
+
|
|
1220
|
+
# React
|
|
1221
|
+
|
|
1222
|
+
\`\`\`tsx title="src/routes/app.tsx"
|
|
961
1223
|
import { Outlet, createFileRoute } from '@tanstack/react-router'
|
|
962
1224
|
|
|
963
1225
|
export const Route = createFileRoute('/app')({
|
|
@@ -974,6 +1236,27 @@ function AppLayoutComponent() {
|
|
|
974
1236
|
}
|
|
975
1237
|
\`\`\`
|
|
976
1238
|
|
|
1239
|
+
# Solid
|
|
1240
|
+
|
|
1241
|
+
\`\`\`tsx title="src/routes/app.tsx"
|
|
1242
|
+
import { Outlet, createFileRoute } from '@tanstack/solid-router'
|
|
1243
|
+
|
|
1244
|
+
export const Route = createFileRoute('/app')({
|
|
1245
|
+
component: AppLayoutComponent,
|
|
1246
|
+
})
|
|
1247
|
+
|
|
1248
|
+
function AppLayoutComponent() {
|
|
1249
|
+
return (
|
|
1250
|
+
<div>
|
|
1251
|
+
<h1>App Layout</h1>
|
|
1252
|
+
<Outlet />
|
|
1253
|
+
</div>
|
|
1254
|
+
)
|
|
1255
|
+
}
|
|
1256
|
+
\`\`\`
|
|
1257
|
+
|
|
1258
|
+
<!-- ::end:framework -->
|
|
1259
|
+
|
|
977
1260
|
The following table shows which component(s) will be rendered based on the URL:
|
|
978
1261
|
|
|
979
1262
|
| URL Path | Component |
|
|
@@ -1028,7 +1311,11 @@ In the tree above, \`_pathlessLayout.tsx\` is a pathless layout route that wraps
|
|
|
1028
1311
|
|
|
1029
1312
|
The \`_pathlessLayout.tsx\` route is used to wrap the child routes with a Pathless layout component:
|
|
1030
1313
|
|
|
1031
|
-
|
|
1314
|
+
<!-- ::start:framework -->
|
|
1315
|
+
|
|
1316
|
+
# React
|
|
1317
|
+
|
|
1318
|
+
\`\`\`tsx title="src/routes/_pathlessLayout.tsx"
|
|
1032
1319
|
import { Outlet, createFileRoute } from '@tanstack/react-router'
|
|
1033
1320
|
|
|
1034
1321
|
export const Route = createFileRoute('/_pathlessLayout')({
|
|
@@ -1045,6 +1332,27 @@ function PathlessLayoutComponent() {
|
|
|
1045
1332
|
}
|
|
1046
1333
|
\`\`\`
|
|
1047
1334
|
|
|
1335
|
+
# Solid
|
|
1336
|
+
|
|
1337
|
+
\`\`\`tsx title="src/routes/_pathlessLayout.tsx"
|
|
1338
|
+
import { Outlet, createFileRoute } from '@tanstack/solid-router'
|
|
1339
|
+
|
|
1340
|
+
export const Route = createFileRoute('/_pathlessLayout')({
|
|
1341
|
+
component: PathlessLayoutComponent,
|
|
1342
|
+
})
|
|
1343
|
+
|
|
1344
|
+
function PathlessLayoutComponent() {
|
|
1345
|
+
return (
|
|
1346
|
+
<div>
|
|
1347
|
+
<h1>Pathless layout</h1>
|
|
1348
|
+
<Outlet />
|
|
1349
|
+
</div>
|
|
1350
|
+
)
|
|
1351
|
+
}
|
|
1352
|
+
\`\`\`
|
|
1353
|
+
|
|
1354
|
+
<!-- ::end:framework -->
|
|
1355
|
+
|
|
1048
1356
|
The following table shows which component will be rendered based on the URL:
|
|
1049
1357
|
|
|
1050
1358
|
| URL Path | Component |
|
|
@@ -1124,7 +1432,11 @@ routes/
|
|
|
1124
1432
|
|
|
1125
1433
|
We can import from the excluded files into our posts route
|
|
1126
1434
|
|
|
1127
|
-
|
|
1435
|
+
<!-- ::start:framework -->
|
|
1436
|
+
|
|
1437
|
+
# React
|
|
1438
|
+
|
|
1439
|
+
\`\`\`tsx title="src/routes/posts.tsx"
|
|
1128
1440
|
import { createFileRoute } from '@tanstack/react-router'
|
|
1129
1441
|
import { PostsTable } from './-posts-table'
|
|
1130
1442
|
import { PostsHeader } from './-components/header'
|
|
@@ -1148,6 +1460,34 @@ function PostComponent() {
|
|
|
1148
1460
|
}
|
|
1149
1461
|
\`\`\`
|
|
1150
1462
|
|
|
1463
|
+
# Solid
|
|
1464
|
+
|
|
1465
|
+
\`\`\`tsx title="src/routes/posts.tsx"
|
|
1466
|
+
import { createFileRoute } from '@tanstack/solid-router'
|
|
1467
|
+
import { PostsTable } from './-posts-table'
|
|
1468
|
+
import { PostsHeader } from './-components/header'
|
|
1469
|
+
import { PostsFooter } from './-components/footer'
|
|
1470
|
+
|
|
1471
|
+
export const Route = createFileRoute('/posts')({
|
|
1472
|
+
loader: () => fetchPosts(),
|
|
1473
|
+
component: PostComponent,
|
|
1474
|
+
})
|
|
1475
|
+
|
|
1476
|
+
function PostComponent() {
|
|
1477
|
+
const posts = Route.useLoaderData()
|
|
1478
|
+
|
|
1479
|
+
return (
|
|
1480
|
+
<div>
|
|
1481
|
+
<PostsHeader />
|
|
1482
|
+
<PostsTable posts={posts} />
|
|
1483
|
+
<PostsFooter />
|
|
1484
|
+
</div>
|
|
1485
|
+
)
|
|
1486
|
+
}
|
|
1487
|
+
\`\`\`
|
|
1488
|
+
|
|
1489
|
+
<!-- ::end:framework -->
|
|
1490
|
+
|
|
1151
1491
|
The excluded files will not be added to \`routeTree.gen.ts\`.
|
|
1152
1492
|
|
|
1153
1493
|
## Pathless Route Group Directories
|
|
@@ -1229,8 +1569,11 @@ Virtual file routes can be configured either via:
|
|
|
1229
1569
|
|
|
1230
1570
|
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:
|
|
1231
1571
|
|
|
1232
|
-
|
|
1233
|
-
|
|
1572
|
+
<!-- ::start:framework -->
|
|
1573
|
+
|
|
1574
|
+
# React
|
|
1575
|
+
|
|
1576
|
+
\`\`\`tsx title="vite.config.ts"
|
|
1234
1577
|
import { defineConfig } from 'vite'
|
|
1235
1578
|
import react from '@vitejs/plugin-react'
|
|
1236
1579
|
import { tanstackRouter } from '@tanstack/router-plugin/vite'
|
|
@@ -1246,6 +1589,26 @@ export default defineConfig({
|
|
|
1246
1589
|
})
|
|
1247
1590
|
\`\`\`
|
|
1248
1591
|
|
|
1592
|
+
# Solid
|
|
1593
|
+
|
|
1594
|
+
\`\`\`tsx title="vite.config.ts"
|
|
1595
|
+
import { defineConfig } from 'vite'
|
|
1596
|
+
import solid from 'vite-plugin-solid'
|
|
1597
|
+
import { tanstackRouter } from '@tanstack/router-plugin/vite'
|
|
1598
|
+
|
|
1599
|
+
export default defineConfig({
|
|
1600
|
+
plugins: [
|
|
1601
|
+
tanstackRouter({
|
|
1602
|
+
target: 'solid',
|
|
1603
|
+
virtualRouteConfig: './routes.ts',
|
|
1604
|
+
}),
|
|
1605
|
+
solid(),
|
|
1606
|
+
],
|
|
1607
|
+
})
|
|
1608
|
+
\`\`\`
|
|
1609
|
+
|
|
1610
|
+
<!-- ::end:framework -->
|
|
1611
|
+
|
|
1249
1612
|
Or, you choose to define the virtual routes directly in the configuration:
|
|
1250
1613
|
|
|
1251
1614
|
\`\`\`tsx
|
|
@@ -1264,6 +1627,48 @@ export default defineConfig({
|
|
|
1264
1627
|
})
|
|
1265
1628
|
\`\`\`
|
|
1266
1629
|
|
|
1630
|
+
<!-- ::start:framework -->
|
|
1631
|
+
|
|
1632
|
+
# React
|
|
1633
|
+
|
|
1634
|
+
\`\`\`tsx title="vite.config.ts"
|
|
1635
|
+
import { defineConfig } from 'vite'
|
|
1636
|
+
import react from '@vitejs/plugin-react'
|
|
1637
|
+
import { tanstackRouter } from '@tanstack/router-plugin/vite'
|
|
1638
|
+
|
|
1639
|
+
const routes = rootRoute('root.tsx', [
|
|
1640
|
+
// ... the rest of your virtual route tree
|
|
1641
|
+
])
|
|
1642
|
+
|
|
1643
|
+
export default defineConfig({
|
|
1644
|
+
plugins: [
|
|
1645
|
+
tanstackRouter({ virtualRouteConfig: routes, target: 'react' }),
|
|
1646
|
+
react(),
|
|
1647
|
+
],
|
|
1648
|
+
})
|
|
1649
|
+
\`\`\`
|
|
1650
|
+
|
|
1651
|
+
# Solid
|
|
1652
|
+
|
|
1653
|
+
\`\`\`tsx title="vite.config.ts"
|
|
1654
|
+
import { defineConfig } from 'vite'
|
|
1655
|
+
import solid from 'vite-plugin-solid'
|
|
1656
|
+
import { tanstackRouter } from '@tanstack/router-plugin/vite'
|
|
1657
|
+
|
|
1658
|
+
const routes = rootRoute('root.tsx', [
|
|
1659
|
+
// ... the rest of your virtual route tree
|
|
1660
|
+
])
|
|
1661
|
+
|
|
1662
|
+
export default defineConfig({
|
|
1663
|
+
plugins: [
|
|
1664
|
+
tanstackRouter({ virtualRouteConfig: routes, target: 'solid' }),
|
|
1665
|
+
solid(),
|
|
1666
|
+
],
|
|
1667
|
+
})
|
|
1668
|
+
\`\`\`
|
|
1669
|
+
|
|
1670
|
+
<!-- ::end:framework -->
|
|
1671
|
+
|
|
1267
1672
|
## Creating Virtual File Routes
|
|
1268
1673
|
|
|
1269
1674
|
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:
|