mirror of
https://github.com/hanzoai/next.js.git
synced 2026-10-30 14:46:38 +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.
36 lines
785 B
TypeScript
36 lines
785 B
TypeScript
import { readFile } from 'fs/promises'
|
|
|
|
async function getPostFile(slug: string) {
|
|
'use cache'
|
|
return readFile('./posts/' + slug + '.txt', 'utf8')
|
|
}
|
|
|
|
async function PostContent({ slug }: { slug: string }) {
|
|
'use cache'
|
|
const file = await getPostFile(slug)
|
|
return <article>{file.trim()}</article>
|
|
}
|
|
|
|
export default async function Page({
|
|
params,
|
|
}: {
|
|
params: Promise<{ slug: string }>
|
|
}) {
|
|
const { slug } = await params
|
|
return <PostContent slug={slug} />
|
|
}
|
|
|
|
export async function generateMetadata({
|
|
params,
|
|
}: {
|
|
params: Promise<{ slug: string }>
|
|
}) {
|
|
const { slug } = await params
|
|
const file = await getPostFile(slug)
|
|
return { title: file.trim() }
|
|
}
|
|
|
|
export async function generateStaticParams() {
|
|
return [{ slug: 'first-post' }, { slug: 'second-post' }]
|
|
}
|