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: Add "Going to production" page for App Router #59304

Merged
merged 111 commits into from
Jan 5, 2024
Merged
Show file tree
Hide file tree
Changes from 44 commits
Commits
Show all changes
111 commits
Select commit Hold shift + click to select a range
affef87
new going to production docs page
Dec 5, 2023
6190ada
Merge branch 'canary' into docs-app-router-production-page
StephDietz Dec 5, 2023
9524d80
update caching section
Dec 5, 2023
a6c9eb7
update javascript section
Dec 5, 2023
b9ac834
error section update
Dec 5, 2023
67b2847
remove sentry section
Dec 5, 2023
3a8174e
switch order of deploying chapters
Dec 5, 2023
de1350a
update links
Dec 5, 2023
be0a117
final edits
Dec 5, 2023
2096af6
remove unused code block
Dec 5, 2023
e684b49
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
04ca7d0
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
057843b
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
f67888b
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
13471bd
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
031e69a
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
8d6546a
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
49e0434
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
d28b81e
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
d7d8422
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
b818c97
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
c65be43
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
7cb9e0c
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
cbbd380
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
2316bce
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
7f29111
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
dd5692d
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
ea54a32
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
5fb7dae
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
fcd6f43
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
2053f33
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
00426f3
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
0de09d3
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
139dd36
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
ebd0bcc
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
ee3db48
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
592b6fd
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
9a38553
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 6, 2023
6abc7fc
edits
Dec 6, 2023
4cad589
merge
Dec 6, 2023
5286c5a
clarify the server actions caching section
Dec 6, 2023
d05ec25
add two more points to recommended optimization strategies
Dec 6, 2023
7602370
update logging info
Dec 6, 2023
da50c59
update loading section
Dec 6, 2023
0f28947
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 11, 2023
18c6bbe
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 11, 2023
9ef203c
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 11, 2023
fb1506b
Merge branch 'canary' into docs-app-router-production-page
StephDietz Dec 11, 2023
b1df9a3
resolve comments left by tim
Dec 11, 2023
7cd2e99
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 15, 2023
807d4d1
resolve Jimmy's feedback
Dec 15, 2023
5b28da0
Merge branch 'canary' into docs-app-router-production-page
StephDietz Dec 15, 2023
6ab67f7
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 15, 2023
db75d6b
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 15, 2023
7bdcd41
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 15, 2023
bdd64b4
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 15, 2023
6d84873
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 15, 2023
0730e46
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 15, 2023
0a8afe3
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 15, 2023
289286f
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 15, 2023
9555bc7
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 15, 2023
b53d7cb
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 15, 2023
a6cf96c
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 15, 2023
7160f85
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 15, 2023
341773c
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 15, 2023
5e218af
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 15, 2023
b342835
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 15, 2023
15d497f
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 15, 2023
205b71b
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 15, 2023
49b4bcb
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 15, 2023
80ee897
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 15, 2023
7142b25
move loading section up and re-order overview bullets
Dec 15, 2023
b554b18
add parallel data fetching
Dec 15, 2023
71f7259
Merge branch 'canary' into docs-app-router-production-page
manovotny Dec 15, 2023
183d06e
Update docs/02-app/01-building-your-application/08-deploying/01-produ…
StephDietz Dec 19, 2023
5fa5e6f
fix file duplicate and link to vercel production checklist
Dec 19, 2023
dcb3241
Merge branch 'canary' into docs-app-router-production-page
delbaoliveira Dec 19, 2023
4edbbe5
Re-order pages
delbaoliveira Dec 19, 2023
a45d971
Add @next/bundle-analyzer page
delbaoliveira Dec 19, 2023
e364bad
Restructure and rewrite (wip)
delbaoliveira Dec 19, 2023
985d131
Move information to the /public folder page
delbaoliveira Dec 19, 2023
7797e5a
Tweak
delbaoliveira Dec 19, 2023
3d95618
Fix broken links
delbaoliveira Dec 19, 2023
ef097e7
Review bundle-analyzer page
delbaoliveira Dec 20, 2023
0194cf0
Review static assets page
delbaoliveira Dec 20, 2023
657160e
Add code-splitting section
delbaoliveira Dec 20, 2023
a3fb1d3
Fix broken hashes
delbaoliveira Dec 20, 2023
a423f29
Update page description
delbaoliveira Dec 20, 2023
ebac057
Fix headings
delbaoliveira Dec 20, 2023
4936fd6
Review intro
delbaoliveira Dec 20, 2023
b29dbce
Review 'automatic optimizations' section
delbaoliveira Dec 20, 2023
8a237e3
Review 'during development' section
delbaoliveira Dec 20, 2023
967038a
Merge branch 'canary' into docs-app-router-production-page
delbaoliveira Dec 21, 2023
52b0855
shipit
delbaoliveira Dec 22, 2023
0e65d69
Fix broken link
delbaoliveira Dec 22, 2023
85e60e1
Merge branch 'canary' into docs-app-router-production-page
delbaoliveira Dec 22, 2023
cdde2f7
Fix headings
delbaoliveira Dec 22, 2023
1cab334
Merge branch 'docs-app-router-production-page' of https://github.com/…
delbaoliveira Dec 22, 2023
0cd08a7
Apply suggestions from code review
delbaoliveira Dec 22, 2023
993c0bd
Apply suggestions from code review
delbaoliveira Dec 22, 2023
c9ee742
Update docs/02-app/01-building-your-application/09-deploying/01-produ…
delbaoliveira Dec 22, 2023
875ae3c
Merge branch 'canary' into docs-app-router-production-page
delbaoliveira Jan 2, 2024
b0afab4
Merge branch 'canary' into docs-app-router-production-page
delbaoliveira Jan 2, 2024
2651278
Apply Lee's feedback
delbaoliveira Jan 2, 2024
8f626d1
Merge branch 'canary' into docs-app-router-production-page
delbaoliveira Jan 2, 2024
aed9a8c
Merge pages and app
delbaoliveira Jan 3, 2024
0a84aeb
Merge branch 'canary' into docs-app-router-production-page
delbaoliveira Jan 3, 2024
56697ee
Merge branch 'canary' into docs-app-router-production-page
delbaoliveira Jan 5, 2024
790f164
Share bundle-analyzer between app and pages
delbaoliveira Jan 5, 2024
a5ca7c3
Review
delbaoliveira Jan 5, 2024
b36dea6
Use searchParams instead of useSearchParams
delbaoliveira Jan 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,139 @@
---
title: Going to Production
description: Before taking your Next.js application to production, here are some recommendations to ensure the best user experience.
---

