Skip to content

perf(mdx): Optimize frontmatter scanning and local bundle cache#17706

Closed
a-hariti wants to merge 4 commits intomasterfrom
mdx-build-cache-frontmatter
Closed

perf(mdx): Optimize frontmatter scanning and local bundle cache#17706
a-hariti wants to merge 4 commits intomasterfrom
mdx-build-cache-frontmatter

Conversation

@a-hariti
Copy link
Copy Markdown
Collaborator

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:

  • Route static params, version lookup, and Algolia frontmatter reads through src/frontmatter.ts.
  • Cleanup redundant frontmatter scanner implementation from src/mdx.ts.
  • Enable the existing MDX bundle cache for local production builds while keeping Vercel runtime excluded.
  • Write Brotli-compressed MDX bundle cache entries atomically to avoid concurrent workers reading partially written cache files.

a-hariti added 4 commits May 10, 2026 18:50
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.
@vercel
Copy link
Copy Markdown

vercel Bot commented May 10, 2026

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated (UTC)
develop-docs Ready Ready Preview, Comment May 10, 2026 10:41pm
sentry-docs Ready Ready Preview, Comment May 10, 2026 10:41pm

Request Review

@a-hariti a-hariti marked this pull request as ready for review May 10, 2026 22:32
Comment thread src/frontmatter.ts
Comment on lines 54 to +59
return getDocsFrontMatterCache;
}

/**
* Collect all available versions for a given document path.
*/
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant