Unverified Commit e0ed599f authored by かがみ's avatar かがみ Committed by GitHub

docs: add typescript samples on using local assets for opengraph/twitter image (#64947)

Co-authored-by: 's avatarLee Robinson <me@leerob.io>
parent 1b6951ea
...@@ -93,6 +93,8 @@ The easiest way to generate an image is to use the [ImageResponse](/docs/app/api ...@@ -93,6 +93,8 @@ The easiest way to generate an image is to use the [ImageResponse](/docs/app/api
```tsx filename="app/about/opengraph-image.tsx" switcher ```tsx filename="app/about/opengraph-image.tsx" switcher
import { ImageResponse } from 'next/og' import { ImageResponse } from 'next/og'
export const runtime = 'edge'
// Image metadata // Image metadata
export const alt = 'About Acme' export const alt = 'About Acme'
export const size = { export const size = {
...@@ -147,6 +149,8 @@ export default async function Image() { ...@@ -147,6 +149,8 @@ export default async function Image() {
```jsx filename="app/about/opengraph-image.js" switcher ```jsx filename="app/about/opengraph-image.js" switcher
import { ImageResponse } from 'next/og' import { ImageResponse } from 'next/og'
export const runtime = 'edge'
// Image metadata // Image metadata
export const alt = 'About Acme' export const alt = 'About Acme'
export const size = { export const size = {
...@@ -397,13 +401,38 @@ export default async function Image({ params }) { ...@@ -397,13 +401,38 @@ export default async function Image({ params }) {
This example uses the Edge runtime to fetch a local image on the file system and passes it as an `ArrayBuffer` to the `src` attribute of an `<img>` element. The local asset should be placed relative to the example source file location. This example uses the Edge runtime to fetch a local image on the file system and passes it as an `ArrayBuffer` to the `src` attribute of an `<img>` element. The local asset should be placed relative to the example source file location.
```tsx filename="app/opengraph-image.tsx" switcher
import { ImageResponse } from 'next/og'
export const runtime = 'edge'
export default async function Image() {
const logoSrc = await fetch(new URL('./logo.png', import.meta.url)).then(
(res) => res.arrayBuffer()
)
return new ImageResponse(
(
<div
style={{
display: 'flex',
alignItems: 'center',
justifyContent: 'center',
}}
>
<img src={logoSrc} height="100" />
</div>
)
)
}
```
```jsx filename="app/opengraph-image.js" switcher ```jsx filename="app/opengraph-image.js" switcher
import { ImageResponse } from 'next/og' import { ImageResponse } from 'next/og'
import { readFile } from 'node:fs/promises'
export const runtime = 'edge' export const runtime = 'edge'
export async function GET() { export default async function Image() {
const logoSrc = await fetch(new URL('./logo.png', import.meta.url)).then( const logoSrc = await fetch(new URL('./logo.png', import.meta.url)).then(
(res) => res.arrayBuffer() (res) => res.arrayBuffer()
) )
...@@ -428,12 +457,37 @@ export async function GET() { ...@@ -428,12 +457,37 @@ export async function GET() {
This example uses the Node.js runtime to fetch a local image on the file system and passes it as an `ArrayBuffer` to the `src` attribute of an `<img>` element. The local asset should be placed relative to the root of your project, rather than the location of the example source file. This example uses the Node.js runtime to fetch a local image on the file system and passes it as an `ArrayBuffer` to the `src` attribute of an `<img>` element. The local asset should be placed relative to the root of your project, rather than the location of the example source file.
```tsx filename="app/opengraph-image.tsx" switcher
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>
)
)
}
```
```jsx filename="app/opengraph-image.js" switcher ```jsx filename="app/opengraph-image.js" switcher
import { ImageResponse } from 'next/og' import { ImageResponse } from 'next/og'
import { join } from 'node:path' import { join } from 'node:path'
import { readFile } from 'node:fs/promises' import { readFile } from 'node:fs/promises'
export async function GET() { export default async function Image() {
const logoData = await readFile(join(process.cwd(), 'logo.png')) const logoData = await readFile(join(process.cwd(), 'logo.png'))
const logoSrc = Uint8Array.from(logoData).buffer const logoSrc = Uint8Array.from(logoData).buffer
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment