Skip to content

Commit 456a8cd

Browse files
delbaoliveiramolebox
authored andcommitted
Docs IA 2.0: Metadata (#78905)
Closes: https://linear.app/vercel/issue/DOC-4642/metadata Redirects: vercel/front#45244 - Expands `generateMetadata` API reference to describe behavior - Adds new examples to the `ImageResponse` API reference - Create JSON-LD guide --------- Co-authored-by: Rich Haines <hello@richardhaines.dev>
1 parent 363fcf6 commit 456a8cd

File tree

15 files changed

+492
-530
lines changed

15 files changed

+492
-530
lines changed

docs/01-app/01-getting-started/10-metadata-and-og-images.mdx

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ related:
77
description: Learn more about the Metadata APIs mentioned in this page.
88
links:
99
- app/api-reference/functions/generate-metadata
10+
- app/api-reference/functions/generate-viewport
11+
- app/api-reference/functions/image-response
1012
- app/api-reference/file-conventions/metadata
1113
- app/api-reference/file-conventions/metadata/app-icons
1214
- app/api-reference/file-conventions/metadata/opengraph-image
13-
- app/api-reference/functions/image-response
15+
- app/api-reference/file-conventions/metadata/robots
16+
- app/api-reference/file-conventions/metadata/sitemap
1417
---
1518

1619
The Metadata APIs can be used to define your application metadata for improved SEO and web shareability and include:
@@ -59,7 +62,7 @@ export const metadata = {
5962
export default function Page() {}
6063
```
6164

62-
You can view a full list of available options, in the [generate metadata documentation](/docs/app/api-reference/functions/generate-metadata#metadata-fields).
65+
You can view a full list of available options, in the [`generateMetadata` documentation](/docs/app/api-reference/functions/generate-metadata#metadata-fields).
6366

6467
## Generated metadata
6568

@@ -177,6 +180,17 @@ export default async function Page({ params }) {
177180
}
178181
```
179182

183+
## File-based metadata
184+
185+
The following special files are available for metadata:
186+
187+
- [favicon.ico, apple-icon.jpg, and icon.jpg](/docs/app/api-reference/file-conventions/metadata/app-icons)
188+
- [opengraph-image.jpg and twitter-image.jpg](/docs/app/api-reference/file-conventions/metadata/opengraph-image)
189+
- [robots.txt](/docs/app/api-reference/file-conventions/metadata/robots)
190+
- [sitemap.xml](/docs/app/api-reference/file-conventions/metadata/sitemap)
191+
192+
You can use these for static metadata, or you can programmatically generate these files with code.
193+
180194
## Favicons
181195

182196
Favicons are small icons that represent your site in bookmarks and search results. To add a favicon to your application, create a `favicon.ico` and add to the root of the app folder.

docs/01-app/02-guides/json-ld.mdx

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
---
2+
title: How to implement JSON-LD in your Next.js application
3+
nav_title: JSON-LD
4+
description: Learn how to add JSON-LD to your Next.js application to describe your content to search engines and AI.
5+
---
6+
7+
[JSON-LD](https://json-ld.org/) is a format for structured data that can be used by search engines and AI to to help them understand the structure of the page beyond pure content. For example, you can use it to describe a person, an event, an organization, a movie, a book, a recipe, and many other types of entities.
8+
9+
Our current recommendation for JSON-LD is to render structured data as a `<script>` tag in your `layout.js` or `page.js` components. For example:
10+
11+
```tsx filename="app/products/[id]/page.tsx" switcher
12+
export default async function Page({ params }) {
13+
const { id } = await params
14+
const product = await getProduct(id)
15+
16+
const jsonLd = {
17+
'@context': 'https://schema.org',
18+
'@type': 'Product',
19+
name: product.name,
20+
image: product.image,
21+
description: product.description,
22+
}
23+
24+
return (
25+
<section>
26+
{/* Add JSON-LD to your page */}
27+
<script
28+
type="application/ld+json"
29+
dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
30+
/>
31+
{/* ... */}
32+
</section>
33+
)
34+
}
35+
```
36+
37+
```jsx filename="app/products/[id]/page.js" switcher
38+
export default async function Page({ params }) {
39+
const { id } = await params
40+
const product = await getProduct(id)
41+
42+
const jsonLd = {
43+
'@context': 'https://schema.org',
44+
'@type': 'Product',
45+
name: product.name,
46+
image: product.image,
47+
description: product.description,
48+
}
49+
50+
return (
51+
<section>
52+
{/* Add JSON-LD to your page */}
53+
<script
54+
type="application/ld+json"
55+
dangerouslySetInnerHTML={{ __html: JSON.stringify(jsonLd) }}
56+
/>
57+
{/* ... */}
58+
</section>
59+
)
60+
}
61+
```
62+
63+
You can validate and test your structured data with the [Rich Results Test](https://search.google.com/test/rich-results) for Google or the generic [Schema Markup Validator](https://validator.schema.org/).
64+
65+
You can type your JSON-LD with TypeScript using community packages like [`schema-dts`](https://www.npmjs.com/package/schema-dts):
66+
67+
```tsx
68+
import { Product, WithContext } from 'schema-dts'
69+
70+
const jsonLd: WithContext<Product> = {
71+
'@context': 'https://schema.org',
72+
'@type': 'Product',
73+
name: 'Next.js Sticker',
74+
image: 'https://nextjs.org/imgs/sticker.png',
75+
description: 'Dynamic at the speed of static.',
76+
}
77+
```

docs/01-app/02-guides/mdx.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ When using file based routing, you can use MDX pages like any other page.
110110

111111
<AppOnly>
112112

113-
In App Router apps, that includes being able to use [metadata](/docs/app/building-your-application/optimizing/metadata).
113+
In App Router apps, that includes being able to use [metadata](/docs/app/getting-started/metadata-and-og-images).
114114

115115
Create a new MDX page within the `/app` directory:
116116

docs/01-app/02-guides/migrating/app-router-migration.mdx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -166,7 +166,7 @@ export default function RootLayout({
166166
- The root layout replaces the `pages/_app.tsx` and `pages/_document.tsx` files.
167167
- `.js`, `.jsx`, or `.tsx` extensions can be used for layout files.
168168

169-
To manage `<head>` HTML elements, you can use the [built-in SEO support](/docs/app/building-your-application/optimizing/metadata):
169+
To manage `<head>` HTML elements, you can use the [built-in SEO support](/docs/app/getting-started/metadata-and-og-images):
170170

171171
```tsx filename="app/layout.tsx" switcher
172172
import type { Metadata } from 'next'
@@ -265,7 +265,7 @@ Page.getLayout = function getLayout(page) {
265265

266266
### Step 3: Migrating `next/head`
267267

268-
In the `pages` directory, the `next/head` React component is used to manage `<head>` HTML elements such as `title` and `meta` . In the `app` directory, `next/head` is replaced with the new [built-in SEO support](/docs/app/building-your-application/optimizing/metadata).
268+
In the `pages` directory, the `next/head` React component is used to manage `<head>` HTML elements such as `title` and `meta` . In the `app` directory, `next/head` is replaced with the new [built-in SEO support](/docs/app/getting-started/metadata-and-og-images).
269269

270270
**Before:**
271271

docs/01-app/02-guides/migrating/from-create-react-app.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ export default function RootLayout({ children }) {
145145
}
146146
```
147147

148-
> **Good to know**: Next.js ignores CRA’s `public/manifest.json`, additional iconography, and [testing configuration](/docs/app/guides/testing) by default. If you need these, Next.js has support with its [Metadata API](/docs/app/building-your-application/optimizing/metadata) and [Testing](/docs/app/guides/testing) setup.
148+
> **Good to know**: Next.js ignores CRA’s `public/manifest.json`, additional iconography, and [testing configuration](/docs/app/guides/testing) by default. If you need these, Next.js has support with its [Metadata API](/docs/app/getting-started/metadata-and-og-images) and [Testing](/docs/app/guides/testing) setup.
149149
150150
### Step 4: Metadata
151151

@@ -189,7 +189,7 @@ export default function RootLayout({ children }) {
189189
}
190190
```
191191

192-
Any [metadata files](/docs/app/building-your-application/optimizing/metadata#file-based-metadata) such as `favicon.ico`, `icon.png`, `robots.txt` are automatically added to the application `<head>` tag as long as you have them placed into the top level of the `app` directory. After moving [all supported files](/docs/app/building-your-application/optimizing/metadata#file-based-metadata) into the `app` directory you can safely delete their `<link>` tags:
192+
Any [metadata files](/docs/app/getting-started/metadata-and-og-images#file-based-metadata) such as `favicon.ico`, `icon.png`, `robots.txt` are automatically added to the application `<head>` tag as long as you have them placed into the top level of the `app` directory. After moving [all supported files](/docs/app/getting-started/metadata-and-og-images#file-based-metadata) into the `app` directory you can safely delete their `<link>` tags:
193193

194194
```tsx filename="app/layout.tsx" switcher
195195
export default function RootLayout({
@@ -227,7 +227,7 @@ export default function RootLayout({ children }) {
227227
}
228228
```
229229

230-
Finally, Next.js can manage your last `<head>` tags with the [Metadata API](/docs/app/building-your-application/optimizing/metadata). Move your final metadata info into an exported [`metadata` object](/docs/app/api-reference/functions/generate-metadata#metadata-object):
230+
Finally, Next.js can manage your last `<head>` tags with the [Metadata API](/docs/app/getting-started/metadata-and-og-images). Move your final metadata info into an exported [`metadata` object](/docs/app/api-reference/functions/generate-metadata#metadata-object):
231231

232232
```tsx filename="app/layout.tsx" switcher
233233
import type { Metadata } from 'next'
@@ -269,7 +269,7 @@ export default function RootLayout({ children }) {
269269
}
270270
```
271271

272-
With the above changes, you shifted from declaring everything in your `index.html` to using Next.js' convention-based approach built into the framework ([Metadata API](/docs/app/building-your-application/optimizing/metadata)). This approach enables you to more easily improve your SEO and web shareability of your pages.
272+
With the above changes, you shifted from declaring everything in your `index.html` to using Next.js' convention-based approach built into the framework ([Metadata API](/docs/app/getting-started/metadata-and-og-images)). This approach enables you to more easily improve your SEO and web shareability of your pages.
273273

274274
### Step 5: Styles
275275

docs/01-app/02-guides/migrating/from-vite.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -249,11 +249,11 @@ export default function RootLayout({ children }) {
249249
}
250250
```
251251

252-
5. Any [metadata files](/docs/app/building-your-application/optimizing/metadata#file-based-metadata)
252+
5. Any [metadata files](/docs/app/getting-started/metadata-and-og-images#file-based-metadata)
253253
such as `favicon.ico`, `icon.png`, `robots.txt` are automatically added to the application
254254
`<head>` tag as long as you have them placed into the top level of the `app` directory. After
255255
moving
256-
[all supported files](/docs/app/building-your-application/optimizing/metadata#file-based-metadata)
256+
[all supported files](/docs/app/getting-started/metadata-and-og-images#file-based-metadata)
257257
into the `app` directory you can safely delete their `<link>` tags:
258258

259259
```tsx filename="app/layout.tsx" switcher
@@ -293,7 +293,7 @@ export default function RootLayout({ children }) {
293293
```
294294

295295
6. Finally, Next.js can manage your last `<head>` tags with the
296-
[Metadata API](/docs/app/building-your-application/optimizing/metadata). Move your final metadata
296+
[Metadata API](/docs/app/getting-started/metadata-and-og-images). Move your final metadata
297297
info into an exported
298298
[`metadata` object](/docs/app/api-reference/functions/generate-metadata#metadata-object):
299299

@@ -339,7 +339,7 @@ export default function RootLayout({ children }) {
339339

340340
With the above changes, you shifted from declaring everything in your `index.html` to using Next.js'
341341
convention-based approach built into the framework
342-
([Metadata API](/docs/app/building-your-application/optimizing/metadata)). This approach enables you
342+
([Metadata API](/docs/app/getting-started/metadata-and-og-images)). This approach enables you
343343
to more easily improve your SEO and web shareability of your pages.
344344

345345
### Step 5: Create the Entrypoint Page

docs/01-app/02-guides/production-checklist.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ While building your application, we recommend using the following features to en
108108

109109
<AppOnly>
110110

111-
- **[Metadata API](/docs/app/building-your-application/optimizing/metadata):** Use the Metadata API to improve your application's Search Engine Optimization (SEO) by adding page titles, descriptions, and more.
111+
- **[Metadata API](/docs/app/getting-started/metadata-and-og-images):** Use the Metadata API to improve your application's Search Engine Optimization (SEO) by adding page titles, descriptions, and more.
112112
- **[Open Graph (OG) images](/docs/app/api-reference/file-conventions/metadata/opengraph-image):** Create OG images to prepare your application for social sharing.
113113
- **[Sitemaps](/docs/app/api-reference/functions/generate-sitemaps) and [Robots](/docs/app/api-reference/file-conventions/metadata/robots):** Help Search Engines crawl and index your pages by generating sitemaps and robots files.
114114

docs/01-app/02-guides/upgrading/codemods.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export function GET(req: NextRequest) {
208208
npx @next/codemod@latest next-og-import .
209209
```
210210

211-
This codemod moves transforms imports from `next/server` to `next/og` for usage of [Dynamic OG Image Generation](/docs/app/building-your-application/optimizing/metadata#dynamic-image-generation).
211+
This codemod moves transforms imports from `next/server` to `next/og` for usage of [Dynamic OG Image Generation](/docs/app/getting-started/metadata-and-og-images#generated-open-graph-images).
212212

213213
For example:
214214

docs/01-app/03-building-your-application/01-routing/03-layouts-and-templates.mdx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ In terms of nesting, `template.js` is rendered between a layout and its children
187187

188188
### Metadata
189189

190-
You can modify the `<head>` HTML elements such as `title` and `meta` using the [Metadata APIs](/docs/app/building-your-application/optimizing/metadata).
190+
You can modify the `<head>` HTML elements such as `title` and `meta` using the [Metadata APIs](/docs/app/getting-started/metadata-and-og-images).
191191

192192
Metadata can be defined by exporting a [`metadata` object](/docs/app/api-reference/functions/generate-metadata#the-metadata-object) or [`generateMetadata` function](/docs/app/api-reference/functions/generate-metadata#generatemetadata-function) in a [`layout.js`](/docs/app/api-reference/file-conventions/layout) or [`page.js`](/docs/app/api-reference/file-conventions/page) file.
193193

0 commit comments

Comments
 (0)