Next.js Metadata Files 是 App Router 的一組特殊檔名:把 favicon.ico、icon.png、apple-icon.png、manifest.ts、opengraph-image.png、robots.txt、sitemap.xml 這些檔案放進 app 資料夾的路由段,Next.js 就會自動提供檔案,並在 <head> 加上對應的 <link> 或 <meta>,不必手寫標籤。每一種都可以放靜態檔,也可以用 .js/.ts 程式產生(favicon 例外)。本文以 Next.js 16(範例在 16.4.3 執行)整理各檔案的位置與輸出結果,詳細說明 favicon、icon、apple-icon、manifest,並說明和 metadata 物件同時設定時,誰會蓋過誰。
Metadata Files 一覽
| 檔案慣例 | 放置位置 | 支援的檔案 | 產生的標籤或網址 |
|---|---|---|---|
favicon | 只能在 app/ 最上層 | 靜態:.ico(不能用程式產生) | <link rel="icon"> |
icon | app/ 底下任何路由段 | 靜態:.ico、.jpg、.jpeg、.png、.svg;程式:.js、.ts、.tsx | <link rel="icon"> |
apple-icon | app/ 底下任何路由段 | 靜態:.jpg、.jpeg、.png;程式:.js、.ts、.tsx | <link rel="apple-touch-icon"> |
manifest | app/ 根層 | 靜態:.json、.webmanifest;程式:.js、.ts | <link rel="manifest"> |
opengraph-image、twitter-image | 任何路由段 | 靜態:.jpg、.jpeg、.png、.gif;程式:.js、.ts、.tsx | og:image、twitter:image 等 <meta> |
robots | app/ 根層 | 靜態:.txt;程式:.js、.ts | /robots.txt |
sitemap | app/ 根層 | 靜態:.xml;程式:.js、.ts | /sitemap.xml |
幾個共通規則:
- Next.js 會讀取檔案內容,自動填好網址、檔案類型與圖片尺寸。正式環境的網址會帶雜湊字串,例如
/favicon.ico?favicon.117ezoe8m31dk.ico,檔案內容改了網址就跟著變。 - 用程式產生的
icon.tsx、sitemap.ts、opengraph-image.tsx等檔案是特殊的 Route Handler,預設會快取;用了 Request-time API(例如cookies()、headers())或動態設定時才會在請求時產生。 - 執行
next build時,這些檔案會出現在路由清單,例如○ /manifest.webmanifest、○ /robots.txt、○ /sitemap.xml、○ /tests/nextjs-metadata-files/static/icon.png。
favicon.ico、robots.txt 這類檔案以前常直接放在 public 資料夾,App Router 官方建議改用上面的檔案慣例,public 資料夾的其他規則見 Next.js public 資料夾用法。
favicon.ico:只能放在 app 最上層
把 favicon.ico 放在 app/ 根層(和根 layout.tsx 同一層):
app/
├─ favicon.ico
├─ layout.tsx
└─ page.tsx
所有頁面的 <head> 都會出現:
<link rel="icon" href="/favicon.ico?favicon.117ezoe8m31dk.ico" sizes="48x48" type="image/x-icon"/>
sizes 與 type 由檔案內容決定,範例用的是 create-next-app 附的 favicon.ico,所以輸出 48x48。無法判斷尺寸時,Next.js 會改寫 sizes="any"。
favicon 有兩個限制:
- 只能放在
app/最上層。把favicon.ico放進子資料夾(例如app/shop/favicon.ico),建置不會報錯,但該頁<head>仍然只有根層的 favicon,/shop/favicon.ico也回 404。不同區段要不同圖示時,改用icon。 - 不能用程式產生,沒有
favicon.tsx這種寫法。要動態產生圖示請用icon.tsx。
icon 與 apple-icon:圖片檔放進路由段
icon 與 apple-icon 可以放在任何路由段,只套用在該段與它底下的頁面:
app/
└─ shop/
├─ icon.png (32×32)
├─ apple-icon.png (180×180)
└─ page.tsx
/shop 的 <head> 輸出(favicon 那行來自根層,會一起出現):
<link rel="icon" href="/favicon.ico?favicon.117ezoe8m31dk.ico" sizes="48x48" type="image/x-icon"/>
<link rel="icon" href="/shop/icon.png?icon.1kjy4mk4ynidf.png" sizes="32x32" type="image/png"/>
<link rel="apple-touch-icon" href="/shop/apple-icon.png?apple-icon.2jjyoit8dqkfj.png" sizes="180x180" type="image/png"/>
icon 對應 rel="icon",apple-icon 對應 iOS 加入主畫面時使用的 rel="apple-touch-icon"。type 與 sizes 都是從圖片讀出來的,.png 就是 image/png。
要提供多個尺寸,在檔名後面加數字,例如 app/docs/icon1.png(16×16)與 app/docs/icon2.png(32×32),輸出依檔名的字典序排列:
<link rel="icon" href="/docs/icon1.png?icon1.08re1g4oiuagw.png" sizes="16x16" type="image/png"/>
<link rel="icon" href="/docs/icon2.png?icon2.3-u6g6qt7fi1o.png" sizes="32x32" type="image/png"/>
用程式產生 icon:icon.tsx 搭配 ImageResponse
icon 與 apple-icon 也可以用 .js、.ts、.tsx 產生:預設匯出一個函式,回傳圖片。最簡單的做法是用 next/og 的 ImageResponse,用 JSX 與行內樣式畫出圖示:
// app/icon.tsx
import { ImageResponse } from 'next/og'
export const size = { width: 32, height: 32 }
export const contentType = 'image/png'
export default function Icon() {
return new ImageResponse(
(
<div
style={{
fontSize: 24,
background: '#0f172a',
width: '100%',
height: '100%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
color: '#38bdf8',
}}
>
N
</div>
),
{ ...size }
)
}
size 與 contentType 是選填的設定匯出,Next.js 用它們填 <link> 的 sizes 與 type;這裡同時把 size 傳給 ImageResponse 當作圖片寬高。輸出:
<link rel="icon" href="/icon?2bd1c54da39e9875" type="image/png" sizes="32x32"/>
網址是 /icon(沒有副檔名)加上雜湊參數,請求回應的 Content-Type 是 image/png。這個函式沒有用到 Request-time API,所以在 next build 時就產生成靜態檔(路由清單標示 ○)。
幾個補充:
- 函式可以接收
params(動態路由參數)。Next.js 16 起params是 Promise,要await params才能取值,例如app/shop/[slug]/icon.tsx裡const { slug } = await params。動態路由的寫法見 Next.js 動態路由 Dynamic Segments 用法。 - 回傳值可以是
Blob、ArrayBuffer、TypedArray、DataView、ReadableStream或Response,ImageResponse符合這個型別。 - 同一個檔案要產生多個圖示,搭配
generateImageMetadata。 icon.tsx本質上是 Route Handler,可以使用和 page、layout 相同的 Route Segment Config。
manifest:manifest.json 或 manifest.ts
Web App Manifest 讓瀏覽器知道網站名稱、主題色、圖示等資訊(例如安裝成 PWA 時使用)。在 app/ 根層放 manifest.json 或 manifest.webmanifest,或用 manifest.ts 回傳 MetadataRoute.Manifest 物件:
// app/manifest.ts
import type { MetadataRoute } from 'next'
export default function manifest(): MetadataRoute.Manifest {
return {
name: '我的 Next.js 應用程式',
short_name: 'NextApp',
description: 'Metadata Files 範例',
start_url: '/',
display: 'standalone',
background_color: '#ffffff',
theme_color: '#2563eb',
icons: [
{
src: '/favicon.ico',
sizes: 'any',
type: 'image/x-icon',
},
],
}
}
建置後網址是 /manifest.webmanifest,回應 Content-Type: application/manifest+json,內容就是函式回傳的 JSON:
{"name":"我的 Next.js 應用程式","short_name":"NextApp","description":"Metadata Files 範例","start_url":"/","display":"standalone","background_color":"#ffffff","theme_color":"#2563eb","icons":[{"src":"/favicon.ico","sizes":"any","type":"image/x-icon"}]}
每個頁面的 <head> 都會加上:
<link rel="manifest" href="/manifest.webmanifest"/>
MetadataRoute.Manifest 可用的欄位會隨 Web 標準更新,完整清單看編輯器的 TypeScript 型別提示或 MDN 的 Web App Manifest 文件。manifest.ts 同樣是預設快取的特殊 Route Handler。
opengraph-image、twitter-image、robots.txt、sitemap.xml
這四種檔案各自有較多設定,這裡只說明放法與輸出,讓你知道它們屬於同一套 Metadata Files。
opengraph-image 與 twitter-image:分享連結到社群平台或通訊軟體時顯示的預覽圖。放在任何路由段,例如 app/blog/opengraph-image.png,旁邊可以放同名的 opengraph-image.alt.txt 當替代文字。1200×630 的 PNG 會輸出:
<meta property="og:image" content="http://localhost:3000/blog/opengraph-image.png?opengraph-image.22wdgbd0jl4rc.png"/>
<meta property="og:image:type" content="image/png"/>
<meta property="og:image:width" content="1200"/>
<meta property="og:image:height" content="630"/>
<meta property="og:image:alt" content="Next.js Metadata Files 範例頁"/>
og:image 必須是完整網址。上面的範例沒有設定 metadataBase,next build 會出現警告:
⚠ metadataBase property in metadata export is not set for resolving social open graph or twitter images, using "http://localhost:3000".
網址前綴因此變成 http://localhost:3000(伺服器實際跑在別的連接埠也一樣),正式站要在根 app/layout.tsx 的 metadata 設定 metadataBase: new URL('https://你的網域')。另外 twitter-image 檔案不得超過 5MB、opengraph-image 不得超過 8MB,超過時建置會失敗。
robots.txt:放在 app/ 根層,可以直接寫 robots.txt,或用 robots.ts 回傳物件:
// app/robots.ts
import type { MetadataRoute } from 'next'
export default function robots(): MetadataRoute.Robots {
return {
rules: { userAgent: '*', allow: '/', disallow: '/private/' },
sitemap: 'https://example.com/sitemap.xml',
}
}
/robots.txt 的輸出:
User-Agent: *
Allow: /
Disallow: /private/
Sitemap: https://example.com/sitemap.xml
sitemap.xml:放在 app/ 根層,可以直接寫 sitemap.xml,或用 sitemap.ts 回傳網址陣列:
// app/sitemap.ts
import type { MetadataRoute } from 'next'
export default function sitemap(): MetadataRoute.Sitemap {
return [
{ url: 'https://example.com', lastModified: new Date('2026-11-01') },
{ url: 'https://example.com/blog', lastModified: new Date('2026-11-01') },
]
}
/sitemap.xml 回應 Content-Type: application/xml:
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
<url>
<loc>https://example.com</loc>
<lastmod>2026-11-01T00:00:00.000Z</lastmod>
</url>
<url>
<loc>https://example.com/blog</loc>
<lastmod>2026-11-01T00:00:00.000Z</lastmod>
</url>
</urlset>
和 metadata 物件同時設定時:icons 不要兩邊都寫
官方 generateMetadata 文件寫的是:檔案式 metadata 優先權較高,會覆寫 metadata 物件與 generateMetadata。不過在 Next.js 16.4.3,icons 的實際行為不是這樣:
| 設定方式 | 輸出結果 |
|---|---|
同一段放 icon.png,page 的 metadata 設 icons: { icon: '/from-metadata-object.png' } | 只有 /from-metadata-object.png,icon.png 沒有輸出 |
同一段放 icon.png,該段 layout 的 metadata 設 icons | 只有 layout 設的圖示,icon.png 沒有輸出 |
上層 layout 設 icons,子路由段放 icon.png | 只有上層 layout 設的圖示,icon.png 沒有輸出 |
上層 layout 設 openGraph.images,子路由段放 opengraph-image.png | 子路由段的 opengraph-image.png 生效 |
同一段放 opengraph-image.png,page 的 metadata 設 openGraph.images | page 設的圖片生效 |
icons 的原因在 Next.js 原始碼 resolve-metadata.ts:最末段收集到的 icon 檔,只在解析完的 metadata 沒有 icons 時才會寫入(if (!resolvedMetadata.icons))。所以整條路由上只要任何一層的 metadata 設過 icons,icon.png、apple-icon.png 就不會出現,而且建置不會有任何警告。
實務上的做法:
- 圖示統一用檔案慣例(
favicon.ico、icon.png、apple-icon.png),metadata物件不要寫icons。官方也建議 icons 盡量用檔案式寫法,省去手動同步設定與實際檔案。 - 同一個路由段不要同時放
opengraph-image.png又在 page 設openGraph.images,選一種就好。
搭配 proxy.ts 時排除 metadata 檔
proxy.ts(Next.js 16 起取代 middleware.ts)沒有設定 matcher 時,每個請求都會經過它,包含 /favicon.ico、/robots.txt、/sitemap.xml。官方建議在 matcher 排除 metadata 檔,官方範例的排除清單就列了 favicon.ico、sitemap.xml、robots.txt。如果站上有 /manifest.webmanifest 或 icon.png,記得一併排除,否則 proxy 裡的登入檢查或重新導向也會套用到這些檔案的請求。matcher 的寫法見 Next.js proxy.js 用法,舊版 middleware 的概念可參考 Next.js Middleware:掌握請求攔截與修改。
參考資料與延伸閱讀
- Next.js Docs: Metadata Files API Reference
- Next.js Docs: favicon, icon, and apple-icon
- Next.js Docs: manifest.json
- Next.js Docs: opengraph-image and twitter-image
- Next.js Docs: robots.txt
- Next.js Docs: sitemap.xml
- Next.js Docs: Metadata and OG images
- Next.js Docs: generateMetadata
- Next.js Docs: proxy.js
- Next.js 原始碼 v16.4.3:resolve-metadata.ts
- MDN: Web app manifests
- Next.js public 資料夾用法:靜態檔案路徑、Cache-Control 快取、basePath 與 robots.txt 放置位置
- Next.js proxy.js 用法:取代 middleware 的 proxy 函式、matcher 設定與 Node.js runtime
- Next.js 動態路由 Dynamic Segments 用法:[slug]、[…slug]、[[…slug]] 與 generateStaticParams
- Next.js Middleware:掌握請求攔截與修改
發佈留言