Skip to content

Commit e6718d6

Browse files
docs: fix TS error in Node.js runtime local assets example (#82672)
## What? Updated the documentation for `opengraph-image` / `twitter-image` to fix the **Node.js runtime with local assets** example. ## Why? The existing snippet did not type-check cleanly with Next.js 15+ and could confuse developers. This update improves developer experience by ensuring the snippet is immediately usable and accurate. ## How? - Reproduced the example in a fresh Next.js 15+ project. - Updated the docs example to: - Use `readFile` + Base64 encoding instead of invalid file URLs. - Clarify that assets should be placed relative to the project root. - Added an entry to **Version History** noting the update in v15.x. ## Checklist - [x] Ran `pnpm prettier-fix` - [x] Verified formatting & linting (`pnpm build && pnpm lint`) - [x] Documentation guidelines followed - [x] Example verified in a real Next.js 15 project Fixes #75583 & #75625 (if considered same class of issue) --------- Co-authored-by: Joseph <[email protected]> Co-authored-by: Joseph <[email protected]>
1 parent 91da003 commit e6718d6

File tree

1 file changed

+54
-1
lines changed

1 file changed

+54
-1
lines changed

docs/01-app/03-api-reference/03-file-conventions/01-metadata/opengraph-image.mdx

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,59 @@ export default async function Image({ params }) {
402402

403403
#### Using Node.js runtime with local assets
404404

405-
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.
405+
These examples use the Node.js runtime to fetch a local image from the file system and pass it to the `<img>` `src` attribute, either as a base64 string or an `ArrayBuffer`. Place the local asset relative to the project root, not the example source file.
406+
407+
```tsx filename="app/opengraph-image.tsx" switcher
408+
import { ImageResponse } from 'next/og'
409+
import { join } from 'node:path'
410+
import { readFile } from 'node:fs/promises'
411+
412+
export default async function Image() {
413+
const logoData = await readFile(join(process.cwd(), 'logo.png'), 'base64')
414+
const logoSrc = `data:image/png;base64,${logoData}`
415+
416+
return new ImageResponse(
417+
(
418+
<div
419+
style={{
420+
display: 'flex',
421+
alignItems: 'center',
422+
justifyContent: 'center',
423+
}}
424+
>
425+
<img src={logoSrc} height="100" />
426+
</div>
427+
)
428+
)
429+
}
430+
```
431+
432+
```jsx filename="app/opengraph-image.js" switcher
433+
import { ImageResponse } from 'next/og'
434+
import { join } from 'node:path'
435+
import { readFile } from 'node:fs/promises'
436+
437+
export default async function Image() {
438+
const logoData = await readFile(join(process.cwd(), 'logo.png'), 'base64')
439+
const logoSrc = `data:image/png;base64,${logoData}`
440+
441+
return new ImageResponse(
442+
(
443+
<div
444+
style={{
445+
display: 'flex',
446+
alignItems: 'center',
447+
justifyContent: 'center',
448+
}}
449+
>
450+
<img src={logoSrc} height="100" />
451+
</div>
452+
)
453+
)
454+
}
455+
```
456+
457+
Passing an `ArrayBuffer` to the `src` attribute of an `<img>` element is not part of the HTML spec. The rendering engine used by `next/og` supports it, but because TypeScript definitions follow the spec, you need a `@ts-expect-error` directive or similar to use this [feature](https://github.com/vercel/satori/issues/606#issuecomment-2144000453).
406458

407459
```tsx filename="app/opengraph-image.tsx" switcher
408460
import { ImageResponse } from 'next/og'
@@ -422,6 +474,7 @@ export default async function Image() {
422474
justifyContent: 'center',
423475
}}
424476
>
477+
{/* @ts-expect-error Satori accepts ArrayBuffer/typed arrays for <img src> at runtime */}
425478
<img src={logoSrc} height="100" />
426479
</div>
427480
)

0 commit comments

Comments
 (0)