Before taking your Next.js application to production, here are some recommendations to ensure the best user experience.

## Overview
StephDietz marked this conversation as resolved.
Show resolved Hide resolved
StephDietz marked this conversation as resolved.
Show resolved Hide resolved

- Use [caching](/docs/app/building-your-application/caching) wherever possible.
- Ensure your database and backend are deployed in the same region.
- Aim to reduce client-side JavaScript bundle sizes by [moving the "use client" boundaries down the React component tree](https://nextjs.org/docs/app/building-your-application/rendering/composition-patterns#moving-client-components-down-the-tree).
StephDietz marked this conversation as resolved.
Show resolved Hide resolved
- Prevent sensitive data from being exposed to the client with [tainting](/docs/app/building-your-application/data-fetching/patterns#preventing-sensitive-data-from-being-exposed-to-the-client)
- Ensure errors in nested route segments are handled gracefully with the [error.js file](https://nextjs.org/docs/app/building-your-application/routing/error-handling)
StephDietz marked this conversation as resolved.
Show resolved Hide resolved
- Customize your 404 (Not Found) responses using the [`notfound()`](/docs/app/api-reference/functions/not-found) function and [`not-found.js` file](https://nextjs.org/docs/app/api-reference/file-conventions/not-found)
StephDietz marked this conversation as resolved.
Show resolved Hide resolved
- Ensure you are [measuring performance](/docs/app/building-your-application/optimizing/analytics).
StephDietz marked this conversation as resolved.
Show resolved Hide resolved
- Run [Lighthouse](https://developers.google.com/web/tools/lighthouse) to check for performance, best practices, accessibility, and SEO. For best results, use a production build of Next.js and use incognito in your browser so results aren't affected by extensions.
StephDietz marked this conversation as resolved.
Show resolved Hide resolved
- Review [Supported Browsers and Features](/docs/architecture/supported-browsers).
- Optimize the user experience with built-in components:
- [Images](/docs/app/building-your-application/optimizing/images)
- [Fonts](/docs/app/building-your-application/optimizing/fonts)
- [Scripts](/docs/app/building-your-application/optimizing/scripts)
- [Link](/docs/app/building-your-application/routing/linking-and-navigating)
- Improve [loading performance](#loading-performance)
- Consider adding a [Content Security Policy](/docs/app/building-your-application/configuring/content-security-policy)

## Caching

Caching is essential for improving response times and reducing requests to backend services. The App Router in Next.js offers a robust [caching mechanism](https://nextjs.org/docs/app/building-your-application/caching) that supports static and dynamic rendering, as well as [revalidation](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#revalidating-data).
StephDietz marked this conversation as resolved.
Show resolved Hide resolved

### Static assets

Next.js automatically adds caching headers to immutable assets in **`/public`** or other static directories. Assets like CSS, static images, and other media are ideal candidates for long-term caching as they rarely change. The default caching headers applied are:

```jsx
Cache-Control: public, max-age=31536000, immutable
```

This setup ensures that these assets are cached efficiently, reducing load times for returning users.
StephDietz marked this conversation as resolved.
Show resolved Hide resolved

### Data caching

Next.js's built-in [Data Cache](/docs/app/building-your-application/caching#data-cache) persists results of data fetches across server requests and deployments. By default, fetch requests are cached, allowing data to be reused in subsequent requests. This feature is particularly useful for content that doesn't change frequently.

### Revalidation
StephDietz marked this conversation as resolved.
Show resolved Hide resolved

The caching behavior of data requests can be controlled through various strategies. Next.js supports [time-based revalidation](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#time-based-revalidation) (automatically refreshing the cache after a set period) and [on-demand revalidation](/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#on-demand-revalidation) (refreshing the cache based on specific events).

### Caching in Server Actions
StephDietz marked this conversation as resolved.
Show resolved Hide resolved

While Server Actions in Next.js are primarily used for mutations (like POST, PUT, DELETE operations), they also play a crucial role in cache management. After a mutation, it's crucial to ensure that the cached data reflects the latest state. Next.js facilitates this by providing APIs such as `revalidatePath` and `revalidateTag`. These APIs can be used to purge or revalidate the cache immediately after a mutation. This capability ensures that any changes made by Server Actions are promptly reflected in the cached data, maintaining data consistency and accuracy.
StephDietz marked this conversation as resolved.
Show resolved Hide resolved

### Opting Out of Caching
StephDietz marked this conversation as resolved.
Show resolved Hide resolved

There are scenarios where caching might not be desirable as users need the most up-to-date content. Next.js allows [opting out of the Data Cache](/docs/app/building-your-application/caching#opting-out-1) for specific fetch requests. This can be achieved with settings such as `cache: 'no-store'` or `revalidate: 0`.

## Reducing JavaScript bundle sizes
StephDietz marked this conversation as resolved.
Show resolved Hide resolved

Making sure your website sends less JavaScript to the browser is important for speed. To help you understand and manage your JavaScript bundles, you can use these tools to visualize your bundles and/or identify large dependencies.
StephDietz marked this conversation as resolved.
Show resolved Hide resolved

- [Import Cost](https://marketplace.visualstudio.com/items?itemName=wix.vscode-import-cost) – Shows the size of each package you import right in VSCode.
- [Package Phobia](https://packagephobia.com/) – Helps you see the size impact of adding new dev dependencies to your project.
- [Bundle Phobia](https://bundlephobia.com/) - Tells you how much a new dependency could increase your JavaScript size.
- [Webpack Bundle Analyzer](https://github.com/vercel/next.js/tree/canary/packages/next-bundle-analyzer) – Creates a visual, interactive map of your webpack output files to show where space is used.
- [bundlejs](https://bundlejs.com/) - A browser tool for bundling and minifying projects, showing compressed sizes.

Additionally, make sure to implement the recommended optimization strategies.

StephDietz marked this conversation as resolved.
Show resolved Hide resolved
- Optimize Server Actions - Focus on simplifying and speeding up your server actions. This can be achieved by optimizing database queries, reducing external API calls, and ensuring server-side computations are efficient. Efficient server-side processing helps reduce the time it takes for your pages to load and improves the overall performance of your website.
StephDietz marked this conversation as resolved.
Show resolved Hide resolved
- Minimize Client-Side JavaScript - It's important to keep JavaScript on the client-side as small as possible for quicker website loading and a better user experience.
- Moving client components down the tree or carefully considering where to place the "use client" boundaries. In other words, moving more rendering / logic to the server using Server Components.
StephDietz marked this conversation as resolved.
Show resolved Hide resolved
- Fetching data on the server: https://nextjs.org/docs/app/building-your-application/data-fetching/patterns#fetching-data-on-the-server
StephDietz marked this conversation as resolved.
Show resolved Hide resolved

### Optimizing with `@next/bundle-analyzer`

[`@next/bundle-analyzer`](https://www.npmjs.com/package/@next/bundle-analyzer) is a plugin for Next.js that helps manage the size of your JavaScript files. It shows you how big each part of your webpack output is. This is helpful because you can find and fix parts of your code that are too large. For example, you might remove large dependencies, split your code, or only load some parts when needed.

How to use `@next/bundle-analyzer`:

1. **Install**: Run `npm install @next/bundle-analyzer webpack-bundle-analyzer`.
2. **Configure**: Add the bundle analyzer's settings to your `next.config.js`.

```js
const withBundleAnalyzer = require('@next/bundle-analyzer')({
enabled: process.env.ANALYZE === 'true',
})
module.exports = withBundleAnalyzer({})
```

StephDietz marked this conversation as resolved.
Show resolved Hide resolved
3. **Run Analysis**: Use `ANALYZE=true npm run build` to start looking at your file sizes.
4. **Review Findings**: Check the report to see where you can make improvements.

Doing this regularly while you develop and before deploying your site can help you identify performance issues earlier.
StephDietz marked this conversation as resolved.
Show resolved Hide resolved

## Logging

Since Next.js runs on both the client and server, there are multiple forms of logging supported:

- `console.log`: Works in both browser and server. In the browser, it logs to the console. On the server, it outputs to the terminal.
- `process.stdout.write`: Used on the server for more control over the output format, writing directly to stdout.
StephDietz marked this conversation as resolved.
Show resolved Hide resolved

If you want a structured logging package, we recommend [Pino](https://www.npmjs.com/package/pino). If you're using Vercel, there are [pre-built logging integrations](https://vercel.com/integrations?utm_source=next-site&utm_medium=docs&utm_campaign=next-website#logging) compatible with Next.js.

## Error Handling

Proper error handling is key to keeping your application stable and providing a good user experience. This is especially important with the Dynamic Route Segments and Server Actions in the App Router.
StephDietz marked this conversation as resolved.
Show resolved Hide resolved

**Use of `error.js` for Error Handling**:

The [`error.js` file](/docs/app/building-your-application/routing/error-handling) convention is used for gracefully handling runtime errors in nested routes. It automatically wraps a route segment and its nested children in a React Error Boundary, allowing you to create a tailored error UI and isolate errors to specific segments while keeping the rest of the application functional.

If an error is thrown inside a Server Component, Next.js forwards an `Error` object to the nearest `error.js` file. This object includes a generic message and a digest property in production as a security measure to prevent leaking sensitive details.

## Loading Performance

To effectively improve loading performance, it's essential to measure the user experience using [Core Web Vitals](https://vercel.com/blog/core-web-vitals?utm_source=next-site&utm_medium=docs&utm_campaign=next-website). These are a set of metrics developed by Google to gauge the quality of user experience on web pages, influencing Google's search rankings.

### Using Lighthouse for Performance Analysis
StephDietz marked this conversation as resolved.
Show resolved Hide resolved

Google Lighthouse is a powerful tool for assessing Core Web Vitals, along with other important aspects such as performance, accessibility, best practices, and SEO. To get the most accurate results:
StephDietz marked this conversation as resolved.
Show resolved Hide resolved

1. Use a production build of your Next.js application.
2. Run Lighthouse in incognito mode to prevent browser extensions from affecting the results.

Lighthouse can be used both as a browser extension and a command-line tool, providing detailed reports on how your website performs against these vital metrics.
StephDietz marked this conversation as resolved.
Show resolved Hide resolved

### Utilizing Vercel's Dashboard (for Vercel Deployments)
StephDietz marked this conversation as resolved.
Show resolved Hide resolved

If your Next.js application is deployed on [Vercel](https://vercel.com/vercel), you can leverage Vercel's built-in analytics dashboard to measure Core Web Vitals. This dashboard provides real-world performance insights based on actual visitor data, offering a practical view of how your website is performing in the field.
StephDietz marked this conversation as resolved.
Show resolved Hide resolved

Once you are able to measure the loading performance, use the following strategies to improve it iteratively. That is, if you apply one strategy, measure the new performance and continue tweaking until you do not see much improvement. Then, you can move on to the next strategy.

- **Optimize Caching**: Use caching strategies as detailed in the caching section.
- **Server Actions Efficiency**: Optimize server actions for quick data retrieval and minimal processing.
- **Reduce JavaScript Bundle Sizes**: Remove unused JavaScript, and move data fetching and logic to the server.
- Setting up your Code Editor to view import costs and sizes
- Finding alternative smaller packages
- Dynamically loading components and dependencies
- **Streaming**: Leverage the Loading UI and Suspense to stream chunks in parallel for improved initial load.
StephDietz marked this conversation as resolved.
Show resolved Hide resolved
Loading