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)
画像と同じルートセグメントに、opengraph-image.alt.txt
ファイルを追加して代替テキストを設定します。
About Acme
<meta property="og:image:alt" content="About Acme" />
twitter-image.alt.txt
twitter-image.(jpg|jpeg|png|gif)
画像と同じルートセグメントに、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 |
知っておくと良いこと:
- デフォルトでは、生成された画像は 静的最適化 されます(ビルド時に生成されキャッシュされます)。ただし、Dynamic APIs やキャッシュされていないデータを使用する場合は除きます。
generateImageMetadata
を使用して、同じファイル内で複数の画像を生成できます。opengraph-image.js
とtwitter-image.js
は特別な Route Handlers で、Dynamic API や dynamic config オプションを使用しない限り、デフォルトでキャッシュされます。
画像を生成する最も簡単な方法は、next/og
の ImageResponse API を使用することです。
import { ImageResponse } from 'next/og'
import { readFile } from 'node:fs/promises'
import { join } from 'node:path'
// 画像メタデータ
export const alt = 'About Acme'
export const size = {
width: 1200,
height: 630,
}
export const contentType = 'image/png'
// 画像生成
export default async function Image() {
// フォント読み込み、process.cwd() は Next.js プロジェクトディレクトリ
const interSemiBold = await readFile(
join(process.cwd(), 'assets/Inter-SemiBold.ttf')
)
return new ImageResponse(
(
// ImageResponse JSX 要素
<div
style={{
fontSize: 128,
background: 'white',
width: '100%',
height: '100%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
About Acme
</div>
),
// ImageResponse オプション
{
// 便利のために、エクスポートされた opengraph-image の
// サイズ設定を ImageResponse の幅と高さにも再利用できます
...size,
fonts: [
{
name: 'Inter',
data: interSemiBold,
style: 'normal',
weight: 400,
},
],
}
)
}
import { ImageResponse } from 'next/og'
import { readFile } from 'node:fs/promises'
import { join } from 'node:path'
// 画像メタデータ
export const alt = 'About Acme'
export const size = {
width: 1200,
height: 630,
}
export const contentType = 'image/png'
// 画像生成
export default async function Image() {
// フォント読み込み、process.cwd() は Next.js プロジェクトディレクトリ
const interSemiBold = await readFile(
join(process.cwd(), 'assets/Inter-SemiBold.ttf')
)
return new ImageResponse(
(
// ImageResponse JSX 要素
<div
style={{
fontSize: 128,
background: 'white',
width: '100%',
height: '100%',
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
About Acme
</div>
),
// ImageResponse オプション
{
// 便利のために、エクスポートされた opengraph-image の
// サイズ設定を ImageResponse の幅と高さにも再利用できます
...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 } }) {
// ...
}
export default function Image({ params }) {
// ...
}
ルート | URL | params |
---|---|---|
app/shop/opengraph-image.js | /shop | undefined |
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
はこの戻り値の型を満たします。
設定エクスポート
opengraph-image
または twitter-image
ルートから alt
、size
、contentType
変数をエクスポートすることで、画像のメタデータをオプションで設定できます。
オプション | 型 |
---|---|
alt | string |
size | { width: number; height: number } |
contentType | string - 画像 MIME タイプ |
alt
export const alt = '画像の代替テキスト'
export default function Image() {}
export const alt = '画像の代替テキスト'
export default function Image() {}
<meta property="og:image:alt" content="画像の代替テキスト" />
size
export const size = { width: 1200, height: 630 }
export default function Image() {}
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() {}
export const contentType = 'image/png'
export default function Image() {}
<meta property="og:image:type" content="image/png" />
ルートセグメント設定
opengraph-image
と twitter-image
は特殊な Route Handlers で、Pages や Layouts と同じ ルートセグメント設定 オプションを使用できます。
例
外部データを使用
この例では、params
オブジェクトと外部データを使用して画像を生成しています。
知っておくと良いこと: デフォルトでは、この生成された画像は 静的に最適化 されます。個々の
fetch
options
やルートセグメントの 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,
}
)
}
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 }) {
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 ランタイムを使用してファイルシステム上のローカル画像を取得し、<img>
要素の src
属性に ArrayBuffer
として渡しています。ローカルアセットは、ソースファイルの場所ではなく、プロジェクトのルートディレクトリに対して相対的に配置する必要があります。
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>
)
)
}
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 が導入されました。 |