Skip to content

Commit 5333625

Browse files
icyJosephJuneezeemoleboxtimneutkensvercel[bot]
authored
Backport/docs fixes 16.1.5 (#88916)
Back porting docs updates into 16-1 (canary now contains changes that shouldn't show in 16.1) --------- Co-authored-by: Eng Zer Jun <engzerjun@gmail.com> Co-authored-by: Rich Haines <hello@richardhaines.dev> Co-authored-by: Tim Neutkens <tim@timneutkens.nl> Co-authored-by: vercel[bot] <35613825+vercel[bot]@users.noreply.github.com> Co-authored-by: Delba de Oliveira <32464864+delbaoliveira@users.noreply.github.com> Co-authored-by: Luke Sandberg <lukesandberg@users.noreply.github.com> Co-authored-by: graphite-app[bot] <96075541+graphite-app[bot]@users.noreply.github.com> Co-authored-by: Jiachi Liu <inbox@huozhi.im> Co-authored-by: Vercel <vercel[bot]@users.noreply.github.com>
1 parent 60de6c2 commit 5333625

22 files changed

Lines changed: 1179 additions & 78 deletions

docs/01-app/01-getting-started/04-linking-and-navigating.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,7 @@ function HoverPrefetchLink({ href, children }) {
328328
329329
React mitigates this with Selective Hydration and you can further improve this by:
330330
331-
- Using the [`@next/bundle-analyzer`](/docs/app/guides/package-bundling#analyzing-javascript-bundles) plugin to identify and reduce bundle size by removing large dependencies.
331+
- Using the [`@next/bundle-analyzer`](/docs/app/guides/package-bundling#nextbundle-analyzer-for-webpack) plugin to identify and reduce bundle size by removing large dependencies.
332332
- Moving logic from the client to the server where possible. See the [Server and Client Components](/docs/app/getting-started/server-and-client-components) docs for guidance.
333333
334334
## Examples

docs/01-app/01-getting-started/05-server-and-client-components.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ Use **Server Components** when you need:
3232

3333
For example, the `<Page>` component is a Server Component that fetches data about a post, and passes it as props to the `<LikeButton>` which handles client-side interactivity.
3434

35-
```tsx filename="app/[id]/page.tsx" highlight={1,12} switcher
35+
```tsx filename="app/[id]/page.tsx" highlight={1,17} switcher
3636
import LikeButton from '@/app/ui/like-button'
3737
import { getPost } from '@/lib/data'
3838

@@ -241,7 +241,7 @@ export default function Search() {
241241

242242
You can pass data from Server Components to Client Components using props.
243243

244-
```tsx filename="app/[id]/page.tsx" highlight={1,7} switcher
244+
```tsx filename="app/[id]/page.tsx" highlight={1,12} switcher
245245
import LikeButton from '@/app/ui/like-button'
246246
import { getPost } from '@/lib/data'
247247

docs/01-app/01-getting-started/06-cache-components.mdx

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ export default async function Page() {
8686
}
8787
```
8888

89-
> **Good to know**: You can verify that a route was fully prerendered by checking the build output summary. Alternatively see what content was added to the static shell of any page by viewing the page source in your browser.
89+
> **Good to know**: You can verify that a route was fully prerendered by checking the build output summary. Alternatively, see what content was added to the static shell of any page by viewing the page source in your browser.
9090
9191
## Defer rendering to request time
9292

@@ -102,12 +102,11 @@ External systems provide content asynchronously, which often takes an unpredicta
102102

103103
In general, when you need the latest data from the source on each request (like real-time feeds or personalized content), defer rendering by providing fallback UI with a Suspense boundary.
104104

105-
For example the `DynamicContent` component below uses multiple operations that are not automatically prerendered.
105+
For example, the `DynamicContent` component below uses multiple operations that are not automatically prerendered.
106106

107107
```tsx filename="page.tsx"
108108
import { Suspense } from 'react'
109109
import fs from 'node:fs/promises'
110-
import { setTimeout } from 'node:timers/promises'
111110

112111
async function DynamicContent() {
113112
// Network request
@@ -120,7 +119,7 @@ async function DynamicContent() {
120119
const file = await fs.readFile('..', 'utf-8')
121120

122121
// Simulating external system delay
123-
await setTimeout(100) // from 'node:timers/promises'
122+
await new Promise((resolve) => setTimeout(resolve, 100))
124123

125124
return <div>Not in the static shell</div>
126125
}
@@ -174,7 +173,7 @@ async function RuntimeData({ searchParams }) {
174173
}
175174
```
176175

177-
To use the `RuntimeData` component in, wrap it in a `<Suspense>` boundary:
176+
To use the `RuntimeData` component, wrap it in a `<Suspense>` boundary:
178177

179178
```tsx filename="page.tsx"
180179
export default async function Page(props) {
@@ -504,6 +503,12 @@ During prerendering the header (static) and the blog posts fetched from the API
504503

505504
When a user visits the page, they instantly see this prerendered shell with the header and blog posts. Only the personalized preferences need to stream in at request time since they depend on the user's cookies. This ensures fast initial page loads while still providing personalized content.
506505

506+
## Metadata and Viewport
507+
508+
`generateMetadata` and `generateViewport` are part of rendering your page or layout. During prerendering, their access to runtime data or uncached dynamic data is tracked separately from the rest of the page.
509+
510+
If a page or layout is prerenderable but only metadata or viewport accesses uncached dynamic data or runtime data, Next.js requires an explicit choice: cache the data if possible, or signal that deferred rendering is intentional. See [Metadata with Cache Components](/docs/app/api-reference/functions/generate-metadata#with-cache-components) and [Viewport with Cache Components](/docs/app/api-reference/functions/generate-viewport#with-cache-components) for how to handle this.
511+
507512
## Enabling Cache Components
508513

509514
You can enable Cache Components (which includes PPR) by adding the [`cacheComponents`](/docs/app/api-reference/config/next-config-js/cacheComponents) option to your Next config file:
@@ -569,7 +574,7 @@ export default function Page() {
569574

570575
### `dynamic = "force-static"`
571576

572-
Start by removing it. When unhandled dynamic or runtime data access is detected during development and built time, Next.js raises an error. Otherwise, the [prerendering](#automatically-prerendered-content) step automatically extracts the static HTML shell.
577+
Start by removing it. When unhandled dynamic or runtime data access is detected during development and build time, Next.js raises an error. Otherwise, the [prerendering](#automatically-prerendered-content) step automatically extracts the static HTML shell.
573578

574579
For dynamic data access, add [`use cache`](#using-use-cache) as close to the data access as possible with a long [`cacheLife`](/docs/app/api-reference/functions/cacheLife) like `'max'` to maintain cached behavior. If needed, add it at the top of the page or layout.
575580

docs/01-app/01-getting-started/13-fonts.mdx

Lines changed: 214 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ The [`next/font`](/docs/app/api-reference/components/font) module automatically
1212

1313
It includes **built-in self-hosting** for any font file. This means you can optimally load web fonts with no layout shift.
1414

15+
<AppOnly>
16+
1517
To start using `next/font`, import it from [`next/font/local`](#local-fonts) or [`next/font/google`](#google-fonts), call it as a function with the appropriate options, and set the `className` of the element you want to apply the font to. For example:
1618

1719
```tsx filename="app/layout.tsx" highlight={1,3-5,9} switcher
@@ -48,12 +50,99 @@ export default function Layout({ children }) {
4850

4951
Fonts are scoped to the component they're used in. To apply a font to your entire application, add it to the [Root Layout](/docs/app/api-reference/file-conventions/layout#root-layout).
5052

53+
</AppOnly>
54+
55+
<PagesOnly>
56+
57+
To start using `next/font`, import it from [`next/font/local`](#local-fonts) or [`next/font/google`](#google-fonts), call it as a function with the appropriate options, and set the `className` of the element you want to apply the font to. For example, you can apply fonts globally in your [Custom App](/docs/pages/building-your-application/routing/custom-app) (`pages/_app`):
58+
59+
```tsx filename="pages/_app.tsx" highlight={1,4-6,10} switcher
60+
import { Geist } from 'next/font/google'
61+
import type { AppProps } from 'next/app'
62+
63+
const geist = Geist({
64+
subsets: ['latin'],
65+
})
66+
67+
export default function MyApp({ Component, pageProps }: AppProps) {
68+
return (
69+
<main className={geist.className}>
70+
<Component {...pageProps} />
71+
</main>
72+
)
73+
}
74+
```
75+
76+
```jsx filename="pages/_app.js" highlight={1,3-5,9} switcher
77+
import { Geist } from 'next/font/google'
78+
79+
const geist = Geist({
80+
subsets: ['latin'],
81+
})
82+
83+
export default function MyApp({ Component, pageProps }) {
84+
return (
85+
<main className={geist.className}>
86+
<Component {...pageProps} />
87+
</main>
88+
)
89+
}
90+
```
91+
92+
If you want to apply the font to the `<html>` element, you can use a [Custom Document](/docs/pages/building-your-application/routing/custom-document) (`pages/_document`):
93+
94+
```tsx filename="pages/_document.tsx" highlight={2,4-6,10} switcher
95+
import { Html, Head, Main, NextScript } from 'next/document'
96+
import { Geist } from 'next/font/google'
97+
98+
const geist = Geist({
99+
subsets: ['latin'],
100+
})
101+
102+
export default function Document() {
103+
return (
104+
<Html lang="en" className={geist.className}>
105+
<Head />
106+
<body>
107+
<Main />
108+
<NextScript />
109+
</body>
110+
</Html>
111+
)
112+
}
113+
```
114+
115+
```jsx filename="pages/_document.js" highlight={2,4-6,10} switcher
116+
import { Html, Head, Main, NextScript } from 'next/document'
117+
import { Geist } from 'next/font/google'
118+
119+
const geist = Geist({
120+
subsets: ['latin'],
121+
})
122+
123+
export default function Document() {
124+
return (
125+
<Html lang="en" className={geist.className}>
126+
<Head />
127+
<body>
128+
<Main />
129+
<NextScript />
130+
</body>
131+
</Html>
132+
)
133+
}
134+
```
135+
136+
</PagesOnly>
137+
51138
## Google fonts
52139

53140
You can automatically self-host any Google Font. Fonts are included stored as static assets and served from the same domain as your deployment, meaning no requests are sent to Google by the browser when the user visits your site.
54141

55142
To start using a Google Font, import your chosen font from `next/font/google`:
56143

144+
<AppOnly>
145+
57146
```tsx filename="app/layout.tsx" switcher
58147
import { Geist } from 'next/font/google'
59148

@@ -90,8 +179,49 @@ export default function RootLayout({ children }) {
90179
}
91180
```
92181

182+
</AppOnly>
183+
184+
<PagesOnly>
185+
186+
```tsx filename="pages/_app.tsx" switcher
187+
import { Geist } from 'next/font/google'
188+
import type { AppProps } from 'next/app'
189+
190+
const geist = Geist({
191+
subsets: ['latin'],
192+
})
193+
194+
export default function MyApp({ Component, pageProps }: AppProps) {
195+
return (
196+
<main className={geist.className}>
197+
<Component {...pageProps} />
198+
</main>
199+
)
200+
}
201+
```
202+
203+
```jsx filename="pages/_app.js" switcher
204+
import { Geist } from 'next/font/google'
205+
206+
const geist = Geist({
207+
subsets: ['latin'],
208+
})
209+
210+
export default function MyApp({ Component, pageProps }) {
211+
return (
212+
<main className={geist.className}>
213+
<Component {...pageProps} />
214+
</main>
215+
)
216+
}
217+
```
218+
219+
</PagesOnly>
220+
93221
We recommend using [variable fonts](https://fonts.google.com/variablefonts) for the best performance and flexibility. But if you can't use a variable font, you will need to specify a weight:
94222

223+
<AppOnly>
224+
95225
```tsx filename="app/layout.tsx" highlight={4} switcher
96226
import { Roboto } from 'next/font/google'
97227

@@ -130,8 +260,51 @@ export default function RootLayout({ children }) {
130260
}
131261
```
132262

263+
</AppOnly>
264+
265+
<PagesOnly>
266+
267+
```tsx filename="pages/_app.tsx" highlight={5} switcher
268+
import { Roboto } from 'next/font/google'
269+
import type { AppProps } from 'next/app'
270+
271+
const roboto = Roboto({
272+
weight: '400',
273+
subsets: ['latin'],
274+
})
275+
276+
export default function MyApp({ Component, pageProps }: AppProps) {
277+
return (
278+
<main className={roboto.className}>
279+
<Component {...pageProps} />
280+
</main>
281+
)
282+
}
283+
```
284+
285+
```jsx filename="pages/_app.js" highlight={4} switcher
286+
import { Roboto } from 'next/font/google'
287+
288+
const roboto = Roboto({
289+
weight: '400',
290+
subsets: ['latin'],
291+
})
292+
293+
export default function MyApp({ Component, pageProps }) {
294+
return (
295+
<main className={roboto.className}>
296+
<Component {...pageProps} />
297+
</main>
298+
)
299+
}
300+
```
301+
302+
</PagesOnly>
303+
133304
## Local fonts
134305

306+
<AppOnly>
307+
135308
To use a local font, import your font from `next/font/local` and specify the [`src`](/docs/app/api-reference/components/font#src) of your local font file. Fonts can be stored in the [`public`](/docs/app/api-reference/file-conventions/public-folder) folder or co-located inside the `app` folder. For example:
136309

137310
```tsx filename="app/layout.tsx" switcher
@@ -170,6 +343,47 @@ export default function RootLayout({ children }) {
170343
}
171344
```
172345

346+
</AppOnly>
347+
348+
<PagesOnly>
349+
350+
To use a local font, import your font from `next/font/local` and specify the [`src`](/docs/pages/api-reference/components/font#src) of your local font file. Fonts can be stored in the [`public`](/docs/pages/api-reference/file-conventions/public-folder) folder or inside the `pages` folder. For example:
351+
352+
```tsx filename="pages/_app.tsx" switcher
353+
import localFont from 'next/font/local'
354+
import type { AppProps } from 'next/app'
355+
356+
const myFont = localFont({
357+
src: './my-font.woff2',
358+
})
359+
360+
export default function MyApp({ Component, pageProps }: AppProps) {
361+
return (
362+
<main className={myFont.className}>
363+
<Component {...pageProps} />
364+
</main>
365+
)
366+
}
367+
```
368+
369+
```jsx filename="pages/_app.js" switcher
370+
import localFont from 'next/font/local'
371+
372+
const myFont = localFont({
373+
src: './my-font.woff2',
374+
})
375+
376+
export default function MyApp({ Component, pageProps }) {
377+
return (
378+
<main className={myFont.className}>
379+
<Component {...pageProps} />
380+
</main>
381+
)
382+
}
383+
```
384+
385+
</PagesOnly>
386+
173387
If you want to use multiple files for a single font family, `src` can be an array:
174388

175389
```js

docs/01-app/01-getting-started/16-proxy.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ export function proxy(request: NextRequest) {
5252
// Alternatively, you can use a default export:
5353
// export default function proxy(request: NextRequest) { ... }
5454

55-
// See "Matching Paths" below to learn more
5655
export const config = {
5756
matcher: '/about/:path*',
5857
}
@@ -69,10 +68,11 @@ export function proxy(request) {
6968
// Alternatively, you can use a default export:
7069
// export default function proxy(request) { ... }
7170

72-
// See "Matching Paths" below to learn more
7371
export const config = {
7472
matcher: '/about/:path*',
7573
}
7674
```
7775

76+
The `matcher` config allows you to filter Proxy to run on specific paths. See the [Matcher](/docs/app/api-reference/file-conventions/proxy#matcher) documentation for more details on path matching.
77+
7878
Read more about [using `proxy`](/docs/app/guides/backend-for-frontend#proxy), or refer to the `proxy` [API reference](/docs/app/api-reference/file-conventions/proxy).

docs/01-app/02-guides/content-security-policy.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -415,9 +415,11 @@ Consider nonces when:
415415
For applications that do not require nonces, you can set the CSP header directly in your [`next.config.js`](/docs/app/api-reference/config/next-config-js) file:
416416
417417
```js filename="next.config.js"
418+
const isDev = process.env.NODE_ENV === 'development'
419+
418420
const cspHeader = `
419421
default-src 'self';
420-
script-src 'self' 'unsafe-eval' 'unsafe-inline';
422+
script-src 'self' 'unsafe-inline'${isDev ? " 'unsafe-eval'" : ''};
421423
style-src 'self' 'unsafe-inline';
422424
img-src 'self' blob: data:;
423425
font-src 'self';

0 commit comments

Comments
 (0)