favicon、icon 和 apple-icon
Favicon、Icon 和 Apple Icon 文件约定的 API 参考。
favicon、icon 或 apple-icon 文件约定允许你为应用设置图标。
它们适合用于添加应用图标,这些图标会显示在 Web 浏览器标签页、手机主屏幕和搜索结果等位置。
设置应用图标有两种方式:
图像文件(.ico、.jpg、.png)
通过在 /app 目录中放置 favicon、icon 或 apple-icon 图像文件,即可使用图像文件设置应用图标。
favicon 图像只能位于 app/ 的顶层。
Next.js 会评估该文件,并自动向应用的 <head> 元素添加适当的标签。
| 文件约定 | 支持的文件类型 | 有效位置 |
|---|---|---|
favicon |
.ico |
app/ |
icon |
.ico, .jpg, .jpeg, .png, .svg |
app/**/* |
apple-icon |
.jpg, .jpeg, .png |
app/**/* |
favicon
将 favicon.ico 图像文件添加到根 /app 路由段。
<link rel="icon" href="/favicon.ico" sizes="any" />
icon
添加 icon.(ico|jpg|jpeg|png|svg) 图像文件。
<link
rel="icon"
href="/icon?<generated>"
type="image/<generated>"
sizes="<generated>"
/>
apple-icon
添加 apple-icon.(jpg|jpeg|png) 图像文件。
<link
rel="apple-touch-icon"
href="/apple-icon?<generated>"
type="image/<generated>"
sizes="<generated>"
/>
须知:
- 你可以通过在文件名后添加数字后缀来设置多个图标。例如
icon1.png、icon2.png等。带编号的文件将按字典序排序。- Favicon 只能在根
/app段中设置。如果你需要更细粒度的控制,可以使用icon。- 相应的
<link>标签和属性(如rel、href、type和sizes)由所评估文件的图标类型和元数据决定。- 例如,32 x 32px 的
.png文件将具有type="image/png"和sizes="32x32"属性。- 当扩展名为
.svg或无法确定文件的图像尺寸时,会向图标添加sizes="any"。更多详情请参阅这份 favicon 手册。
使用代码生成图标(.js、.ts、.tsx)
除了使用 字面图像文件,你还可以通过代码以编程方式生成图标。
通过创建默认导出一个函数的 icon 或 apple-icon 路由来生成应用图标。
| 文件约定 | 支持的文件类型 |
|---|---|
icon |
.js, .ts, .tsx |
apple-icon |
.js, .ts, .tsx |
生成图标最简单的方法是使用 next/og 中的 ImageResponse API。
TypeScript
import { ImageResponse } from 'next/og'
// Image metadata
export const size = {
width: 32,
height: 32,
}
export const contentType = 'image/png'
// Image generation
export default function Icon() {
return new ImageResponse(
(
// ImageResponse JSX element
<div
style={{
fontSize: 24,
background: 'black',
width: '100%',
height: '100%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
color: 'white',
}}
>
A
</div>
),
// ImageResponse options
{
// For convenience, we can re-use the exported icons size metadata
// config to also set the ImageResponse's width and height.
...size,
}
)
}
JavaScript
import { ImageResponse } from 'next/og'
// Image metadata
export const size = {
width: 32,
height: 32,
}
export const contentType = 'image/png'
// Image generation
export default function Icon() {
return new ImageResponse(
(
// ImageResponse JSX element
<div
style={{
fontSize: 24,
background: 'black',
width: '100%',
height: '100%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
color: 'white',
}}
>
A
</div>
),
// ImageResponse options
{
// For convenience, we can re-use the exported icons size metadata
// config to also set the ImageResponse's width and height.
...size,
}
)
}
<link rel="icon" href="/icon?<generated>" type="image/png" sizes="32x32" />
须知:
- 默认情况下,生成的图标会进行静态优化(在构建时生成并缓存),除非它们使用请求时 API 或未缓存的数据。
- 你可以使用
generateImageMetadata在同一文件中生成多个图标。- 你无法生成
favicon图标。请改用icon或 favicon.ico 文件。- 应用图标是特殊的路由处理器,默认会被缓存,除非它们使用请求时 API 或动态配置 选项。
Props
默认导出函数接收以下 props:
params(可选)
一个 Promise,其解析结果是一个对象,该对象包含从根段一直到 icon 或 apple-icon 所在段的动态路由参数对象。
须知:
如果你使用
generateImageMetadata,该函数还会接收一个idprop,它是一个 Promise,会解析为generateImageMetadata返回的其中一项中的id值。
TypeScript
export default async function Icon({
params,
}: {
params: Promise<{ slug: string }>
}) {
const { slug } = await params
// ...
}
JavaScript
export default async function Icon({ params }) {
const { slug } = await params
// ...
}
| 路由 | URL | params |
|---|---|---|
app/shop/icon.js |
/shop |
undefined |
app/shop/[slug]/icon.js |
/shop/1 |
Promise<{ slug: '1' }> |
app/shop/[tag]/[item]/icon.js |
/shop/1/2 |
Promise<{ tag: '1', item: '2' }> |
返回值
默认导出函数应返回 Blob | ArrayBuffer | TypedArray | DataView | ReadableStream | Response。
须知:
ImageResponse满足此返回类型。
配置导出项
你可以选择在 icon 或 apple-icon 路由中导出 size 和 contentType 变量,以此来配置图标的元数据。
| 选项 | 类型 |
|---|---|
size |
{ width: number; height: number } |
contentType |
string - 图片 MIME 类型 |
size
TypeScript
export const size = { width: 32, height: 32 }
export default function Icon() {}
JavaScript
export const size = { width: 32, height: 32 }
export default function Icon() {}
<link rel="icon" sizes="32x32" />
contentType
TypeScript
export const contentType = 'image/png'
export default function Icon() {}
JavaScript
export const contentType = 'image/png'
export default function Icon() {}
<link rel="icon" type="image/png" />
路由段配置
icon 和 apple-icon 是专门的路由处理器,可以使用与页面和布局相同的路由段配置选项。
版本历史
| 版本 | 变更 |
|---|---|
v16.0.0 |
params 现在是一个会解析为对象的 Promise |
v13.3.0 |
引入 favicon、icon 和 apple-icon |