opengraph-image および twitter-image
opengraph-image
および twitter-image
のファイル規約により、ルートセグメントの Open Graph および Twitter 画像を設定できます。
これらは、ユーザーがサイトへのリンクを共有した際に、ソーシャルネットワークやメッセージアプリに表示される画像を設定するのに役立ちます。
Open Graph および Twitter 画像を設定する方法は2つあります。
画像ファイル (.jpg, .png, .gif)
ルートセグメントの共有画像を設定するには、セグメント内に opengraph-image
または twitter-image
の画像ファイルを配置します。
Next.js はファイルを評価し、適切なタグをアプリの <head>
要素に自動的に追加します。
ファイル規約 | サポートされているファイルタイプ |
---|---|
opengraph-image | .jpg , .jpeg , .png , .gif |
twitter-image | .jpg , .jpeg , .png , .gif |
opengraph-image.alt | .txt |
twitter-image.alt | .txt |
知っておくと良いこと:
twitter-image
のファイルサイズは 5MB を超えてはならず、opengraph-image
のファイルサイズは 8MB を超えてはなりません。画像ファイルのサイズがこれらの制限を超えると、ビルドは失敗します。
opengraph-image
任意のルートセグメントに opengraph-image.(jpg|jpeg|png|gif)
画像ファイルを追加します。
<meta property="og:image" content="<generated>" />
<meta property="og:image:type" content="<generated>" />
<meta property="og:image:width" content="<generated>" />
<meta property="og:image:height" content="<generated>" />
twitter-image
任意のルートセグメントに twitter-image.(jpg|jpeg|png|gif)
画像ファイルを追加します。
<meta name="twitter:image" content="<generated>" />
<meta name="twitter:image:type" content="<generated>" />
<meta name="twitter:image:width" content="<generated>" />
<meta name="twitter:image:height" content="<generated>" />
opengraph-image.alt.txt
opengraph-image.(jpg|jpeg|png|gif)
画像と同じルートセグメントに、そのaltテキストである付随する opengraph-image.alt.txt
ファイルを追加します。
About Acme
<meta property="og:image:alt" content="About Acme" />
twitter-image.alt.txt
twitter-image.(jpg|jpeg|png|gif)
画像と同じルートセグメントに、そのaltテキストである付随する twitter-image.alt.txt
ファイルを追加します。
About Acme
<meta property="twitter:image:alt" content="About Acme" />
コードによる画像の生成 (.js, .ts, .tsx)
実際の画像ファイルを使用するだけでなく、コードを使用して画像をプログラム的に生成できます。
opengraph-image
または twitter-image
ルートを作成し、関数をデフォルトエクスポートすることで、ルートセグメントの共有画像を生成します。
ファイル規約 | サポートされているファイルタイプ |
---|---|
opengraph-image | .js , .ts , .tsx |
twitter-image | .js , .ts , .tsx |
知っておくと良いこと:
- デフォルトでは、生成された画像は 静的に最適化されます (ビルド時に生成されキャッシュされます)。ただし、動的 API またはキャッシュされていないデータを使用する場合は除きます。
generateImageMetadata
を使用して、同じファイル内で複数の画像を生成できます。opengraph-image.js
とtwitter-image.js
は特別なルートハンドラであり、動的 API または 動的設定オプションを使用しない限り、デフォルトでキャッシュされます。
画像を生成する最も簡単な方法は、next/og
の ImageResponse API を使用することです。
import { ImageResponse } from 'next/og'
import { readFile } from 'node:fs/promises'
import { join } from 'node:path'
// Image metadata
export const alt = 'About Acme'
export const size = {
width: 1200,
height: 630,
}
export const contentType = 'image/png'
// Image generation
export default async function Image() {
// Font loading, process.cwd() is Next.js project directory
const interSemiBold = await readFile(
join(process.cwd(), 'assets/Inter-SemiBold.ttf')
)
return new ImageResponse(
(
// ImageResponse JSX element
<div
style={{
fontSize: 128,
background: 'white',
width: '100%',
height: '100%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
About Acme
</div>
),
// ImageResponse options
{
// For convenience, we can re-use the exported opengraph-image
// size config to also set the ImageResponse's width and height.
...size,
fonts: [
{
name: 'Inter',
data: interSemiBold,
style: 'normal',
weight: 400,
},
],
}
)
}
<meta property="og:image" content="<generated>" />
<meta property="og:image:alt" content="About Acme" />
<meta property="og:image:type" content="image/png" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
Props
デフォルトのエクスポート関数は以下の props を受け取ります
params
(オプション)
ルートセグメントから opengraph-image
または twitter-image
が配置されているセグメントまでの 動的ルーティングパラメータ オブジェクトを含むオブジェクト。
export default function Image({ params }: { params: { slug: string } }) {
// ...
}
ルート | URL | params |
---|---|---|
app/shop/opengraph-image.js | /shop | 未定義 |
app/shop/[slug]/opengraph-image.js | /shop/1 | { slug: '1' } |
app/shop/[tag]/[item]/opengraph-image.js | /shop/1/2 | { tag: '1', item: '2' } |
戻り値
デフォルトのエクスポート関数は Blob
| ArrayBuffer
| TypedArray
| DataView
| ReadableStream
| Response
を返す必要があります。
知っておくと良いこと:
ImageResponse
はこの戻り型を満たします。
Config エクスポート
opengraph-image
または twitter-image
ルートから alt
、size
、および contentType
変数をエクスポートすることで、画像のメタデータをオプションで設定できます。
オプション | タイプ |
---|---|
alt | string |
size | { width: number; height: number } |
contentType | string - 画像 MIME タイプ |
alt
export const alt = 'My images alt text'
export default function Image() {}
<meta property="og:image:alt" content="My images alt text" />
size
export const size = { width: 1200, height: 630 }
export default function Image() {}
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />
contentType
export const contentType = 'image/png'
export default function Image() {}
<meta property="og:image:type" content="image/png" />
ルートセグメント設定
opengraph-image
および twitter-image
は、ページやレイアウトと同じルートセグメント設定オプションを使用できる、特殊なルートハンドラです。
例
外部データの使用
この例では、params
オブジェクトと外部データを使用して画像を生成します。
知っておくと良いこと: デフォルトでは、この生成された画像は静的に最適化されます。この動作を変更するには、個々の
fetch
options
またはルートセグメントの オプションを設定できます。
import { ImageResponse } from 'next/og'
export const alt = 'About Acme'
export const size = {
width: 1200,
height: 630,
}
export const contentType = 'image/png'
export default async function Image({ params }: { params: { slug: string } }) {
const post = await fetch(`https://.../posts/${params.slug}`).then((res) =>
res.json()
)
return new ImageResponse(
(
<div
style={{
fontSize: 48,
background: 'white',
width: '100%',
height: '100%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
{post.title}
</div>
),
{
...size,
}
)
}
ローカルアセットをNode.jsランタイムで使用する
この例では、Node.js ランタイムを使用してファイルシステムのローカル画像をフェッチし、それを ArrayBuffer
として <img>
要素の src
属性に渡します。ローカルアセットは、例のソースファイルの場所ではなく、プロジェクトのルートからの相対パスで配置する必要があります。
import { ImageResponse } from 'next/og'
import { join } from 'node:path'
import { readFile } from 'node:fs/promises'
export default async function Image() {
const logoData = await readFile(join(process.cwd(), 'logo.png'))
const logoSrc = Uint8Array.from(logoData).buffer
return new ImageResponse(
(
<div
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
<img src={logoSrc} height="100" />
</div>
)
)
}
バージョン履歴
バージョン | 変更点 |
---|---|
v13.3.0 | opengraph-image および twitter-image が導入されました。 |
この情報は役立ちましたか?