React Metadata Files for Images
Sources:
Overview
About
This note is about …
- Favicons
- Icons
- Apple-Icons
- Open-Graph and Twitter Images
- Sitemap.xml
- Robots.txt
- Manifest.json
Images (Favicons, Icons, etc.)
The favicon
, icon
, and apple-icon
file conventions allow one to set icons for the 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:
Image Files
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 will evaluate the file and automatically add the appropriate tags to your app’s <head>
element.
File convention | Supported file types | Valid locations |
---|---|---|
favicon | .ico | app/ |
icon | .ico , .jpg , .jpeg , .png , .svg | app/**/* |
apple-icon | .jpg , .jpeg , .png | app/**/* |
Favicon
Add a favicon.ico
image file to the root /app
route segment.
App Icons
Add an icon.(ico|jpg|jpeg|png|svg)
image file.
Apple Icons
Add an apple-icon.(jpg|jpeg|png)
image file.
Good to know
- 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.- Favicons can only be set in the root
/app
segment. If you need more granularity, you can useicon
.- The appropriate
<link>
tags and attributes such asrel
,href
,type
, andsizes
are determined by the icon type and metadata of the evaluated file.
- For example, a 32 by 32px
.png
file will havetype="image/png"
andsizes="32x32"
attributes.sizes="any"
is added tofavicon.ico
output to avoid a browser bug where an.ico
icon is favored over.svg
.
Generate Icons using Code
In addition to using literal image files, you can programmatically generate icons using code.
Generate an app icon by creating an icon
or apple-icon
route that default exports a function.
File convention | Supported file types |
---|---|
icon | .js , .ts , .tsx |
apple-icon | .js , .ts , .tsx |
Good to Know:
- By default, generated icons are statically optimized (generated at build time and cached) unless they use dynamic functions or uncached data.
- You can generate multiple icons in the same file using
generateImageMetadata
.- You cannot generate a
favicon
icon. Useicon
or a favicon.ico file instead.
Props
The default export function receives the following props:
Params (optional)
An object containing the dynamic route parameters object from the root segment down to the segment icon
or apple-icon
is colocated in.
Route | 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' } |
app/shop/[...slug]/icon.js | /shop/1/2 | { slug: ['1', '2'] } |
Returns
The default export function should return a Blob
| ArrayBuffer
| TypedArray
| DataView
| ReadableStream
| Response
.
Good to know:
ImageResponse
satisfies this return type.
Config Exports
You can optionally configure the icon’s metadata by exporting size
and contentType
variables from the icon
or apple-icon
route.
Option | Type |
---|---|
size | { width: number; height: number } |
contentType | string - image MIME type |
size
icon.tsx | apple-icon.tsx
contentType
Route Segment Config
icon
and apple-icon
are specialized Route Handlers that can use the same route segment configuration options as Pages and Layouts.
See Also
-
React (Tool)
Appendix
Note created on 2024-05-09 and last modified on 2024-05-09.
Backlinks
(c) No Clocks, LLC | 2024