Skip to content

Commit

Permalink
feat(react-router): allow to redirect to external targets, add `reloa…
Browse files Browse the repository at this point in the history
…dDocument` option (#3004)

* feat(react-router): allow to redirect to external targets, add `reloadDocument` option

* ci: apply automated fixes

---------

Co-authored-by: autofix-ci[bot] <114827586+autofix-ci[bot]@users.noreply.github.com>
  • Loading branch information
schiller-manuel and autofix-ci[bot] authored Dec 13, 2024
1 parent ccf7972 commit 5ad95cf
Show file tree
Hide file tree
Showing 41 changed files with 1,396 additions and 100 deletions.
40 changes: 40 additions & 0 deletions docs/framework/react/api/router/NavigateOptionsType.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,47 @@ type NavigateOptions = ToOptions & {
replace?: boolean
resetScroll?: boolean
ignoreBlocker?: boolean
reloadDocument?: boolean
href?: string
}
```
## NavigateOptions properties
The `NavigateOptions` object accepts the following properties:
### `replace`
- Type: `boolean`
- Optional
- Defaults to `false`.
- If `true`, the location will be committed to the browser history using `history.replace` instead of `history.push`.
### `resetScroll`
- Type: `boolean`
- Optional
- Defaults to `true` so that the scroll position will be reset to 0,0 after the location is committed to the browser history.
- If `false`, the scroll position will not be reset to 0,0 after the location is committed to history.
### `ignoreBlocker`
- Type: `boolean`
- Optional
- Defaults to `false`.
- If `true`, navigation will ignore any blockers that might prevent it.
### `reloadDocument`
- Type: `boolean`
- Optional
- Defaults to `false`.
- If `true`, navigation to a route inside of router will trigger a full page load instead of the traditional SPA navigation.
### `href`
- Type: `string`
- Optional
- This can be used instead of `to` to navigate to a fully built href, e.g. pointing to an external target.
- [`ToOptions`](./ToOptionsType.md)
11 changes: 9 additions & 2 deletions docs/framework/react/api/router/RedirectType.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ The `Redirect` type is used to represent a redirect action in TanStack Router.

```tsx
export type Redirect = {
code?: number
statusCode?: number
throw?: any
headers?: HeadersInit
} & NavigateOptions
```
Expand All @@ -18,7 +19,7 @@ export type Redirect = {
The `Redirect` object accepts/contains the following properties:
### `code` property
### `statusCode` property
- Type: `number`
- Optional
Expand All @@ -29,3 +30,9 @@ The `Redirect` object accepts/contains the following properties:
- Type: `any`
- Optional
- If provided, will throw the redirect object instead of returning it. This can be useful in places where `throwing` in a function might cause it to have a return type of `never`. In that case, you can use `redirect({ throw: true })` to throw the redirect object instead of returning it.
### `headers` property
- Type: `HeadersInit`
- Optional
- The HTTP headers to use when redirecting.
14 changes: 14 additions & 0 deletions docs/framework/react/start/server-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -578,6 +578,20 @@ export const doStuff = createServerFn({ method: 'GET' }).handler(async () => {
})
```

You can also redirect to an external target using `href`:

```tsx
import { redirect } from '@tanstack/react-router'
import { createServerFn } from '@tanstack/start'

export const auth = createServerFn({ method: 'GET' }).handler(async () => {
// Redirect the user to the auth provider
throw redirect({
href: 'https://authprovider.com/login',
})
})
```

> ⚠️ Do not use Vinxi's `sendRedirect` function to send soft redirects from within server functions. This will send the redirect using the `Location` header and will force a full page hard navigation on the client.
## Redirect Headers
Expand Down
1 change: 1 addition & 0 deletions e2e/react-router/basic-file-based/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
"@types/react": "^18.2.47",
"@types/react-dom": "^18.2.18",
"@vitejs/plugin-react": "^4.3.4",
"combinate": "^1.1.11",
"vite": "^6.0.3"
}
}
Loading

0 comments on commit 5ad95cf

Please sign in to comment.