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
Copy file name to clipboardExpand all lines: docs/01-app/01-getting-started/06-cache-components.mdx
+23-8Lines changed: 23 additions & 8 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -232,37 +232,52 @@ Like Server Actions, arguments to cached functions must be serializable. This me
232
232
You can accept unserializable values as arguments as long as you don't introspect them. However, you can return them. This allows patterns like cached components that accept Server or Client Components as children:
233
233
234
234
```tsx filename="app/cached-wrapper.tsx" switcher
235
-
import { ReactNode } from'react'
235
+
importtype { ReactNode } from'react'
236
+
import { setTimeout } from'node:timers/promises'
237
+
238
+
asyncfunction getSiteTitle() {
239
+
// Simulate a slow database or API call
240
+
awaitsetTimeout(1000) // from 'node:timers/promises'
241
+
return'My Website'
242
+
}
236
243
237
244
exportasyncfunction CachedWrapper({ children }: { children:ReactNode }) {
238
245
'use cache'
246
+
const title =awaitgetSiteTitle()
247
+
239
248
// Don't introspect children, just pass it through
240
249
return (
241
250
<divclassName="wrapper">
242
-
<header>Cached Header</header>
251
+
<h1>{title}</h1>
243
252
{children}
244
253
</div>
245
254
)
246
255
}
247
256
```
248
257
249
258
```jsx filename="app/cached-wrapper.js" switcher
259
+
import { setTimeout } from'node:timers/promises'
260
+
261
+
asyncfunctiongetSiteTitle() {
262
+
// Simulate a slow database or API call
263
+
awaitsetTimeout(1000) // from 'node:timers/promises'
264
+
return'My Website'
265
+
}
266
+
250
267
exportasyncfunctionCachedWrapper({ children }) {
251
268
'use cache'
269
+
consttitle=awaitgetSiteTitle()
270
+
252
271
// Don't introspect children, just pass it through
253
272
return (
254
273
<div className="wrapper">
255
-
<header>Cached Header</header>
274
+
<h1>{title}</h1>
256
275
{children}
257
276
</div>
258
277
)
259
278
}
260
279
```
261
280
262
-
#### Avoid passing dynamic inputs
263
-
264
-
You must not pass dynamic or runtime data into `use cache` functions unless you avoid introspecting them. Passing values from `cookies()`, `headers()`, or other runtime APIs as arguments will cause errors, as the cache key cannot be determined at pre-render time.
265
-
266
281
### Tagging and revalidating
267
282
268
283
Tag cached data with [`cacheTag`](/docs/app/api-reference/functions/cacheTag) and revalidate it after mutations using [`updateTag`](/docs/app/api-reference/functions/updateTag) in Server Actions for immediate updates, or [`revalidateTag`](/docs/app/api-reference/functions/revalidateTag) delay in updates are acceptable.
@@ -521,7 +536,7 @@ export default function Page() {
521
536
522
537
### Passing dynamic props
523
538
524
-
Components only opt into dynamic rendering when the value is accessed. For example, if you are reading `searchParams` from a `<Page />` component, you can forward this value to another component as a prop:
539
+
Components that access runtime values like `cookies` or `searchParams` cannot be prerendered. To prerender more of a page'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:
Copy file name to clipboardExpand all lines: docs/01-app/01-getting-started/07-fetching-data.mdx
+14-19Lines changed: 14 additions & 19 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -18,10 +18,11 @@ This page will walk you through how you can fetch data in [Server and Client Com
18
18
19
19
### Server Components
20
20
21
-
You can fetch data in Server Components using:
21
+
You can fetch data in Server Components using any asynchronous I/O, such as:
22
22
23
23
1. The [`fetch` API](#with-the-fetch-api)
24
24
2. An [ORM or database](#with-an-orm-or-database)
25
+
3. Reading from the filesystem using Node.js APIs like `fs`
25
26
26
27
#### With the `fetch` API
27
28
@@ -285,7 +286,7 @@ To improve the initial load time and user experience, you can use streaming to b
285
286
height="785"
286
287
/>
287
288
288
-
There are two ways you can implement streaming in your application:
289
+
There are two ways you can leverage streaming in your application:
289
290
290
291
1. Wrapping a page with a [`loading.js` file](#with-loadingjs)
291
292
2. Wrapping a component with [`<Suspense>`](#with-suspense)
@@ -356,7 +357,7 @@ export default function BlogPage() {
356
357
<p>Read the latest posts below.</p>
357
358
</header>
358
359
<main>
359
-
{/*Any content wrapped in a <Suspense> boundarywill be streamed */}
360
+
{/*If there's any dynamic content inside this boundary, it will be streamed in*/}
360
361
<Suspensefallback={<BlogListSkeleton />}>
361
362
<BlogList />
362
363
</Suspense>
@@ -380,7 +381,7 @@ export default function BlogPage() {
380
381
<p>Read the latest posts below.</p>
381
382
</header>
382
383
<main>
383
-
{/*Any content wrapped in a <Suspense> boundarywill be streamed */}
384
+
{/*If there's any dynamic content inside this boundary, it will be streamed in*/}
384
385
<Suspense fallback={<BlogListSkeleton />}>
385
386
<BlogList />
386
387
</Suspense>
@@ -400,19 +401,9 @@ In development, you can preview and inspect the loading state of your components
400
401
401
402
### Sequential data fetching
402
403
403
-
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.
404
+
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.
414
-
415
-
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:
406
+
For example, `<Playlists>` can only fetch data after `<Artist>` completes because it needs the `artistID`:
@@ -482,7 +473,9 @@ async function Playlists({ artistID }) {
482
473
}
483
474
```
484
475
485
-
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.
476
+
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.
477
+
478
+
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.
486
479
487
480
### Parallel data fetching
488
481
@@ -597,11 +590,12 @@ export default async function Page({
597
590
returnisAvailable? <Itemid={id} /> :null
598
591
}
599
592
600
-
exportconst preload = (id:string) => {
593
+
const preload = (id:string) => {
601
594
// void evaluates the given expression and returns undefined
Copy file name to clipboardExpand all lines: docs/01-app/01-getting-started/08-updating-data.mdx
+32Lines changed: 32 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -328,6 +328,38 @@ export function Button() {
328
328
}
329
329
```
330
330
331
+
### Refreshing
332
+
333
+
After a mutation, you may want to refresh the current page to show the latest data. You can do this by calling [`refresh`](/docs/app/api-reference/functions/refresh) from `next/cache` in a Server Action:
This refreshes the client router, ensuring the UI reflects the latest state. The `refresh()` function does not revalidate tagged data. To revalidate tagged data, use [`updateTag`](/docs/app/api-reference/functions/updateTag) or [`revalidateTag`](/docs/app/api-reference/functions/revalidateTag) instead.
362
+
331
363
### Revalidating
332
364
333
365
After performing an update, you can revalidate the Next.js cache and show the updated data by calling [`revalidatePath`](/docs/app/api-reference/functions/revalidatePath) or [`revalidateTag`](/docs/app/api-reference/functions/revalidateTag) within the Server Function:
Copy file name to clipboardExpand all lines: docs/01-app/03-api-reference/06-cli/next.mdx
+1-1Lines changed: 1 addition & 1 deletion
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -149,7 +149,7 @@ The following options are available for the `next telemetry` command:
149
149
150
150
Learn more about [Telemetry](/telemetry).
151
151
152
-
### `next typegen`Options
152
+
### `next typegen`options
153
153
154
154
`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.
0 commit comments