perf(mdx): Optimize frontmatter scanning and local bundle cache#17706
perf(mdx): Optimize frontmatter scanning and local bundle cache#17706
Conversation
Route static params and version lookup through the frontmatter module so Next build avoids the slower duplicated scanner in mdx compilation code. Preserve mixed-case override behavior while keeping generated route sets unchanged.
Use the existing MDX bundle cache outside CI for local production builds while keeping Vercel runtime excluded.
Keep frontmatter scanning owned by src/frontmatter.ts and remove the stale scanner exports from src/mdx.ts. This avoids maintaining two route metadata implementations now that callers no longer need the mdx module for frontmatter.
Write compressed MDX bundle cache entries to a temporary file before renaming them into place. This prevents concurrent build workers from reading partially written Brotli cache files.
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
| return getDocsFrontMatterCache; | ||
| } | ||
|
|
||
| /** | ||
| * Collect all available versions for a given document path. | ||
| */ |
There was a problem hiding this comment.
Bug: The new getDocsFrontMatter() and getDevDocsFrontMatter() functions lack the Vercel runtime guard, causing expensive filesystem scans in production serverless environments, which was previously prevented.
Severity: HIGH
Suggested Fix
Reintroduce the Vercel runtime guard in getDocsFrontMatter() and getDevDocsFrontMatter() in src/frontmatter.ts. The check should prevent execution in a Vercel production environment (e.g., if (process.env.VERCEL && !process.env.CI && process.env.NODE_ENV !== 'development')) by rejecting the promise or throwing an error, similar to the logic in the old src/mdx.ts file.
Prompt for AI Agent
Review the code at the location below. A potential bug has been identified by an AI
agent. Verify if this is a real issue. If it is, propose a fix; if not, explain why it's
not valid.
Location: src/frontmatter.ts#L54-L59
Potential issue: The refactored functions `getDocsFrontMatter()` and
`getDevDocsFrontMatter()` in `src/frontmatter.ts` are missing a runtime guard that was
present in the previous implementation. This guard prevented expensive filesystem
scanning operations on Vercel production deployments. Without it, runtime calls to these
functions, such as from the `Page` component in `app/[[...path]]/page.tsx`, will trigger
a recursive scan of the entire docs folder. This can lead to `EMFILE` errors, severe
performance degradation, or failures on Vercel's read-only filesystem, issues the
original guard was designed to prevent.
Also affects:
src/frontmatter.ts:62~67
Did we get this right? 👍 / 👎 to inform future reviews.
This PR reduces MDX build overhead by moving route metadata reads away from the heavier MDX compilation module and into the dedicated frontmatter scanner.
Changes:
src/frontmatter.ts.src/mdx.ts.