主题
网站图标、图标和苹果图标
¥favicon, icon, and apple-icon
favicon、icon 或 apple-icon 文件约定允许你为应用设置图标。
¥The favicon, icon, or apple-icon file conventions allow you to set icons for your application.
它们对于添加出现在网络浏览器选项卡、手机主屏幕和搜索引擎结果等位置的应用图标非常有用。
¥They are useful for adding app icons that appear in places like web browser tabs, phone home screens, and search engine results.
设置应用图标有两种方法:
¥There are two ways to set app icons:
图片文件(.ico、.jpg、.png)
¥Image files (.ico, .jpg, .png)
通过将 favicon、icon 或 apple-icon 图片文件放入 /app 目录中,可以使用图片文件设置应用图标。favicon 映像只能位于 app/ 的顶层。
¥Use an image file to set an app icon by placing a favicon, icon, or apple-icon image file within your /app directory. The favicon image can only be located in the top level of app/.
Next.js 将评估该文件并自动将适当的标签添加到应用的 <head> 元素中。
¥Next.js will evaluate the file and automatically add the appropriate tags to your app's <head> element.
| 文件约定 | 支持的文件类型 | 有效位置 |
|---|---|---|
favicon | .ico | app/ |
icon | .ico、.jpg、.jpeg、.png、.svg | app/**/* |
apple-icon | .jpg, .jpeg, .png | app/**/* |
favicon
将 favicon.ico 映像文件添加到根 /app 航路段。
¥Add a favicon.ico image file to the root /app route segment.
html
<link rel="icon" href="/favicon.ico" sizes="any" />icon
添加 icon.(ico|jpg|jpeg|png|svg) 图片文件。
¥Add an icon.(ico|jpg|jpeg|png|svg) image file.
html
<link
rel="icon"
href="/icon?<generated>"
type="image/<generated>"
sizes="<generated>"
/>apple-icon
添加 apple-icon.(jpg|jpeg|png) 图片文件。
¥Add an apple-icon.(jpg|jpeg|png) image file.
html
<link
rel="apple-touch-icon"
href="/apple-icon?<generated>"
type="image/<generated>"
sizes="<generated>"
/>需要了解:
¥Good to know:
你可以通过在文件名中添加数字后缀来设置多个图标。例如,
icon1.png、icon2.png等。编号文件将按词法排序。¥You can set multiple icons by adding a number suffix to the file name. For example,
icon1.png,icon2.png, etc. Numbered files will sort lexically.网站图标只能设置在根
/app段中。如果需要更细粒度,可以使用icon。¥Favicons can only be set in the root
/appsegment. If you need more granularity, you can useicon.适当的
<link>标签和属性(例如rel、href、type和sizes)由评估文件的图标类型和元数据确定。¥The appropriate
<link>tags and attributes such asrel,href,type, andsizesare determined by the icon type and metadata of the evaluated file.例如,32 x 32px
.png文件将具有type="image/png"和sizes="32x32"属性。¥For example, a 32 by 32px
.pngfile will havetype="image/png"andsizes="32x32"attributes.当扩展名为
.svg或文件的图片大小未确定时,sizes="any"会添加到图标中。此 图标手册 中有更多详细信息。¥
sizes="any"is added to icons when the extension is.svgor the image size of the file is not determined. More details in this favicon handbook.
使用代码(.js、.ts、.tsx)生成图标
¥Generate icons using code (.js, .ts, .tsx)
除了使用 字面量图片文件 之外,你还可以使用代码以编程方式生成图标。
¥In addition to using literal image files, you can programmatically generate icons using code.
通过创建默认导出函数的 icon 或 apple-icon 路由来生成应用图标。
¥Generate an app icon by creating an icon or apple-icon route that default exports a function.
| 文件约定 | 支持的文件类型 |
|---|---|
icon | .js, .ts, .tsx |
apple-icon | .js, .ts, .tsx |
生成图标的最简单方法是使用 next/og 中的 ImageResponse API。
¥The easiest way to generate an icon is to use the ImageResponse API from next/og.
tsx
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,
}
)
}html
<link rel="icon" href="/icon?<generated>" type="image/png" sizes="32x32" />需要了解:
¥Good to know:
默认情况下,生成的图标为 静态优化(在构建时生成并缓存),除非它们使用 动态 API 或未缓存的数据。
¥By default, generated icons are statically optimized (generated at build time and cached) unless they use Dynamic APIs or uncached data.
你可以使用
generateImageMetadata在同一个文件中生成多个图标。¥You can generate multiple icons in the same file using
generateImageMetadata.你无法生成
favicon图标。请改用icon或 favicon.ico 文件。¥You cannot generate a
faviconicon. Useiconor a favicon.ico file instead.应用图标是特殊的路由处理程序,除非使用 动态 API 或 动态配置 选项,否则默认情况下会被缓存。
¥App icons are special Route Handlers that are cached by default unless they use a Dynamic API or dynamic config option.
属性
¥Props
默认导出函数接收以下属性:
¥The default export function receives the following props:
params(可选)
¥params (optional)
包含从根段到段 icon 或 apple-icon 的 动态路由参数 对象的对象位于同一位置。
¥An object containing the dynamic route parameters object from the root segment down to the segment icon or apple-icon is colocated in.
tsx
export default function Icon({ params }: { params: { slug: string } }) {
// ...
}| 路由 | URL | params |
|---|---|---|
app/shop/icon.js | /shop | undefined |
app/shop/[slug]/icon.js | /shop/1 | { slug: '1' } |
app/shop/[tag]/[item]/icon.js | /shop/1/2 | { tag: '1', item: '2' } |
返回
¥Returns
默认导出函数应返回 Blob | ArrayBuffer | TypedArray | DataView | ReadableStream | Response。
¥The default export function should return a Blob | ArrayBuffer | TypedArray | DataView | ReadableStream | Response.
需要了解:
ImageResponse满足此返回类型。¥Good to know:
ImageResponsesatisfies this return type.
配置导出
¥Config exports
你可以选择通过从 icon 或 apple-icon 路由导出 size 和 contentType 变量来配置图标的元数据。
¥You can optionally configure the icon's metadata by exporting size and contentType variables from the icon or apple-icon route.
| 选项 | 类型 |
|---|---|
size | { width: number; height: number } |
contentType | string - 图片 MIME 类型 |
size
tsx
export const size = { width: 32, height: 32 }
export default function Icon() {}html
<link rel="icon" sizes="32x32" />contentType
tsx
export const contentType = 'image/png'
export default function Icon() {}html
<link rel="icon" type="image/png" />路由片段配置
¥Route Segment Config
icon 和 apple-icon 是专门的 路由处理程序,可以使用与页面和布局相同的 路由段配置 选项。
¥icon and apple-icon are specialized Route Handlers that can use the same route segment configuration options as Pages and Layouts.
版本历史
¥Version History
| 版本 | 更改 |
|---|---|
v13.3.0 | favicon icon 和 apple-icon 推出 |