opengraph-image 與 twitter-image

opengraph-imagetwitter-image 檔案約定允許您為路由區段設定 Open Graph 和 Twitter 圖片。

這些設定對於當使用者在社交網路和通訊應用中分享您網站的連結時,顯示的圖片非常有用。

設定 Open Graph 和 Twitter 圖片有兩種方式:

圖片檔案 (.jpg, .png, .gif)

透過在路由區段中放置 opengraph-imagetwitter-image 圖片檔案,來設定該區段的分享圖片。

Next.js 會評估該檔案並自動將適當的標籤添加到您應用的 <head> 元素中。

檔案約定支援的檔案類型
opengraph-image.jpg, .jpeg, .png, .gif
twitter-image.jpg, .jpeg, .png, .gif
opengraph-image.alt.txt
twitter-image.alt.txt

opengraph-image

在任何路由區段中添加 opengraph-image.(jpg|jpeg|png|gif) 圖片檔案。

<head> output
<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) 圖片檔案。

<head> output
<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 檔案來設定其替代文字。

opengraph-image.alt.txt
關於 Acme
<head> output
<meta property="og:image:alt" content="關於 Acme" />

twitter-image.alt.txt

在與 twitter-image.(jpg|jpeg|png|gif) 圖片相同的路由區段中,添加一個 twitter-image.alt.txt 檔案來設定其替代文字。

twitter-image.alt.txt
關於 Acme
<head> output
<meta property="twitter:image:alt" content="關於 Acme" />

使用程式碼生成圖片 (.js, .ts, .tsx)

除了使用實際圖片檔案外,您還可以透過程式碼生成圖片。

透過建立一個預設導出函式的 opengraph-imagetwitter-image 路由,來生成路由區段的分享圖片。

檔案約定支援的檔案類型
opengraph-image.js, .ts, .tsx
twitter-image.js, .ts, .tsx

須知

  • 預設情況下,生成的圖片會進行靜態優化(在構建時生成並緩存),除非它們使用了動態函式或未緩存的資料。
  • 您可以在同一檔案中使用 generateImageMetadata 生成多個圖片。

生成圖片最簡單的方法是使用 next/og 中的 ImageResponse API。

import { ImageResponse } from 'next/og'

// 路由區段配置
export const runtime = 'edge'

// 圖片元數據
export const alt = '關於 Acme'
export const size = {
  width: 1200,
  height: 630,
}

export const contentType = 'image/png'

// 圖片生成
export default async function Image() {
  // 字體
  const interSemiBold = fetch(
    new URL('./Inter-SemiBold.ttf', import.meta.url)
  ).then((res) => res.arrayBuffer())

  return new ImageResponse(
    (
      // ImageResponse JSX 元素
      <div
        style={{
          fontSize: 128,
          background: 'white',
          width: '100%',
          height: '100%',
          display: 'flex',
          alignItems: 'center',
          justifyContent: 'center',
        }}
      >
        關於 Acme
      </div>
    ),
    // ImageResponse 選項
    {
      // 為方便起見,我們可以重複使用導出的 opengraph-image
      // 尺寸配置來設定 ImageResponse 的寬度和高度。
      ...size,
      fonts: [
        {
          name: 'Inter',
          data: await interSemiBold,
          style: 'normal',
          weight: 400,
        },
      ],
    }
  )
}
<head> output
<meta property="og:image" content="<generated>" />
<meta property="og:image:alt" content="關於 Acme" />
<meta property="og:image:type" content="image/png" />
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />

屬性

預設導出函式接收以下屬性:

params (可選)

一個物件,包含從根區段到 opengraph-imagetwitter-image 所在區段的動態路由參數物件。

export default function Image({ params }: { params: { slug: string } }) {
  // ...
}
路由URLparams
app/shop/opengraph-image.js/shopundefined
app/shop/[slug]/opengraph-image.js/shop/1{ slug: '1' }
app/shop/[tag]/[item]/opengraph-image.js/shop/1/2{ tag: '1', item: '2' }
app/shop/[...slug]/opengraph-image.js/shop/1/2{ slug: ['1', '2'] }

返回值

預設導出函式應返回 Blob | ArrayBuffer | TypedArray | DataView | ReadableStream | Response 其中之一。

須知ImageResponse 符合此返回類型。

配置導出

您可以選擇性地透過從 opengraph-imagetwitter-image 路由導出 altsizecontentType 變數來配置圖片的元數據。

選項類型
altstring
size{ width: number; height: number }
contentTypestring - 圖片 MIME 類型

alt

export const alt = '我的圖片替代文字'

export default function Image() {}
<head> output
<meta property="og:image:alt" content="我的圖片替代文字" />

size

export const size = { width: 1200, height: 630 }

export default function Image() {}
<head> output
<meta property="og:image:width" content="1200" />
<meta property="og:image:height" content="630" />

contentType

export const contentType = 'image/png'

export default function Image() {}
<head> output
<meta property="og:image:type" content="image/png" />

路由區段配置

opengraph-imagetwitter-image 是特殊的路由處理器,可以使用與頁面和佈局相同的路由區段配置選項。

選項類型預設值
dynamic'auto' | 'force-dynamic' | 'error' | 'force-static''auto'
revalidatefalse | 'force-cache' | 0 | numberfalse
runtime'nodejs' | 'edge''nodejs'
preferredRegion'auto' | 'global' | 'home' | string | string[]'auto'
export const runtime = 'edge'

export default function Image() {}

範例

使用外部資料

此範例使用 params 物件和外部資料來生成圖片。

須知: 預設情況下,此生成的圖片會進行靜態優化。您可以配置個別的 fetch 選項 或路由區段的選項來改變此行為。

import { ImageResponse } from 'next/og'

export const runtime = 'edge'

export const alt = '關於 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,
    }
  )
}

版本歷史

版本變更
v13.3.0引入 opengraph-imagetwitter-image

On this page