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: apps/docs/content/en/docs/01-app/01-getting-started/08-partial-prerendering.mdx
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -34,7 +34,7 @@ To understand Partial Prerendering, it helps to be familiar with the rendering s
34
34
35
35
### Static Rendering
36
36
37
-
With Static Rendering, HTML is generated ahead of time—either at build time or through [revalidation](/docs/app/building-your-application/data-fetching/incremental-static-regeneration). The result is cached and shared across users and requests.
37
+
With Static Rendering, HTML is generated ahead of time—either at build time or through [revalidation](/docs/app/guides/incremental-static-regeneration). The result is cached and shared across users and requests.
38
38
39
39
In Partial Prerendering, Next.js prerenders a **static shell** for a route. This can include the layout and any other components that don't depend on request-time data.
Copy file name to clipboardExpand all lines: apps/docs/content/en/docs/01-app/01-getting-started/09-fetching-data.mdx
+272-1Lines changed: 272 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -8,9 +8,11 @@ related:
8
8
links:
9
9
- app/api-reference/functions/fetch
10
10
- app/api-reference/file-conventions/loading
11
+
- app/api-reference/config/next-config-js/logging
12
+
- app/api-reference/config/next-config-js/taint
11
13
---
12
14
13
-
This page will walk you through how you can fetch data in [Server Components](#server-components)and [Client Components](#client-components). As well as how to [stream](#streaming)content that depends on data.
15
+
This page will walk you through how you can fetch data in [Server and Client Components](/docs/app/getting-started/server-and-client-components), and how to [stream](#streaming)components that depend on data.
14
16
15
17
## Fetching data
16
18
@@ -53,6 +55,11 @@ export default async function Page() {
53
55
}
54
56
```
55
57
58
+
> **Good to know:**
59
+
>
60
+
> -`fetch` responses are not cached by default. However, Next.js will [prerender](/docs/app/getting-started/partial-prerendering#static-rendering) the route and the output will be cached for improved performance. If you'd like to opt into [dynamic rendering](/docs/app/getting-started/partial-prerendering#dynamic-rendering), use the `{ cache: 'no-store' }` option. See the [`fetch` API Reference](/docs/app/api-reference/functions/fetch).
61
+
> - During development, you can log `fetch` calls for better visibility and debugging. See the [`logging` API reference](/docs/app/api-reference/config/next-config-js/logging).
62
+
56
63
#### With an ORM or database
57
64
58
65
Since Server Components are rendered on the server, you can safely make database queries using an ORM or database client. Turn your component into an asynchronous function, and await the call:
@@ -203,6 +210,7 @@ export default function BlogPage() {
@@ -352,3 +360,266 @@ export default function BlogPage() {
352
360
An instant loading state is fallback UI that is shown immediately to the user after navigation. For the best user experience, we recommend designing loading states that are meaningful and help users understand the app is responding. For example, you can use skeletons and spinners, or a small but meaningful part of future screens such as a cover photo, title, etc.
353
361
354
362
In development, you can preview and inspect the loading state of your components using the [React Devtools](https://react.dev/learn/react-developer-tools).
363
+
364
+
## Examples
365
+
366
+
### Sequential data fetching
367
+
368
+
Sequential data fetching happens when nested components in a tree each fetch their own data and the requests are not [deduplicated](/docs/app/deep-dive/caching#request-memoization), leading to longer response times.
There may be cases where you want this pattern because one fetch depends on the result of the other.
379
+
380
+
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:
{/* Show fallback UI while the Playlists component is loading */}
428
+
<Suspense fallback={<div>Loading...</div>}>
429
+
{/* Pass the artist ID to the Playlists component */}
430
+
<Playlists artistID={artist.id} />
431
+
</Suspense>
432
+
</>
433
+
)
434
+
}
435
+
436
+
asyncfunctionPlaylists({ artistID }) {
437
+
// Use the artist ID to fetch playlists
438
+
constplaylists=awaitgetArtistPlaylists(artistID)
439
+
440
+
return (
441
+
<ul>
442
+
{playlists.map((playlist) => (
443
+
<li key={playlist.id}>{playlist.name}</li>
444
+
))}
445
+
</ul>
446
+
)
447
+
}
448
+
```
449
+
450
+
To improve the user experience, you should use [React `<Suspense>`](/docs/app/building-your-application/routing/loading-ui-and-streaming#streaming-with-suspense) 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.
451
+
452
+
### Parallel data fetching
453
+
454
+
Parallel data fetching happens when data requests in a route are eagerly initiated and start at the same time.
455
+
456
+
By default, [layouts and pages](/docs/app/getting-started/layouts-and-pages) are rendered in parallel. So each segment starts fetching data as soon as possible.
457
+
458
+
However, within _any_ component, multiple `async`/`await` requests can still be sequential if placed after the other. For example, `getAlbums` will be blocked until `getArtist` is resolved:
You can initiate requests in parallel by defining them outside the components that use the data, and resolving them together, for example, with [`Promise.all`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/all):
> **Good to know:** If one request fails when using `Promise.all`, the entire operation will fail. To handle this, you can use the [`Promise.allSettled`](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Promise/allSettled) method instead.
539
+
540
+
### Preloading data
541
+
542
+
You can preload data by creating an utility function that you eagerly call above blocking requests. `<Item>` conditionally renders based on the `checkIsAvailable()` function.
543
+
544
+
You can call `preload()` before `checkIsAvailable()` to eagerly initiate `<Item/>` data dependencies. By the time `<Item/>` is rendered, its data has already been fetched.
545
+
546
+
```tsx filename="app/item/[id]/page.tsx" switcher
547
+
import { getItem } from'@/lib/data'
548
+
549
+
exportdefaultasyncfunction Page({
550
+
params,
551
+
}: {
552
+
params:Promise<{ id:string }>
553
+
}) {
554
+
const { id } =awaitparams
555
+
// starting loading item data
556
+
preload(id)
557
+
// perform another asynchronous task
558
+
const isAvailable =awaitcheckIsAvailable()
559
+
560
+
returnisAvailable? <Itemid={id} /> :null
561
+
}
562
+
563
+
exportconst preload = (id:string) => {
564
+
// void evaluates the given expression and returns undefined
Additionally, you can use React's [`cache` function](https://react.dev/reference/react/cache) and the [`server-only` package](https://www.npmjs.com/package/server-only) to create a reusable utility function. This approach allows you to cache the data fetching function and ensure that it's only executed on the server.
Copy file name to clipboardExpand all lines: apps/docs/content/en/docs/01-app/01-getting-started/11-error-handling.mdx
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -274,7 +274,7 @@ Errors will bubble up to the nearest parent error boundary. This allows for gran
274
274
275
275
### Global errors
276
276
277
-
While less common, you can handle errors in the root layout using the [`global-error.js`](/docs/app/api-reference/file-conventions/error#global-error) file, located in the root app directory, even when leveraging [internationalization](/docs/app/building-your-application/routing/internationalization). Global error UI must define its own `<html>` and `<body>` tags, since it is replacing the root layout or template when active.
277
+
While less common, you can handle errors in the root layout using the [`global-error.js`](/docs/app/api-reference/file-conventions/error#global-error) file, located in the root app directory, even when leveraging [internationalization](/docs/app/guides/internationalization). Global error UI must define its own `<html>` and `<body>` tags, since it is replacing the root layout or template when active.
278
278
279
279
```tsx filename="app/global-error.tsx" switcher
280
280
'use client'// Error boundaries must be Client Components
Copy file name to clipboardExpand all lines: apps/docs/content/en/docs/01-app/02-guides/index.mdx
+4-4Lines changed: 4 additions & 4 deletions
Original file line number
Diff line number
Diff line change
@@ -5,15 +5,15 @@ description: Learn how to implement common UI patterns and use cases using Next.
5
5
6
6
### Data Fetching
7
7
8
-
-[Using the `fetch` API](/docs/app/building-your-application/data-fetching/fetching#fetching-data-on-the-server-with-the-fetch-api)
9
-
-[Using an ORM or database client](/docs/app/building-your-application/data-fetching/fetching#fetching-data-on-the-server-with-an-orm-or-database)
8
+
-[Using the `fetch` API](/docs/app/getting-started/fetching-data#with-the-fetch-api)
9
+
-[Using an ORM or database client](/docs/app/getting-started/fetching-data#with-an-orm-or-database)
10
10
-[Reading search params on the server](/docs/app/api-reference/file-conventions/page)
11
11
-[Reading search params on the client](/docs/app/api-reference/functions/use-search-params)
12
12
13
13
### Revalidating Data
14
14
15
-
-[Using ISR to revalidate data after a certain time](/docs/app/building-your-application/data-fetching/incremental-static-regeneration#time-based-revalidation)
16
-
-[Using ISR to revalidate data on-demand](/docs/app/building-your-application/data-fetching/incremental-static-regeneration#on-demand-revalidation-with-revalidatepath)
15
+
-[Using ISR to revalidate data after a certain time](/docs/app/guides/incremental-static-regeneration#time-based-revalidation)
16
+
-[Using ISR to revalidate data on-demand](/docs/app/guides/incremental-static-regeneration#on-demand-revalidation-with-revalidatepath)
Because all layouts and pages in the `app/` directory default to [Server Components](/docs/app/getting-started/server-and-client-components), we do not need to worry about the size of the translation files affecting our client-side JavaScript bundle size. This code will **only run on the server**, and only the resulting HTML will be sent to the browser.
170
170
171
-
## Static Generation
171
+
## Static Rendering
172
172
173
173
To generate static routes for a given set of locales, we can use `generateStaticParams` with any page or layout. This can be global, for example, in the root layout:
Copy file name to clipboardExpand all lines: apps/docs/content/en/docs/01-app/02-guides/migrating/app-router-migration.mdx
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -470,7 +470,7 @@ export default function ExampleClientComponent() {
470
470
In addition, the new `useRouter` hook has the following changes:
471
471
472
472
-`isFallback` has been removed because `fallback` has [been replaced](#replacing-fallback).
473
-
- The `locale`, `locales`, `defaultLocales`, `domainLocales` values have been removed because built-in i18n Next.js features are no longer necessary in the `app` directory. [Learn more about i18n](/docs/app/building-your-application/routing/internationalization).
473
+
- The `locale`, `locales`, `defaultLocales`, `domainLocales` values have been removed because built-in i18n Next.js features are no longer necessary in the `app` directory. [Learn more about i18n](/docs/app/guides/internationalization).
474
474
-`basePath` has been removed. The alternative will not be part of `useRouter`. It has not yet been implemented.
475
475
-`asPath` has been removed because the concept of `as` has been removed from the new router.
476
476
-`isReady` has been removed because it is no longer necessary. During [static rendering](/docs/app/getting-started/partial-prerendering#static-rendering), any component that uses the [`useSearchParams()`](/docs/app/api-reference/functions/use-search-params) hook will skip the prerendering step and instead be rendered on the client at runtime.
Copy file name to clipboardExpand all lines: apps/docs/content/en/docs/01-app/02-guides/migrating/from-create-react-app.mdx
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -39,7 +39,7 @@ Depending on your needs, Next.js allows you to choose your data fetching strateg
39
39
40
40
### Middleware
41
41
42
-
[Next.js Middleware](/docs/app/building-your-application/routing/middleware) allows you to run code on the server before a request is completed. For instance, you can avoid a flash of unauthenticated content by redirecting a user to a login page in the middleware for authenticated-only pages. You can also use it for features like A/B testing, experimentation, and [internationalization](/docs/app/building-your-application/routing/internationalization).
42
+
[Next.js Middleware](/docs/app/building-your-application/routing/middleware) allows you to run code on the server before a request is completed. For instance, you can avoid a flash of unauthenticated content by redirecting a user to a login page in the middleware for authenticated-only pages. You can also use it for features like A/B testing, experimentation, and [internationalization](/docs/app/guides/internationalization).
Copy file name to clipboardExpand all lines: apps/docs/content/en/docs/01-app/02-guides/migrating/from-vite.mdx
+1-1Lines changed: 1 addition & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -39,7 +39,7 @@ Depending on your needs, Next.js allows you to choose your data fetching strateg
39
39
40
40
### Middleware
41
41
42
-
[Next.js Middleware](/docs/app/building-your-application/routing/middleware) allows you to run code on the server before a request is completed. This is especially useful to avoid having a flash of unauthenticated content when the user visits an authenticated-only page by redirecting the user to a login page. The middleware is also useful for experimentation and [internationalization](/docs/app/building-your-application/routing/internationalization).
42
+
[Next.js Middleware](/docs/app/building-your-application/routing/middleware) allows you to run code on the server before a request is completed. This is especially useful to avoid having a flash of unauthenticated content when the user visits an authenticated-only page by redirecting the user to a login page. The middleware is also useful for experimentation and [internationalization](/docs/app/guides/internationalization).
0 commit comments