-
Notifications
You must be signed in to change notification settings - Fork 29.4k
docs: redirect and permanentRedirect can be called from event handlers, etc #84110
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
base: canary
Are you sure you want to change the base?
docs: redirect and permanentRedirect can be called from event handlers, etc #84110
Conversation
--- | ||
|
||
The `permanentRedirect` function allows you to redirect the user to another URL. `permanentRedirect` can be used in Server Components, Client Components, [Route Handlers](/docs/app/api-reference/file-conventions/route), and [Server Actions](/docs/app/getting-started/updating-data). | ||
The `permanentRedirect` function allows you to redirect the user to another URL. `permanentRedirect` can be used in [Server and Client Components](/docs/app/getting-started/server-and-client-components), [Route Handlers](/docs/app/api-reference/file-conventions/route), and [Server Actions](/docs/app/getting-started/updating-data). |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The permanentRedirect documentation mentions it can be used in Client Components but lacks the detailed explanation about event handlers and effects that redirect now includes.
View Details
📝 Patch Details
diff --git a/docs/01-app/03-api-reference/04-functions/permanentRedirect.mdx b/docs/01-app/03-api-reference/04-functions/permanentRedirect.mdx
index e2e58db64d..af3cba7746 100644
--- a/docs/01-app/03-api-reference/04-functions/permanentRedirect.mdx
+++ b/docs/01-app/03-api-reference/04-functions/permanentRedirect.mdx
@@ -6,7 +6,7 @@ related:
- app/api-reference/functions/redirect
---
-The `permanentRedirect` function allows you to redirect the user to another URL. `permanentRedirect` can be used in [Server and Client Components](/docs/app/getting-started/server-and-client-components), [Route Handlers](/docs/app/api-reference/file-conventions/route), and [Server Actions](/docs/app/getting-started/updating-data).
+The `permanentRedirect` function allows you to redirect the user to another URL. `permanentRedirect` can be used in [Server and Client Components](/docs/app/getting-started/server-and-client-components), [Route Handlers](/docs/app/api-reference/file-conventions/route), and [Server Actions](/docs/app/getting-started/updating-data). It can also be called from Client Component event handlers and effects.
When used in a streaming context, this will insert a meta tag to emit the redirect on the client side. When used in a server action, it will serve a 303 HTTP redirect response to the caller. Otherwise, it will serve a 308 (Permanent) HTTP redirect response to the caller.
@@ -59,4 +59,33 @@ export default async function Profile({ params }) {
}
```
+### Client Component
+
+`permanentRedirect` can be called in Client Component event handlers and effects.
+
+```tsx filename="components/client-redirect-button.tsx"
+'use client'
+
+import { permanentRedirect } from 'next/navigation'
+
+export function ClientRedirectButton() {
+ return (
+ <button
+ onClick={() => {
+ permanentRedirect('/dashboard')
+ }}
+ >
+ Redirect to Dashboard
+ </button>
+ )
+}
+```
+
> **Good to know**: `permanentRedirect` does not require you to use `return permanentRedirect()` as it uses the TypeScript [`never`](https://www.typescriptlang.org/docs/handbook/2/functions.html#never) type.
+
+## Version History
+
+| Version | Changes |
+| --------- | -------------------------------------------------------- |
+| `v15.0.0` | `permanentRedirect` can be used in Client Components |
+| `v13.0.0` | `permanentRedirect` introduced. |
Analysis
Inconsistent documentation between redirect and permanentRedirect functions
What fails: permanentRedirect
documentation lacks explicit mention of Client Component event handlers and effects capabilities that were added in v15.0.0, while redirect
documentation correctly documents these features.
How to reproduce:
- Compare
docs/01-app/03-api-reference/04-functions/redirect.mdx
anddocs/01-app/03-api-reference/04-functions/permanentRedirect.mdx
- Notice
redirect
states "it can also be called from Client Component event handlers and effects" with event handler examples - Notice
permanentRedirect
only mentions "can be used in Server and Client Components" without event handler details
Result: Documentation inconsistency where permanentRedirect
appears to have fewer capabilities than redirect
, despite both functions using identical underlying implementation and having the same capabilities.
Expected: Both functions should document identical Client Component capabilities per source code analysis showing both use the same getRedirectError()
mechanism and have identical functionality.
As of v15, redirect, and also permanentRedirect, can be invoked from event handlers, and effects.
Closes: https://linear.app/vercel/issue/DOC-5097/redirect-can-be-called-in-event-handlerseffects