@@ -67,6 +67,45 @@ export default async function AdminPage() {
...
@@ -67,6 +67,45 @@ export default async function AdminPage() {
}
}
```
```
You can also pass an error cause to `forbidden` to handle different types of errors before escalating to the Next.js'errorpage.Forexample,youcancall`forbidden`withanerrorcauseassociatedwithasessionexpirationerrorandredirecttheusertotheloginpage.
Youcanalsopassanerrorcauseto`notFound`tohandledifferenttypesoferrorsbeforeescalatingtotheNext.js' error page. For example, you can call `notFound` with a path where the resource was relocated to, which can be selectively handled when using this function, while still falling back to a 404 page if unhandled.
```tsx filename="app/auth/page.js"
import { ResourceRelocatedError } from '@/app/lib/dal'
import { notFound } from 'next/navigation'
async function fetchArticle(slug) {
const res = await fetch('https://...')
if (res.status === 301) {
notFound(new ResourceRelocatedError('/new-url'))
}
returnres.json()
}
exportdefaultfunctionPage({params}){
letarticle
try{
constslug=awaitparams.slug
article=awaitfetchArticle(slug)
}catch(err){
if(err.causeinstanceofResourceRelocatedError){
redirect(err.cause.redirectTo)
}
throwerr
}
// ...
}
```
> **Good to know**: `notFound()` does not require you to use `returnnotFound()` due to using the TypeScript [`never`](https://www.typescriptlang.org/docs/handbook/2/functions.html#never) type.
> **Good to know**: `notFound()` does not require you to use `returnnotFound()` due to using the TypeScript [`never`](https://www.typescriptlang.org/docs/handbook/2/functions.html#never) type.
| `path` | `string` | The URL to redirect to. Can be a relative or absolute path. |
| `path` | `string` | The URL to redirect to. Can be a relative or absolute path. |
| `type` | `'replace'` (default) or `'push'` (default in Server Actions) | The type of redirect to perform. |
| `type` | `'replace'` (default) or `'push'` (default in Server Actions) | The type of redirect to perform. |
| `cause` | `Error` | An optional error that will be attached to the `cause` property of the error thrown by Next.js. |
By default, `permanentRedirect` will use `push` (adding a new entry to the browser history stack) in [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) and `replace` (replacing the current URL in the browser history stack) everywhere else. You can override this behavior by specifying the `type` parameter.
By default, `permanentRedirect` will use `push` (adding a new entry to the browser history stack) in [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) and `replace` (replacing the current URL in the browser history stack) everywhere else. You can override this behavior by specifying the `type` parameter.
| `path` | `string` | The URL to redirect to. Can be a relative or absolute path. |
| `path` | `string` | The URL to redirect to. Can be a relative or absolute path. |
| `type` | `'replace'` (default) or `'push'` (default in Server Actions) | The type of redirect to perform. |
| `type` | `'replace'` (default) or `'push'` (default in Server Actions) | The type of redirect to perform. |
| `cause` | `Error` | An optional error that will be attached to the `cause` property of the error thrown by Next.js. |
By default, `redirect` will use `push` (adding a new entry to the browser history stack) in [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) and `replace` (replacing the current URL in the browser history stack) everywhere else. You can override this behavior by specifying the `type` parameter.
By default, `redirect` will use `push` (adding a new entry to the browser history stack) in [Server Actions](/docs/app/building-your-application/data-fetching/server-actions-and-mutations) and `replace` (replacing the current URL in the browser history stack) everywhere else. You can override this behavior by specifying the `type` parameter.
@@ -75,6 +75,49 @@ export default async function DashboardPage() {
...
@@ -75,6 +75,49 @@ export default async function DashboardPage() {
}
}
```
```
You can also pass an error cause to `forbidden` to handle different types of errors before escalating to the Next.js'errorpage.Forexample,youcancall`unauthorized`withanerrorcauseassociatedwithasessionexpirationerrorandredirecttheusertotheloginpage.