Skip to content

Commit 7c6b575

Browse files
Christopher96uChristopher Menendez
andauthored
refactor(router-core,start-server-core): improve JSDocs for redirect (#4848)
Co-authored-by: Christopher Menendez <[email protected]>
1 parent d5fd30f commit 7c6b575

File tree

4 files changed

+31
-2
lines changed

4 files changed

+31
-2
lines changed

docs/router/framework/react/api/router/RedirectType.md

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,3 +36,12 @@ The `Redirect` object accepts/contains the following properties:
3636
- Type: `HeadersInit`
3737
- Optional
3838
- The HTTP headers to use when redirecting.
39+
40+
### Navigation Properties
41+
42+
Since `Redirect` extends `NavigateOptions`, it also supports navigation properties:
43+
44+
- **`to`**: Use for internal application routes (e.g., `/dashboard`, `../profile`)
45+
- **`href`**: Use for external URLs (e.g., `https://example.com`, `https://authprovider.com`)
46+
47+
> **Important**: For external URLs, always use the `href` property instead of `to`. The `to` property is designed for internal navigation within your application.

docs/router/framework/react/api/router/redirectFunction.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,22 @@ The `redirect` function accepts a single argument, the `options` to determine th
2323
import { redirect } from '@tanstack/react-router'
2424

2525
const route = createRoute({
26-
// throwing a redirect object
26+
// throwing an internal redirect object using 'to' property
2727
loader: () => {
2828
if (!user) {
2929
throw redirect({
3030
to: '/login',
3131
})
3232
}
3333
},
34+
// throwing an external redirect object using 'href' property
35+
loader: () => {
36+
if (needsExternalAuth) {
37+
throw redirect({
38+
href: 'https://authprovider.com/login',
39+
})
40+
}
41+
},
3442
// or forcing `redirect` to throw itself
3543
loader: () => {
3644
if (!user) {

packages/router-core/src/link.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,12 @@ export interface RequiredToOptions<
393393
in out TFrom extends string,
394394
in out TTo extends string | undefined,
395395
> {
396+
/**
397+
* The internal route path to navigate to. This should be a relative or absolute path within your application.
398+
* For external URLs, use the `href` property instead.
399+
* @example "/dashboard" or "../profile"
400+
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/NavigateOptionsType#href)
401+
*/
396402
to: ToPathOption<TRouter, TFrom, TTo> & {}
397403
}
398404

@@ -401,6 +407,12 @@ export interface OptionalToOptions<
401407
in out TFrom extends string,
402408
in out TTo extends string | undefined,
403409
> {
410+
/**
411+
* The internal route path to navigate to. This should be a relative or absolute path within your application.
412+
* For external URLs, use the `href` property instead.
413+
* @example "/dashboard" or "../profile"
414+
* @link [API Docs](https://tanstack.com/router/latest/docs/framework/react/api/router/NavigateOptionsType#href)
415+
*/
404416
to?: ToPathOption<TRouter, TFrom, TTo> & {}
405417
}
406418

packages/start-server-core/src/createStartHandler.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -279,7 +279,7 @@ export function createStartHandler<TRouter extends AnyRouter>({
279279
!response.options.to.startsWith('/')
280280
) {
281281
throw new Error(
282-
`Server side redirects must use absolute paths via the 'href' or 'to' options. Received: ${JSON.stringify(response.options)}`,
282+
`Server side redirects must use absolute paths via the 'href' or 'to' options. The redirect() method's "to" property accepts an internal path only. Use the "href" property to provide an external URL. Received: ${JSON.stringify(response.options)}`,
283283
)
284284
}
285285

0 commit comments

Comments
 (0)