@tanstack/react-router 1.147.3 → 1.150.0

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.
@@ -730,6 +730,38 @@ The \`RouteApi\` has the following properties and methods:
730
730
 
731
731
  - A type-safe version of [\`useNavigate\`](./useNavigateHook.md) that is pre-bound to the route ID that the \`RouteApi\` instance was created with.
732
732
 
733
+ ### \`redirect\` method
734
+
735
+ \`\`\`tsx
736
+ redirect(opts?: RedirectOptions): Redirect
737
+ \`\`\`
738
+
739
+ - A type-safe version of the [\`redirect\`](./redirectFunction.md) function that is pre-bound to the route ID that the \`RouteApi\` instance was created with.
740
+ - The \`from\` parameter is automatically set to the route ID, enabling type-safe relative redirects.
741
+ - Options
742
+ - All options from [\`redirect\`](./redirectFunction.md) except \`from\`, which is automatically provided.
743
+ - Returns
744
+ - A \`Redirect\` object that can be thrown from \`beforeLoad\` or \`loader\` callbacks.
745
+
746
+ #### Example
747
+
748
+ \`\`\`tsx
749
+ import { getRouteApi } from '@tanstack/react-router'
750
+
751
+ const routeApi = getRouteApi('/dashboard/settings')
752
+
753
+ export const Route = createFileRoute('/dashboard/settings')({
754
+ beforeLoad: ({ context }) => {
755
+ if (!context.user) {
756
+ // Type-safe redirect - 'from' is automatically '/dashboard/settings'
757
+ throw routeApi.redirect({
758
+ to: '../login', // Relative path to sibling route
759
+ })
760
+ }
761
+ },
762
+ })
763
+ \`\`\`
764
+
733
765
  # Route class
734
766
 
735
767
  > [!CAUTION]
@@ -1217,6 +1249,30 @@ An instance of the \`Route\` has the following properties and methods:
1217
1249
  - Type: \`(lazyImporter: () => Promise<Partial<UpdatableRouteOptions>>) => this\`
1218
1250
  - Updates the route instance with a new lazy importer which will be resolved lazily when loading the route. This can be useful for code splitting.
1219
1251
 
1252
+ ### \`.redirect\` method
1253
+
1254
+ - Type: \`(opts?: RedirectOptions) => Redirect\`
1255
+ - A type-safe version of the [\`redirect\`](./redirectFunction.md) function that is pre-bound to the route's path.
1256
+ - The \`from\` parameter is automatically set to the route's \`fullPath\`, enabling type-safe relative redirects.
1257
+ - See [\`RouteApi.redirect\`](./RouteApiType.md#redirect-method) for more details.
1258
+
1259
+ #### Example
1260
+
1261
+ \`\`\`tsx
1262
+ import { createFileRoute } from '@tanstack/react-router'
1263
+
1264
+ export const Route = createFileRoute('/dashboard/settings')({
1265
+ beforeLoad: ({ context }) => {
1266
+ if (!context.user) {
1267
+ // Type-safe redirect - 'from' is automatically '/dashboard/settings'
1268
+ throw Route.redirect({
1269
+ to: '../login', // Relative path to sibling route
1270
+ })
1271
+ }
1272
+ },
1273
+ })
1274
+ \`\`\`
1275
+
1220
1276
  ### ...\`RouteApi\` methods
1221
1277
 
1222
1278
  - All of the methods from [\`RouteApi\`](./RouteApiType.md) are available.
@@ -3059,6 +3115,8 @@ The \`redirect\` function accepts a single argument, the \`options\` to determin
3059
3115
 
3060
3116
  ## Examples
3061
3117
 
3118
+ ### Using the standalone \`redirect\` function
3119
+
3062
3120
  \`\`\`tsx
3063
3121
  import { redirect } from '@tanstack/react-router'
3064
3122
 
@@ -3092,6 +3150,62 @@ const route = createRoute({
3092
3150
  })
3093
3151
  \`\`\`
3094
3152
 
3153
+ ### Using Route.redirect (File-Based Routes)
3154
+
3155
+ When using file-based routing with \`createFileRoute\`, you can use the \`Route.redirect\` method directly. This method automatically sets the \`from\` parameter based on the route's path, enabling type-safe relative redirects without manually specifying the origin route:
3156
+
3157
+ \`\`\`tsx
3158
+ // In routes/dashboard/settings.tsx
3159
+ import { createFileRoute } from '@tanstack/react-router'
3160
+
3161
+ export const Route = createFileRoute('/dashboard/settings')({
3162
+ beforeLoad: ({ context }) => {
3163
+ if (!context.user) {
3164
+ // Relative redirect - automatically knows we're redirecting from '/dashboard/settings'
3165
+ throw Route.redirect({
3166
+ to: '../login', // Redirects to '/dashboard/login'
3167
+ })
3168
+ }
3169
+ },
3170
+ loader: () => {
3171
+ // Also works in loader
3172
+ if (needsMigration) {
3173
+ throw Route.redirect({
3174
+ to: './migrate', // Redirects to '/dashboard/settings/migrate'
3175
+ })
3176
+ }
3177
+ },
3178
+ })
3179
+ \`\`\`
3180
+
3181
+ ### Using getRouteApi().redirect
3182
+
3183
+ For accessing the redirect method outside of the route definition file, you can use \`getRouteApi\`:
3184
+
3185
+ \`\`\`tsx
3186
+ import { getRouteApi } from '@tanstack/react-router'
3187
+
3188
+ const routeApi = getRouteApi('/dashboard/settings')
3189
+
3190
+ // In a beforeLoad or loader callback
3191
+ function checkAuth() {
3192
+ if (!user) {
3193
+ // Type-safe redirect with automatic 'from' parameter
3194
+ throw routeApi.redirect({
3195
+ to: '../login',
3196
+ })
3197
+ }
3198
+ }
3199
+ \`\`\`
3200
+
3201
+ ### Benefits of Route-Bound Redirect
3202
+
3203
+ Using \`Route.redirect\` or \`getRouteApi().redirect\` instead of the standalone \`redirect\` function offers several advantages:
3204
+
3205
+ 1. **No need to specify \`from\`**: The route path is automatically used as the origin
3206
+ 2. **Type-safe relative paths**: TypeScript validates that relative paths like \`../sibling\` or \`./child\` resolve to valid routes
3207
+ 3. **Refactoring-friendly**: If the route path changes, the redirect origin updates automatically
3208
+
3095
3209
  # Search middleware to retain search params
3096
3210
 
3097
3211
  \`retainSearchParams\` is a search middleware that allows to keep search params.