mirror of
https://github.com/hanzoai/next.js.git
synced 2026-09-24 17:00:51 +00:00
Extracted from https://github.com/vercel/next.js/pull/95468 and arguably the main fix there. We need to add code to wait for top-level awaits in two cases: - If a metadata module (like `opengraph-image.tsx`) is slow to evaluate, cache warming wouldn't "wait" for it, and then the actual build would fail with `Unexpected cache miss after cache warming phase`. This is fixed with placing `instrumentModuleGetter` around them. - If the evaluation is fast but still async (e.g. `await readFile` on a large font), the build passes but the segment collector reads the module too early, doesn't see `generateStaticParams`, and so the image becomes dynamic instead of prerendered. The fix is `await ensureUserland` before looking at exports. Remaining callsites are wrapped so we don't add more bugs like this, but it's not strictly required now because they are already being awaited elsewhere. I've done this for consistency. New tests fail before the fix.
34 lines
937 B
TypeScript
34 lines
937 B
TypeScript
import { ImageResponse } from 'next/og'
|
|
import { readFile } from 'node:fs/promises'
|
|
import { join } from 'node:path'
|
|
|
|
export const size = { width: 1200, height: 630 }
|
|
export const contentType = 'image/png'
|
|
|
|
// The top-level await makes this an async module that's also evaluated during
|
|
// the page's prerender, via the metadata image route's exports.
|
|
const font = await readFile(join(process.cwd(), 'assets/typewr__.ttf'))
|
|
|
|
export async function generateImage(text: string) {
|
|
return new ImageResponse(
|
|
(
|
|
<div
|
|
style={{
|
|
width: '100%',
|
|
height: '100%',
|
|
display: 'flex',
|
|
alignItems: 'center',
|
|
justifyContent: 'center',
|
|
fontSize: 88,
|
|
fontFamily: 'Typewriter',
|
|
background: '#fff',
|
|
color: '#000',
|
|
}}
|
|
>
|
|
{text}
|
|
</div>
|
|
),
|
|
{ ...size, fonts: [{ name: 'Typewriter', data: font, style: 'normal' }] }
|
|
)
|
|
}
|