Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Docs: Next.js 15 updates #65603

Merged
merged 70 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from 17 commits
Commits
Show all changes
70 commits
Select commit Hold shift + click to select a range
a64971c
Add PPR page
delbaoliveira May 9, 2024
a03ba9b
Reorder rendering section
delbaoliveira May 9, 2024
f02dbf9
Clean up, use consistent `experimental` titles
delbaoliveira May 9, 2024
8f09765
Document experimental_ppr
delbaoliveira May 9, 2024
f8d7b3b
Update ppr next.config.js page
delbaoliveira May 9, 2024
79b8b2b
Document unstable_after
delbaoliveira May 9, 2024
513c610
Add package bundling page
delbaoliveira May 10, 2024
66db9a2
Clean up, fix broken links
delbaoliveira May 10, 2024
c8da54f
Broken links
delbaoliveira May 10, 2024
20a8384
Missing p
delbaoliveira May 10, 2024
1748791
Fix links
delbaoliveira May 10, 2024
afb0d5d
Fix server actions example
delbaoliveira May 10, 2024
fae8c7e
Update mentions of `useFormStatus`
delbaoliveira May 10, 2024
827bce7
Review useOptimistic example
delbaoliveira May 10, 2024
13e7542
Review server actions page
delbaoliveira May 10, 2024
365fec4
Apply suggestions from code review
delbaoliveira May 10, 2024
abe69e2
Update docs/02-app/01-building-your-application/03-rendering/01-parti…
delbaoliveira May 10, 2024
75b92d7
`fetch` rework
delbaoliveira May 14, 2024
81eb834
Revamp caching and revalidating sections
delbaoliveira May 14, 2024
37e5f4c
Merge branch 'canary' into docs-iusd
manovotny May 20, 2024
68e6e55
Add React Compiler docs
delbaoliveira May 21, 2024
1b4b2a4
Apply suggestions from code review
delbaoliveira May 21, 2024
db58642
Apply package bundling feedback, add version history
delbaoliveira May 21, 2024
4b4a7b3
Merge branch 'docs-iusd' of https://github.com/vercel/next.js into do…
delbaoliveira May 21, 2024
161ed38
Add v15 upgrade guide (wip)
delbaoliveira May 21, 2024
a4ada6a
PPR polish
delbaoliveira May 21, 2024
cf6dc83
Update fetch api docs
delbaoliveira May 21, 2024
fd0dc91
Revamp data fetching section (wip)
delbaoliveira May 21, 2024
327b1bc
update Data Cache and Client-side Router cache doc section
StephDietz May 21, 2024
6d212a5
Update docs/02-app/02-api-reference/04-functions/unstable_after.mdx
anthonyshew May 21, 2024
61e7a2d
Update docs/02-app/01-building-your-application/02-data-fetching/01-f…
anthonyshew May 21, 2024
0c68ef0
Update docs/02-app/01-building-your-application/02-data-fetching/01-f…
anthonyshew May 21, 2024
57f3689
update route handler docs
StephDietz May 21, 2024
3097485
Update docs/02-app/01-building-your-application/02-data-fetching/01-f…
anthonyshew May 21, 2024
e266fd7
Update docs/02-app/01-building-your-application/02-data-fetching/01-f…
anthonyshew May 21, 2024
b20a69d
Update docs/02-app/01-building-your-application/02-data-fetching/01-f…
anthonyshew May 21, 2024
7457a83
Merge branch 'docs-iusd' of https://github.com/vercel/next.js into do…
StephDietz May 21, 2024
d4f7f83
First batch of broken links.
anthonyshew May 21, 2024
f360451
Merge branch 'canary' into docs-iusd
StephDietz May 21, 2024
13617ae
Merge branch 'docs-iusd' of https://github.com/vercel/next.js into do…
anthonyshew May 21, 2024
1810717
First batch of broken links.
anthonyshew May 21, 2024
b540554
Undo sanity example.
anthonyshew May 21, 2024
642aaba
Second batch of broken links.
anthonyshew May 21, 2024
00a170d
More broken links.
anthonyshew May 21, 2024
96dcc67
Testing link...
anthonyshew May 21, 2024
ccce593
Bring it back.
anthonyshew May 21, 2024
4b652ea
Move it to separate lines?
anthonyshew May 21, 2024
fa08dbb
Maybe...?
anthonyshew May 21, 2024
7d36013
This is right?
anthonyshew May 21, 2024
2c04502
lmao
anthonyshew May 21, 2024
8a32697
Update docs/02-app/02-api-reference/05-next-config-js/ppr.mdx
manovotny May 22, 2024
4ae5aa2
Merge branch 'canary' into docs-iusd
delbaoliveira May 22, 2024
2876f00
Review route handlers docs
delbaoliveira May 22, 2024
9d11925
Rework client-side router cache docs
delbaoliveira May 22, 2024
2299916
Add note for fetchCache = 'default-cache' for root layout
delbaoliveira May 22, 2024
0e7e013
fix `after` import
ztanner May 22, 2024
641f705
Condense good to know
manovotny May 23, 2024
03f91f4
Apply suggestions from code review
manovotny May 23, 2024
4434f72
Update docs/02-app/01-building-your-application/02-data-fetching/03-s…
manovotny May 23, 2024
e442464
Split package.json and gitignore sentenses
manovotny May 23, 2024
85f45d6
Adds Tailwind globals
manovotny May 23, 2024
b4f89c6
reword
manovotny May 23, 2024
378ae0b
React compiler
manovotny May 23, 2024
1531fe9
Valid root layout
manovotny May 23, 2024
17720eb
Explicit update commands
manovotny May 23, 2024
d4b0f85
Update upgrade guide and metadata
delbaoliveira May 23, 2024
78b0471
speed insights
manovotny May 23, 2024
f840fc0
Update rc command
manovotny May 23, 2024
0c8fc2f
fix "before" blocks for serverComponentsExternalPackages & bundlePage…
ztanner May 23, 2024
4007608
lint
ztanner May 23, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
---
title: Partial Prerendering (Experimental)
description: Learn how to combine the benefits of static and dynamic rendering with Partial Prerendering.
---

