Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs(start): correctly import the redirect fn #2338

Merged
merged 1 commit into from
Sep 15, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions docs/framework/react/start/server-functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,11 @@ Server functions can throw a `redirect` error to redirect the user to a differen
- During SSR, redirects are handled by sending a 302 response to the client with the new location
- On the client, redirects are handled by the router automatically from within a route lifecycle or a component that uses the `useServerFn` hook. If you call a server function from anywhere else, redirects will not be handled automatically.

To throw a redirect, you can use the `redirect` function exported from the `@tanstack/start` package:
To throw a redirect, you can use the `redirect` function exported from the `@tanstack/react-router` package:

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

export const doStuff = createServerFn('GET', async () => {
// Redirect the user to the home page
Expand All @@ -365,7 +366,8 @@ Redirects can utilize all of the same options as `router.navigate`, `useNavigate
Redirects can also set the status code of the response by passing a `status` option:

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

export const doStuff = createServerFn('GET', async () => {
// Redirect the user to the home page with a 301 status code
Expand All @@ -383,7 +385,8 @@ export const doStuff = createServerFn('GET', async () => {
You can also set custom headers on a redirect by passing a `headers` option:

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

export const doStuff = createServerFn('GET', async () => {
// Redirect the user to the home page with a custom header
Expand Down