@@ -232,14 +232,23 @@ Like Server Actions, arguments to cached functions must be serializable. This me
You can accept unserializable values as arguments as long as you don'tintrospectthem.However,youcanreturnthem.ThisallowspatternslikecachedcomponentsthatacceptServerorClientComponentsaschildren:
```tsx filename="app/cached-wrapper.tsx" switcher
import { ReactNode } from 'react'
import type { ReactNode } from 'react'
import { setTimeout } from 'node:timers/promises'
async function getSiteTitle() {
// Simulate a slow database or API call
await setTimeout(1000) // from 'node:timers/promises'
return 'My Website'
}
export async function CachedWrapper({ children }: { children: ReactNode }) {
'use cache'
const title = await getSiteTitle()
// Don't introspect children, just pass it through
return (
<div className="wrapper">
<header>Cached Header</header>
<h1>{title}</h1>
{children}
</div>
)
...
...
@@ -247,22 +256,28 @@ export async function CachedWrapper({ children }: { children: ReactNode }) {
```
```jsx filename="app/cached-wrapper.js" switcher
import { setTimeout } from 'node:timers/promises'
async function getSiteTitle() {
// Simulate a slow database or API call
await setTimeout(1000) // from 'node:timers/promises'
return 'My Website'
}
export async function CachedWrapper({ children }) {
'use cache'
const title = await getSiteTitle()
// Don't introspect children, just pass it through
Componentsthataccessruntimevalueslike`cookies`or`searchParams`cannotbeprerendered.Toprerendermoreofapage's content, you can pass these props down and access their values lower in the tree. For example, if you are reading `searchParams` from a `<Page />` component, you can forward this value to another component as a prop:
@@ -285,7 +286,7 @@ To improve the initial load time and user experience, you can use streaming to b
height="785"
/>
There are two ways you can implement streaming in your application:
There are two ways you can leverage streaming in your application:
1. Wrapping a page with a [`loading.js` file](#with-loadingjs)
2. Wrapping a component with [`<Suspense>`](#with-suspense)
...
...
@@ -356,7 +357,7 @@ export default function BlogPage() {
<p>Read the latest posts below.</p>
</header>
<main>
{/* Any content wrapped in a <Suspense> boundary will be streamed */}
{/* If there'sanydynamiccontentinsidethisboundary,itwillbestreamedin*/}
<Suspensefallback={<BlogListSkeleton/>}>
<BlogList/>
</Suspense>
...
...
@@ -380,7 +381,7 @@ export default function BlogPage() {
<p>Readthelatestpostsbelow.</p>
</header>
<main>
{/* Any content wrapped in a <Suspense> boundary will be streamed */}
{/* If there's any dynamic content inside this boundary, it will be streamed in */}
<Suspensefallback={<BlogListSkeleton/>}>
<BlogList/>
</Suspense>
...
...
@@ -400,19 +401,9 @@ In development, you can preview and inspect the loading state of your components
### Sequential data fetching
Sequential data fetching happens when nested components in a tree each fetch their own data and the requests are not [deduplicated](/docs/app/guides/caching#request-memoization), leading to longer response times.
Sequential data fetching happens when one request depends on data from another.
There may be cases where you want this pattern because one fetch depends on the result of the other.
For example, the `<Playlists>` component will only start fetching data once the `<Artist>` component has finished fetching data because `<Playlists>` depends on the `artistID` prop:
For example, `<Playlists>` can only fetch data after `<Artist>` completes because it needs the `artistID`:
@@ -482,7 +473,9 @@ async function Playlists({ artistID }) {
}
```
To improve the user experience, you should use [React `<Suspense>`](/docs/app/getting-started/linking-and-navigating#streaming) to show a `fallback` while data is being fetch. This will enable [streaming](#streaming) and prevent the whole route from being blocked by the sequential data requests.
In this example, `<Suspense>` allows the playlists to stream in after the artist data loads. However, the page still waits for the artist data before displaying anything. To prevent this, you can wrap the entire page component in a `<Suspense>` boundary (for example, using a [`loading.js` file](#with-loadingjs)) to show a loading state immediately.
Ensure your data source can resolve the first request quickly, as it blocks everything else. If you can't optimize the request further, consider [caching](#deduplicate-requests-and-cache-data) the result if the data changes infrequently.
### Parallel data fetching
...
...
@@ -597,11 +590,12 @@ export default async function Page({
returnisAvailable?<Itemid={id}/>:null
}
export const preload = (id: string) => {
constpreload=(id:string)=>{
// void evaluates the given expression and returns undefined
@@ -149,7 +149,7 @@ The following options are available for the `next telemetry` command:
Learn more about [Telemetry](/telemetry).
### `next typegen` Options
### `next typegen` options
`next typegen` generates TypeScript definitions for your application's routes without performing a full build. This is useful for IDE autocomplete and CI type-checking of route usage.