> Partial Prerendering is an **experimental** feature and subject to change.

Partial Prerendering (PPR) is a rendering strategy that combines the benefits of static and dynamic rendering on the same route.

Next.js currently defaults to static rendering unless you use [dynamic functions](/docs/app/building-your-application/rendering/server-components#dynamic-functions). These APIs opt a whole route into dynamic rendering.
delbaoliveira marked this conversation as resolved.
Show resolved Hide resolved

With PPR, you can wrap any dynamic UI in a [Suspense](https://react.dev/reference/react/Suspense) boundary. When a new request comes in, Next.js will immediately serve a static HTML shell from the cache, then render and [stream](/docs/app/building-your-application/rendering/server-components#streaming) the dynamic parts in the same HTTP request.

<Image
alt="Partially Prerendered Product Page showing static nav and product information, and dynamic cart and recommended products"
srcLight="/learn/light/thinking-in-ppr.png"
srcDark="/learn/dark/thinking-in-ppr.png"
width="1600"
height="632"
/>

## Incremental Adoption (Version 15)

In Next.js 15, you can incrementally adopt Partial Prerendering in [layouts](/docs/app/building-your-application/routing/layouts-and-templates) and [pages](/docs/app/building-your-application/routing/pages) by setting the [`ppr`](/docs/app/api-reference/next-config-js/ppr) `next.config.js` option to `incremental`, and exporting the `experimental_ppr` [route config option](/docs/app/api-reference/file-conventions/route-segment-config) at the top of the file:
manovotny marked this conversation as resolved.
Show resolved Hide resolved
manovotny marked this conversation as resolved.
Show resolved Hide resolved

```js filename="next.config.js"
const nextConfig = {
experimental: {
ppr: 'incremental',
},
}

module.exports = nextConfig
```

```tsx filename="app/page.tsx" switcher
import { Suspense } from "react"
import { StaticComponent, DynamicComponent, Fallback } from "@/app/ui"

export const experimental_ppr = true

export default function Page() {
return {
<>
<StaticComponent />
<Suspense fallback={<Fallback />}>
<DynamicComponent />
</Suspense>
</>
};
}
```

```jsx filename="app/page.js" switcher
import { Suspense } from "react"
import { StaticComponent, DynamicComponent, Fallback } from "@/app/ui"

export const experimental_ppr = true

export default function Page() {
return {
<>
<StaticComponent />
<Suspense fallback={<Fallback />}>
<DynamicComponent />
</Suspense>
</>
};
}
```

> **Good to know**:
>
> - Routes that don't have `experimental_ppr` will default to `false` and will not be prerendered using PPR. You need to explicitly opt-in to PPR for each route.
> - `experimental_ppr` will apply to all children of the route segment, including nested layouts and pages. You don't have to add it to every file, only the top segment of a route.
> - To disable PPR for children segments, you can set `experimental_ppr` to `false` in the child segment.

## Enabling PPR (Version 14)

For version 14, you can enable it by adding the [`ppr`](/docs/app/api-reference/next-config-js/ppr) option to your `next.config.js` file. This will apply to all routes in your application:

```js filename="next.config.js"
const nextConfig = {
experimental: {
ppr: true,
},
}

module.exports = nextConfig
```
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,13 @@ As a developer, you do not need to choose between static and dynamic rendering a

#### Dynamic Functions

Dynamic functions rely on information that can only be known at request time such as a user's cookies, current requests headers, or the URL's search params. In Next.js, these dynamic functions are:
Dynamic functions rely on information that can only be known at request time such as a user's cookies, current requests headers, or the URL's search params. In Next.js, these dynamic APIs are:

- **[`cookies()`](/docs/app/api-reference/functions/cookies) and [`headers()`](/docs/app/api-reference/functions/headers)**: Using these in a Server Component will opt the whole route into dynamic rendering at request time.
- **[`searchParams`](/docs/app/api-reference/file-conventions/page#searchparams-optional)**: Using the `searchParams` prop on a [Page](/docs/app/api-reference/file-conventions/page) will opt the page into dynamic rendering at request time.
- [`cookies()`](/docs/app/api-reference/functions/cookies)
- [`headers()`](/docs/app/api-reference/functions/headers)
- [`unstable_noStore()`](/docs/app/api-reference/functions/unstable_noStore)
- [`unstable_after()`](/docs/app/api-reference/functions/unstable_after):
- [`searchParams` prop](/docs/app/api-reference/file-conventions/page#searchparams-optional)

Using any of these functions will opt the whole route into dynamic rendering at request time.

Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
---
title: Optimizing Package Bundling
nav_title: Package Bundling
description: Learn how to optimize your application's server and client bundles.
related:
description: Learn more about optimizing your application for production.
links:
- app/building-your-application/deploying/production-checklist
---

Bundling external packages can significantly improve the performance of your application. <AppOnly>By default, packages imported inside Server Components and Route Handlers are automatically bundled by Next.js. This page will guide you through how to analyze and further optimize package bundling.</AppOnly> <PagesOnly>By default, packages imported into your application are not bundled. This can impact performance or might not work if external packages are not pre-bundled, for example, if imported from a monorepo or `node_modules`. This page will guide you through how to analyze and configure package bundling.</PagesOnly>

## Analyzing JavaScript bundles

[`@next/bundle-analyzer`](https://www.npmjs.com/package/@next/bundle-analyzer) is a plugin for Next.js that helps you manage the size of your application bundles. It generates a visual report of the size of each package and their dependencies. You can use the information to remove large dependencies, split, or [lazy-load](/docs/app/building-your-application/optimizing/lazy-loading) your code.

### Installation

Install the plugin by running the following command:

```bash
npm i @next/bundle-analyzer
# or
yarn add @next/bundle-analyzer
# or
pnpm add @next/bundle-analyzer
```

Then, add the bundle analyzer's settings to your `next.config.js`.

```js filename="next.config.js"
/** @type {import('next').NextConfig} */
const nextConfig = {}

const withBundleAnalyzer = require('@next/bundle-analyzer')()

module.exports =
process.env.ANALYZE === 'true' ? withBundleAnalyzer(nextConfig) : nextConfig
delbaoliveira marked this conversation as resolved.
Show resolved Hide resolved
```

### Generating a report

Run the following command to analyze your bundles:

```bash
ANALYZE=true npm run build
# or
ANALYZE=true yarn build
# or
ANALYZE=true pnpm build
```

The report will open three new tabs in your browser, which you can inspect. Periodically evaluating your application's bundles can help you maintain application performance over time.

## Optimizing package imports

Some packages, such as icon libraries, can export hundreds of modules, which can cause performance issues in development and production.

You can optimize how these packages are imported by adding the [`optimizePackageImports`](/docs/app/api-reference/next-config-js/optimizePackageImports) option to your `next.config.js`. This option will only load the modules you _actually_ use, while still giving you the convenience of writing import statements with many named exports.

```js filename="next.config.js"
/** @type {import('next').NextConfig} */
const nextConfig = {
experimental: {
optimizePackageImports: ['icon-library'],
},
}

module.exports = nextConfig
```

Next.js also optimizes some libraries automatically. See the [full list](https://nextjs.org/docs/app/api-reference/next-config-js/optimizePackageImports)
delbaoliveira marked this conversation as resolved.
Show resolved Hide resolved

<PagesOnly>

## Bundling specific packages

To bundle specific packages, you can use the [`transpilePackages`](/docs/app/api-reference/next-config-js/transpilePackages) option in your `next.config.js`. This option is useful for bundling external packages that are not pre-bundled, for example, in a monorepo or imported from `node_modules`.

```js filename="next.config.js"
/** @type {import('next').NextConfig} */
const nextConfig = {
transpilePackages: ['package-name'],
}

module.exports = nextConfig
```

## Bundling all packages

To automatically bundle all packages (default behavior in the App Router), you can use the [`bundlePagesRouterDependencies`](/docs/pages/api-reference/next-config-js/bundlePagesRouterDependencies) option in your `next.config.js`.

```js filename="next.config.js"
/** @type {import('next').NextConfig} */
const nextConfig = {
bundlePagesRouterDependencies: true,
}

module.exports = nextConfig
```

## Opting specific packages out of bundling

If you have the [`bundlePagesRouterDependencies`](/docs/pages/api-reference/next-config-js/bundlePagesRouterDependencies) option enabled, you can opt specific packages out of automatic bundling using the [`serverExternalPackages`](/docs/pages/api-reference/next-config-js/serverExternalPackages) option in your `next.config.js`:

```js filename="next.config.js"
/** @type {import('next').NextConfig} */
const nextConfig = {
// Automatically bundle external packages in the Pages Router:
bundlePagesRouterDependencies: true,
// Opt specific packages out of bundling for both App and Pages Router:
serverExternalPackages: ['package-name'],
}

module.exports = nextConfig
```

</PagesOnly>

<AppOnly>

## Opting specific packages out of bundling

Since packages imported inside Server Components and Route Handlers are automatically bundled by Next.js, you can opt specific packages out of bundling using the [`serverExternalPackages`](/docs/app/api-reference/next-config-js/serverExternalPackages) option in your `next.config.js`.

```js filename="next.config.js"
/** @type {import('next').NextConfig} */
const nextConfig = {
serverExternalPackages: ['package-name'],
}

module.exports = nextConfig
```

delbaoliveira marked this conversation as resolved.
Show resolved Hide resolved
</AppOnly>
delbaoliveira marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,9 @@ description: Optimize the performance of third-party libraries in your applicati

{/* The content of this doc is shared between the app and pages router. You can use the `<PagesOnly>Content</PagesOnly>` component to add content that is specific to the Pages Router. Any shared content should not be wrapped in a component. */}

**`@next/third-parties`** is a library that provides a collection of components and utilities that
improve the performance and developer experience of loading popular third-party libraries in your
Next.js application.
**`@next/third-parties`** is a library that provides a collection of components and utilities that improve the performance and developer experience of loading popular third-party libraries in your Next.js application.

All third-party integrations provided by `@next/third-parties` have been optimized for performance
and ease of use.
All third-party integrations provided by `@next/third-parties` have been optimized for performance and ease of use.

## Getting Started

Expand All @@ -30,14 +27,11 @@ All supported third-party libraries from Google can be imported from `@next/thir

### Google Tag Manager

The `GoogleTagManager` component can be used to instantiate a [Google Tag
Manager](https://developers.google.com/tag-platform/tag-manager) container to your
page. By default, it fetches the original inline script after hydration occurs on the page.
The `GoogleTagManager` component can be used to instantiate a [Google Tag Manager](https://developers.google.com/tag-platform/tag-manager) container to your page. By default, it fetches the original inline script after hydration occurs on the page.

<AppOnly>

To load Google Tag Manager for all routes, include the component directly in your root layout and
pass in your GTM container ID:
To load Google Tag Manager for all routes, include the component directly in your root layout and pass in your GTM container ID:

```tsx filename="app/layout.tsx" switcher
import { GoogleTagManager } from '@next/third-parties/google'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ Let's explore some strategies and techniques to optimize memory and address comm

Applications with a large amount of dependencies will use more memory.

The [Bundle Analyzer](/docs/app/building-your-application/optimizing/bundle-analyzer) can help you investigate large dependencies in your application that may be able to be removed to improve performance and memory usage.
The [Bundle Analyzer](/docs/app/building-your-application/optimizing/package-bundling#analyzing-javascript-bundles) can help you investigate large dependencies in your application that may be able to be removed to improve performance and memory usage.

## Run `next build` with `--experimental-debug-memory-usage`

Expand Down