export const metadata = {
generator : 'Next.js' ,
applicationName : 'Next.js' ,
referrer : 'origin-when-cross-origin' ,
keywords : [ 'Next.js' , 'React' , 'JavaScript' ] ,
authors : [{ name : 'Seb' } , { name : 'Josh' , url : 'https://nextjs.net.cn' }] ,
creator : 'Jiachi Liu' ,
publisher : 'Sebastian Markbåge' ,
formatDetection : {
email : false ,
address : false ,
telephone : false ,
} ,
}
< meta name = "application-name" content = "Next.js" />
< meta name = "author" content = "Seb" />
< link rel = "author" href = "https://nextjs.net.cn" />
< meta name = "author" content = "Josh" />
< meta name = "generator" content = "Next.js" />
< meta name = "keywords" content = "Next.js,React,JavaScript" />
< meta name = "referrer" content = "origin-when-cross-origin" />
< meta name = "color-scheme" content = "dark" />
< meta name = "creator" content = "Jiachi Liu" />
< meta name = "publisher" content = "Sebastian Markbåge" />
< meta name = "format-detection" content = "telephone=no, address=no, email=no" />
如果未配置,metadataBase
将使用默认值 。
在 Vercel 上
对于生产环境部署,将使用 VERCEL_PROJECT_PRODUCTION_URL
。
对于预览部署,VERCEL_BRANCH_URL
将优先使用,如果不存在,则回退到 VERCEL_URL
。
如果存在这些值,它们将用作 metadataBase
的默认值 ,否则它将回退到 https://127.0.0.1:${process.env.PORT || 3000}
。这允许 Open Graph 图像在本地构建以及 Vercel 预览和生产环境部署中都能正常工作。覆盖默认值时,我们建议使用环境变量来计算 URL。这允许为本地开发、测试和生产环境配置 URL。
有关这些环境变量的更多详细信息,请参阅系统环境变量 文档。
URL 组合
URL 组合优先考虑开发人员意图,而不是默认的目录遍历语义。
metadataBase
和 metadata
字段之间的尾部斜杠已规范化。
metadata
字段中的“绝对”路径(通常会替换整个 URL 路径)被视为“相对”路径(从 metadataBase
的末尾开始)。
例如,给定以下 metadataBase
import type { Metadata } from 'next'
export const metadata : Metadata = {
metadataBase : new URL ( 'https://acme.com' ) ,
}
继承上述 metadataBase
并设置自身值的任何 metadata
字段都将解析如下
metadata
字段解析后的 URL /
https://acme.com
./
https://acme.com
payments
https://acme.com/payments
/payments
https://acme.com/payments
./payments
https://acme.com/payments
../payments
https://acme.com/payments
https://beta.acme.com/payments
https://beta.acme.com/payments
openGraph
export const metadata = {
openGraph : {
title : 'Next.js' ,
description : 'The React Framework for the Web' ,
url : 'https://nextjs.net.cn' ,
siteName : 'Next.js' ,
images : [
{
url : 'https://nextjs.net.cn/og.png' , // Must be an absolute URL
width : 800 ,
height : 600 ,
} ,
{
url : 'https://nextjs.net.cn/og-alt.png' , // Must be an absolute URL
width : 1800 ,
height : 1600 ,
alt : 'My custom alt' ,
} ,
] ,
videos : [
{
url : 'https://nextjs.net.cn/video.mp4' , // Must be an absolute URL
width : 800 ,
height : 600 ,
} ,
] ,
audio : [
{
url : 'https://nextjs.net.cn/audio.mp3' , // Must be an absolute URL
} ,
] ,
locale : 'en_US' ,
type : 'website' ,
} ,
}
< meta property = "og:title" content = "Next.js" />
< meta property = "og:description" content = "The React Framework for the Web" />
< meta property = "og:url" content = "https://nextjs.net.cn/" />
< meta property = "og:site_name" content = "Next.js" />
< meta property = "og:locale" content = "en_US" />
< meta property = "og:image" content = "https://nextjs.net.cn/og.png" />
< meta property = "og:image:width" content = "800" />
< meta property = "og:image:height" content = "600" />
< meta property = "og:image" content = "https://nextjs.net.cn/og-alt.png" />
< meta property = "og:image:width" content = "1800" />
< meta property = "og:image:height" content = "1600" />
< meta property = "og:image:alt" content = "My custom alt" />
< meta property = "og:video" content = "https://nextjs.net.cn/video.mp4" />
< meta property = "og:video:width" content = "800" />
< meta property = "og:video:height" content = "600" />
< meta property = "og:audio" content = "https://nextjs.net.cn/audio.mp3" />
< meta property = "og:type" content = "website" />
export const metadata = {
openGraph : {
title : 'Next.js' ,
description : 'The React Framework for the Web' ,
type : 'article' ,
publishedTime : '2023-01-01T00:00:00.000Z' ,
authors : [ 'Seb' , 'Josh' ] ,
} ,
}
< meta property = "og:title" content = "Next.js" />
< meta property = "og:description" content = "The React Framework for the Web" />
< meta property = "og:type" content = "article" />
< meta property = "article:published_time" content = "2023-01-01T00:00:00.000Z" />
< meta property = "article:author" content = "Seb" />
< meta property = "article:author" content = "Josh" />
需知 :
对于 Open Graph 图像,使用基于文件的元数据 API 可能更方便。基于文件的 API 会自动为您生成正确的元数据,而无需将配置导出与实际文件同步。
robots
import type { Metadata } from 'next'
export const metadata : Metadata = {
robots : {
index : false ,
follow : true ,
nocache : true ,
googleBot : {
index : true ,
follow : false ,
noimageindex : true ,
'max-video-preview' : - 1 ,
'max-image-preview' : 'large' ,
'max-snippet' : - 1 ,
} ,
} ,
}
< meta name = "robots" content = "noindex, follow, nocache" />
< meta
name = "googlebot"
content = "index, nofollow, noimageindex, max-video-preview:-1, max-image-preview:large, max-snippet:-1"
/>
icons
温馨提示 :我们建议尽可能使用基于文件的元数据 API 来处理图标。基于文件的 API 会自动为您生成正确的元数据,而无需将配置导出与实际文件同步。
export const metadata = {
icons : {
icon : '/icon.png' ,
shortcut : '/shortcut-icon.png' ,
apple : '/apple-icon.png' ,
other : {
rel : 'apple-touch-icon-precomposed' ,
url : '/apple-touch-icon-precomposed.png' ,
} ,
} ,
}
< link rel = "shortcut icon" href = "/shortcut-icon.png" />
< link rel = "icon" href = "/icon.png" />
< link rel = "apple-touch-icon" href = "/apple-icon.png" />
< link
rel = "apple-touch-icon-precomposed"
href = "/apple-touch-icon-precomposed.png"
/>
export const metadata = {
icons : {
icon : [
{ url : '/icon.png' } ,
new URL ( '/icon.png' , 'https://example.com' ) ,
{ url : '/icon-dark.png' , media : '(prefers-color-scheme: dark)' } ,
] ,
shortcut : [ '/shortcut-icon.png' ] ,
apple : [
{ url : '/apple-icon.png' } ,
{ url : '/apple-icon-x3.png' , sizes : '180x180' , type : 'image/png' } ,
] ,
other : [
{
rel : 'apple-touch-icon-precomposed' ,
url : '/apple-touch-icon-precomposed.png' ,
} ,
] ,
} ,
}
< link rel = "shortcut icon" href = "/shortcut-icon.png" />
< link rel = "icon" href = "/icon.png" />
< link rel = "icon" href = "https://example.com/icon.png" />
< link rel = "icon" href = "/icon-dark.png" media = "(prefers-color-scheme: dark)" />
< link rel = "apple-touch-icon" href = "/apple-icon.png" />
< link
rel = "apple-touch-icon-precomposed"
href = "/apple-touch-icon-precomposed.png"
/>
< link
rel = "apple-touch-icon"
href = "/apple-icon-x3.png"
sizes = "180x180"
type = "image/png"
/>
温馨提示 :Microsoft Edge 的 Chromium 版本不再支持 msapplication-*
元标记,因此不再需要。
themeColor
已弃用 :自 Next.js 14 起,metadata
中的 themeColor
选项已弃用。请改用viewport
配置 。
manifest
Web 应用程序清单文件规范 。
export const metadata = {
manifest : 'https://nextjs.net.cn/manifest.json' ,
}
< link rel = "manifest" href = "https://nextjs.net.cn/manifest.json" />
Twitter 规范(令人惊讶地)不仅用于 X(以前称为 Twitter)。
详细了解Twitter 卡片标记参考 。
export const metadata = {
twitter : {
card : 'summary_large_image' ,
title : 'Next.js' ,
description : 'The React Framework for the Web' ,
siteId : '1467726470533754880' ,
creator : '@nextjs' ,
creatorId : '1467726470533754880' ,
images : [ 'https://nextjs.net.cn/og.png' ] , // Must be an absolute URL
} ,
}
< meta name = "twitter:card" content = "summary_large_image" />
< meta name = "twitter:site:id" content = "1467726470533754880" />
< meta name = "twitter:creator" content = "@nextjs" />
< meta name = "twitter:creator:id" content = "1467726470533754880" />
< meta name = "twitter:title" content = "Next.js" />
< meta name = "twitter:description" content = "The React Framework for the Web" />
< meta name = "twitter:image" content = "https://nextjs.net.cn/og.png" />
export const metadata = {
twitter : {
card : 'app' ,
title : 'Next.js' ,
description : 'The React Framework for the Web' ,
siteId : '1467726470533754880' ,
creator : '@nextjs' ,
creatorId : '1467726470533754880' ,
images : {
url : 'https://nextjs.net.cn/og.png' ,
alt : 'Next.js Logo' ,
} ,
app : {
name : 'twitter_app' ,
id : {
iphone : 'twitter_app://iphone' ,
ipad : 'twitter_app://ipad' ,
googleplay : 'twitter_app://googleplay' ,
} ,
url : {
iphone : 'https://iphone_url' ,
ipad : 'https://ipad_url' ,
} ,
} ,
} ,
}
< meta name = "twitter:site:id" content = "1467726470533754880" />
< meta name = "twitter:creator" content = "@nextjs" />
< meta name = "twitter:creator:id" content = "1467726470533754880" />
< meta name = "twitter:title" content = "Next.js" />
< meta name = "twitter:description" content = "The React Framework for the Web" />
< meta name = "twitter:card" content = "app" />
< meta name = "twitter:image" content = "https://nextjs.net.cn/og.png" />
< meta name = "twitter:image:alt" content = "Next.js Logo" />
< meta name = "twitter:app:name:iphone" content = "twitter_app" />
< meta name = "twitter:app:id:iphone" content = "twitter_app://iphone" />
< meta name = "twitter:app:id:ipad" content = "twitter_app://ipad" />
< meta name = "twitter:app:id:googleplay" content = "twitter_app://googleplay" />
< meta name = "twitter:app:url:iphone" content = "https://iphone_url" />
< meta name = "twitter:app:url:ipad" content = "https://ipad_url" />
< meta name = "twitter:app:name:ipad" content = "twitter_app" />
< meta name = "twitter:app:name:googleplay" content = "twitter_app" />
viewport
已弃用 :自 Next.js 14 起,metadata
中的 viewport
选项已弃用。请改用viewport
配置 。
verification
export const metadata = {
verification : {
google : 'google' ,
yandex : 'yandex' ,
yahoo : 'yahoo' ,
other : {
me : [ 'my-email' , 'my-link' ] ,
} ,
} ,
}
< meta name = "google-site-verification" content = "google" />
< meta name = "y_key" content = "yahoo" />
< meta name = "yandex-verification" content = "yandex" />
< meta name = "me" content = "my-email" />
< meta name = "me" content = "my-link" />
appleWebApp
export const metadata = {
itunes : {
appId : 'myAppStoreID' ,
appArgument : 'myAppArgument' ,
} ,
appleWebApp : {
title : 'Apple Web App' ,
statusBarStyle : 'black-translucent' ,
startupImage : [
'/assets/startup/apple-touch-startup-image-768x1004.png' ,
{
url : '/assets/startup/apple-touch-startup-image-1536x2008.png' ,
media : '(device-width: 768px) and (device-height: 1024px)' ,
} ,
] ,
} ,
}
< meta
name = "apple-itunes-app"
content = "app-id=myAppStoreID, app-argument=myAppArgument"
/>
< meta name = "mobile-web-app-capable" content = "yes" />
< meta name = "apple-mobile-web-app-title" content = "Apple Web App" />
< link
href = "/assets/startup/apple-touch-startup-image-768x1004.png"
rel = "apple-touch-startup-image"
/>
< link
href = "/assets/startup/apple-touch-startup-image-1536x2008.png"
media = "(device-width: 768px) and (device-height: 1024px)"
rel = "apple-touch-startup-image"
/>
< meta
name = "apple-mobile-web-app-status-bar-style"
content = "black-translucent"
/>
alternates
export const metadata = {
alternates : {
canonical : 'https://nextjs.net.cn' ,
languages : {
'en-US' : 'https://nextjs.net.cn/en-US' ,
'de-DE' : 'https://nextjs.net.cn/de-DE' ,
} ,
media : {
'only screen and (max-width: 600px)' : 'https://nextjs.net.cn/mobile' ,
} ,
types : {
'application/rss+xml' : 'https://nextjs.net.cn/rss' ,
} ,
} ,
}
< link rel = "canonical" href = "https://nextjs.net.cn" />
< link rel = "alternate" hreflang = "en-US" href = "https://nextjs.net.cn/en-US" />
< link rel = "alternate" hreflang = "de-DE" href = "https://nextjs.net.cn/de-DE" />
< link
rel = "alternate"
media = "only screen and (max-width: 600px)"
href = "https://nextjs.net.cn/mobile"
/>
< link
rel = "alternate"
type = "application/rss+xml"
href = "https://nextjs.net.cn/rss"
/>
appLinks
export const metadata = {
appLinks : {
ios : {
url : 'https://nextjs.net.cn/ios' ,
app_store_id : 'app_store_id' ,
} ,
android : {
package : 'com.example.android/package' ,
app_name : 'app_name_android' ,
} ,
web : {
url : 'https://nextjs.net.cn/web' ,
should_fallback : true ,
} ,
} ,
}
< meta property = "al:ios:url" content = "https://nextjs.net.cn/ios" />
< meta property = "al:ios:app_store_id" content = "app_store_id" />
< meta property = "al:android:package" content = "com.example.android/package" />
< meta property = "al:android:app_name" content = "app_name_android" />
< meta property = "al:web:url" content = "https://nextjs.net.cn/web" />
< meta property = "al:web:should_fallback" content = "true" />
archives
描述具有历史意义的记录、文档或其他资料的集合(来源 )。
export const metadata = {
archives : [ 'https://nextjs.net.cn/13' ] ,
}
< link rel = "archives" href = "https://nextjs.net.cn/13" />
assets
export const metadata = {
assets : [ 'https://nextjs.net.cn/assets' ] ,
}
< link rel = "assets" href = "https://nextjs.net.cn/assets" />
bookmarks
export const metadata = {
bookmarks : [ 'https://nextjs.net.cn/13' ] ,
}
< link rel = "bookmarks" href = "https://nextjs.net.cn/13" />
category
export const metadata = {
category : 'technology' ,
}
< meta name = "category" content = "technology" />
facebook
您可以将 Facebook 应用或 Facebook 帐户连接到您的网页,以便使用某些 Facebook 社交插件Facebook 文档
需要注意的是 :您可以指定 appId 或 admins,但不能同时指定两者。
export const metadata = {
facebook : {
appId : '12345678' ,
} ,
}
< meta property = "fb:app_id" content = "12345678" />
export const metadata = {
facebook : {
admins : '12345678' ,
} ,
}
< meta property = "fb:admins" content = "12345678" />
如果要生成多个 fb:admins 元标记,可以使用数组值。
export const metadata = {
facebook : {
admins : [ '12345678' , '87654321' ] ,
} ,
}
< meta property = "fb:admins" content = "12345678" />
< meta property = "fb:admins" content = "87654321" />
other
所有元数据选项都应使用内置支持来涵盖。但是,可能存在特定于您网站的自定义元数据标记,或者刚刚发布的全新元数据标记。您可以使用“其他”选项来呈现任何自定义元数据标记。
export const metadata = {
other : {
custom : 'meta' ,
} ,
}
< meta name = "custom" content = "meta" />
如果要生成多个相同键的元标记,可以使用数组值。
export const metadata = {
other : {
custom : [ 'meta1' , 'meta2' ] ,
} ,
}
< meta name = "custom" content = "meta1" /> < meta name = "custom" content = "meta2" />
不支持的元数据
以下元数据类型当前没有内置支持。但是,它们仍然可以在布局或页面本身中呈现。
资源提示
<link>
元素有许多 rel
关键字,可用于提示浏览器可能需要外部资源。浏览器使用此信息根据关键字应用预加载优化。
虽然元数据 API 不直接支持这些提示,但您可以使用新的 ReactDOM
方法 将它们安全地插入到文档的 <head>
中。
'use client'
import ReactDOM from 'react-dom'
export function PreloadResources () {
ReactDOM .preload ( '...' , { as : '...' })
ReactDOM .preconnect ( '...' , { crossOrigin : '...' })
ReactDOM .prefetchDNS ( '...' )
return '...'
}
<link rel="preload">
在页面渲染(浏览器)生命周期的早期开始加载资源。 MDN 文档 。
ReactDOM .preload (href: string , options: { as : string })
< link rel = "preload" href = "..." as = "..." />
<link rel="preconnect">
预先启动与源的连接。 MDN 文档 。
ReactDOM .preconnect (href: string , options ?: { crossOrigin? : string })
< link rel = "preconnect" href = "..." crossorigin />
<link rel="dns-prefetch">
尝试在请求资源之前解析域名。 MDN 文档 。
ReactDOM .prefetchDNS (href: string)
< link rel = "dns-prefetch" href = "..." />
需知 :
这些方法目前仅在客户端组件中受支持,这些组件在初始页面加载时仍然是服务器端渲染的。
Next.js 的内置功能(如 next/font
、next/image
和 next/script
)会自动处理相关的资源提示。
类型
您可以使用 Metadata
类型为您的元数据添加类型安全。如果您在 IDE 中使用内置的 TypeScript 插件 ,则无需手动添加类型,但如果需要,您仍然可以显式添加它。
metadata
对象
import type { Metadata } from 'next'
export const metadata : Metadata = {
title : 'Next.js' ,
}
generateMetadata
函数 常规函数
import type { Metadata } from 'next'
export function generateMetadata () : Metadata {
return {
title : 'Next.js' ,
}
}
异步函数 import type { Metadata } from 'next'
export async function generateMetadata () : Promise < Metadata > {
return {
title : 'Next.js' ,
}
}
带有段落属性 import type { Metadata } from 'next'
type Props = {
params : { id : string }
searchParams : { [key : string ] : string | string [] | undefined }
}
export function generateMetadata ({ params , searchParams } : Props ) : Metadata {
return {
title : 'Next.js' ,
}
}
export default function Page ({ params , searchParams } : Props ) {}
带有父元数据 import type { Metadata , ResolvingMetadata } from 'next'
export async function generateMetadata (
{ params , searchParams } : Props ,
parent : ResolvingMetadata
) : Promise < Metadata > {
return {
title : 'Next.js' ,
}
}
JavaScript 项目 /** @type {import("next").Metadata} */
export const metadata = {
title : 'Next.js' ,
}
版本历史
版本 更改 v13.2.0
viewport
、themeColor
和 colorScheme
已弃用,取而代之的是 viewport
配置 。v13.2.0
引入了 metadata
和 generateMetadata
。