You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Adds a `kind` parameter to the `redirect()` function, giving developers
control over how redirects behave on the client side.
### Motivation
Resolves#289. The default redirect behavior (RSC navigation with
`replaceState`) isn't always what's needed. For example, OAuth flows
require a full browser navigation, some redirects should preserve
history for back-button support, and server actions sometimes need to
signal a redirect to the client without automatically navigating.
### API
```ts
redirect(url: string, status?: number, kind?: RedirectKind): never
```
**`RedirectKind`** is a union of four values:
| Kind | Behavior |
|---|---|
| `"navigate"` | **(default)** RSC navigation with `replaceState` — same
as current behavior |
| `"push"` | RSC navigation with `pushState` — adds a history entry so
the user can go back |
| `"location"` | Full browser navigation via `location.href` — useful
for external URLs or OAuth flows |
| `"error"` | Throws the redirect error on the client — allows custom
handling via `try`/`catch` in server actions |
Copy file name to clipboardExpand all lines: docs/src/pages/en/(pages)/framework/http.mdx
+45Lines changed: 45 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -280,6 +280,51 @@ export default function MyComponent() {
280
280
};
281
281
```
282
282
283
+
The `redirect()` function accepts an optional third argument `kind` that controls how the redirect is performed on the client. The available kinds are:
284
+
285
+
| Kind | Description |
286
+
| --- | --- |
287
+
|`"navigate"`|**(default)** Performs an RSC navigation using `replaceState`. The browser URL changes without adding a history entry. |
288
+
|`"push"`| Performs an RSC navigation using `pushState`. The browser URL changes and a new history entry is added, so the user can navigate back. |
289
+
|`"location"`| Forces a full browser navigation via `location.href`. Useful for redirecting to external URLs or when a full page reload is needed. |
290
+
|`"error"`| Throws the redirect error on the client instead of navigating. This allows custom handling via `try`/`catch` in server action calls. |
291
+
292
+
```jsx
293
+
import { redirect } from"@lazarv/react-server";
294
+
295
+
// RSC navigation with pushState (adds history entry)
296
+
redirect("/dashboard", 302, "push");
297
+
298
+
// Full browser navigation
299
+
redirect("/oauth/authorize", 302, "location");
300
+
301
+
// Throw on client for custom handling
302
+
redirect("/login", 302, "error");
303
+
```
304
+
305
+
When using the `"error"` kind in a server action, the client can catch the redirect error and handle it:
Copy file name to clipboardExpand all lines: docs/src/pages/en/(pages)/router/server-routing.mdx
+13Lines changed: 13 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -192,6 +192,19 @@ export default function App() {
192
192
}
193
193
```
194
194
195
+
You can also specify a `kind` parameter to control how the redirect behaves on the client:
196
+
197
+
```tsx
198
+
import { redirect } from"@lazarv/react-server";
199
+
200
+
exportdefaultfunction ProtectedPage() {
201
+
// Redirect with pushState so the user can navigate back
202
+
redirect("/login", 302, "push");
203
+
}
204
+
```
205
+
206
+
Available redirect kinds: `"navigate"` (default, replaceState), `"push"` (pushState), `"location"` (full browser navigation), and `"error"` (throw on client for custom handling). See the [HTTP redirect documentation](/framework/http#redirect) for details.
0 commit comments