Unverified Commit ed35e2a6 authored by Steven Tey's avatar Steven Tey Committed by GitHub

Added docs on async `not-found.js` server components (#51999)

While `not-found.js` components do not accept any props, you can still turn the `not-found.js` component into an async server component to fetch dynamic data:


```tsx filename="app/blog/not-found.tsx" switcher
import Link from 'next/link'
import { headers } from "next/headers";

export default async function NotFound() {
  const headersList = headers();
  const domain = headersList.get("host")
  const data = await getSiteData(domain)
  return (
    <div>
      <h2>Not Found: {data.name}</h2>
      <p>Could not find requested resource</p>
      <p>
        View <Link href="/blog">all posts</Link>
      </p>
    </div>
  )
}
```
Co-authored-by: 's avatarLee Robinson <9113740+leerob@users.noreply.github.com>
Co-authored-by: 's avatarJJ Kasper <22380829+ijjk@users.noreply.github.com>
parent b149fb70
...@@ -13,9 +13,7 @@ export default function NotFound() { ...@@ -13,9 +13,7 @@ export default function NotFound() {
<div> <div>
<h2>Not Found</h2> <h2>Not Found</h2>
<p>Could not find requested resource</p> <p>Could not find requested resource</p>
<p> <Link href="/">Return Home</Link>
View <Link href="/blog">all posts</Link>
</p>
</div> </div>
) )
} }
...@@ -29,9 +27,7 @@ export default function NotFound() { ...@@ -29,9 +27,7 @@ export default function NotFound() {
<div> <div>
<h2>Not Found</h2> <h2>Not Found</h2>
<p>Could not find requested resource</p> <p>Could not find requested resource</p>
<p> <Link href="/">Return Home</Link>
View <Link href="/blog">all posts</Link>
</p>
</div> </div>
) )
} }
...@@ -43,6 +39,50 @@ export default function NotFound() { ...@@ -43,6 +39,50 @@ export default function NotFound() {
`not-found.js` components do not accept any props. `not-found.js` components do not accept any props.
## Data Fetching
By default, `not-found` is a Server Component. You can mark it as `async` to fetch and display data:
```tsx filename="app/blog/not-found.tsx" switcher
import Link from 'next/link'
import { headers } from 'next/headers'
export default async function NotFound() {
const headersList = headers()
const domain = headersList.get('host')
const data = await getSiteData(domain)
return (
<div>
<h2>Not Found: {data.name}</h2>
<p>Could not find requested resource</p>
<p>
View <Link href="/blog">all posts</Link>
</p>
</div>
)
}
```
```jsx filename="app/blog/not-found.jsx" switcher
import Link from 'next/link'
import { headers } from 'next/headers'
export default async function NotFound() {
const headersList = headers()
const domain = headersList.get('host')
const data = await getSiteData(domain)
return (
<div>
<h2>Not Found: {data.name}</h2>
<p>Could not find requested resource</p>
<p>
View <Link href="/blog">all posts</Link>
</p>
</div>
)
}
```
## Version History ## Version History
| Version | Changes | | Version | Changes |
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment