favicon、icon 和 apple-icon
favicon
、icon
或 apple-icon
檔案命名慣例可讓您為應用程式設定圖示。
它們適用於新增應用程式圖示,這些圖示會出現在網頁瀏覽器分頁、手機主畫面和搜尋引擎結果等位置。
有兩種方式可以設定應用程式圖示
圖片檔案 (.ico、.jpg、.png)
使用圖片檔案設定應用程式圖示,方法是將 favicon
、icon
或 apple-icon
圖片檔案放置在 /app
目錄中。 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
,取決於圖示類型和已評估檔案的 metadata。- 例如,32 x 32 像素的
.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。
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
(選填)
一個物件,其中包含從根區段到 icon
或 apple-icon
所在的區段的動態路由參數物件,會並置在其中。
export default function Icon({ params }: { params: { slug: string } }) {
// ...
}
路由 | URL | params |
---|---|---|
app/shop/icon.js | /shop | 未定義 |
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
。
要知道:
ImageResponse
符合此傳回類型。
Config exports
您可以選擇性地透過從 icon
或 apple-icon
路由匯出 size
和 contentType
變數來設定圖示的 metadata。
選項 | 類型 |
---|---|
size | { width: number; height: number } |
contentType | string - 圖片 MIME 類型 |
size
export const size = { width: 32, height: 32 }
export default function Icon() {}
<link rel="icon" sizes="32x32" />
contentType
export const contentType = 'image/png'
export default function Icon() {}
<link rel="icon" type="image/png" />
路由區段設定
icon
和 apple-icon
是特殊的路由處理器,可以使用與 Pages 和 Layouts 相同的路由區段設定選項。
版本歷史
版本 | 變更 |
---|---|
v13.3.0 | 引入了 favicon 、icon 和 apple-icon |
這有幫助嗎?