N Next.js 中文
原文 ↗
Next.js 文档/API Reference / API 参考/Metadata Files / 元数据文件

favicon、icon 和 apple-icon

Favicon、Icon 和 Apple Icon 文件约定的 API 参考。

更新于 2026-03-03Next.js 16.4.6

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 路由段。

<head> outputhtml
<link rel="icon" href="/favicon.ico" sizes="any" />

icon

添加 icon.(ico|jpg|jpeg|png|svg) 图像文件。

<head> outputhtml
<link
  rel="icon"
  href="/icon?<generated>"
  type="image/<generated>"
  sizes="<generated>"
/>

apple-icon

添加 apple-icon.(jpg|jpeg|png) 图像文件。

<head> outputhtml
<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

app/icon.tsxtsx
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

app/icon.jsjsx
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,
    }
  )
}
<head> outputhtml
<link rel="icon" href="/icon?<generated>" type="image/png" sizes="32x32" />

须知:

Props

默认导出函数接收以下 props:

params(可选)

一个 Promise,其解析结果是一个对象,该对象包含从根段一直到 icon 或 apple-icon 所在段的动态路由参数对象。

须知:

如果你使用 generateImageMetadata,该函数还会接收一个 id prop,它是一个 Promise,会解析为 generateImageMetadata 返回的其中一项中的 id 值。

TypeScript

app/shop/[slug]/icon.tsxtsx
export default async function Icon({
  params,
}: {
  params: Promise<{ slug: string }>
}) {
  const { slug } = await params
  // ...
}

JavaScript

app/shop/[slug]/icon.jsjsx
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

icon.tsx | apple-icon.tsxtsx
export const size = { width: 32, height: 32 }

export default function Icon() {}

JavaScript

icon.js | apple-icon.jsjsx
export const size = { width: 32, height: 32 }

export default function Icon() {}
<head> outputhtml
<link rel="icon" sizes="32x32" />

contentType

TypeScript

icon.tsx | apple-icon.tsxtsx
export const contentType = 'image/png'

export default function Icon() {}

JavaScript

icon.js | apple-icon.jsjsx
export const contentType = 'image/png'

export default function Icon() {}
<head> outputhtml
<link rel="icon" type="image/png" />

路由段配置

icon 和 apple-icon 是专门的路由处理器,可以使用与页面和布局相同的路由段配置选项。

版本历史

版本 变更
v16.0.0 params 现在是一个会解析为对象的 Promise
v13.3.0 引入 favicon、icon 和 apple-icon