fix: Block MDX compilation at Vercel runtime to protect release registry#15953
fix: Block MDX compilation at Vercel runtime to protect release registry#15953
Conversation
This prevents MDX compilation from running at Vercel runtime, which was causing two issues: 1. Hammering release-registry.services.sentry.io with requests on every page view that triggered runtime rendering (404s, edge cases) 2. Failing with 'read-only file system' errors from esbuild trying to write to /var/task/public/mdx-images All docs pages are statically generated during CI builds, so MDX compilation should never happen at runtime. This change fails fast with a clear error message instead of attempting compilation that would fail anyway. Fixes DOCS-915, DOCS-9H5, DOCS-9N0, DOCS-9HB
|
The latest updates on your projects. Learn more about Vercel for GitHub.
|
Block getDocsFrontMatter() and getDevDocsFrontMatter() from running at Vercel runtime. These functions scan the filesystem for MDX files, which fails on Lambda's read-only filesystem and generates unnecessary spans. The doc tree is pre-computed during CI builds (doctree.json), so these functions should never be called at runtime. This complements the existing guard in getFileBySlug(). Fixes DOCS-83Q, DOCS-9A5, DOCS-9RE Co-authored-by: factory-droid[bot] <138933559+factory-droid[bot]@users.noreply.github.com>
| return Promise.reject( | ||
| new Error( | ||
| `[MDX Runtime Error] Attempted to scan docs frontmatter at Vercel runtime. ` + | ||
| `This should not happen - the doc tree should be pre-computed during CI. ` + | ||
| `If you're seeing this error, the requested path may not exist or was not included in generateStaticParams().` | ||
| ) |
There was a problem hiding this comment.
Bug: Runtime guards added in src/mdx.ts are missing from duplicate functions in src/frontmatter.ts, causing runtime crashes in endpoints that import from the unguarded file.
Severity: CRITICAL
🔍 Detailed Analysis
The pull request introduces runtime guards in src/mdx.ts to prevent MDX compilation on Vercel, but fails to add these same guards to duplicate functions (getDocsFrontMatter, getDevDocsFrontMatter) in src/frontmatter.ts. Two runtime endpoints, app/sitemap.ts and app/api/source-map/route.ts, import these functions from the unguarded src/frontmatter.ts file. As a result, when these endpoints are executed on Vercel, they will still attempt to scan the read-only filesystem, leading to unhandled ENOENT errors and causing the endpoints to crash. This negates the primary goal of the pull request.
💡 Suggested Fix
Apply the same isVercelRuntime guards from src/mdx.ts to the corresponding functions in src/frontmatter.ts. Alternatively, consolidate the duplicate getDocsFrontMatter and getDevDocsFrontMatter functions into a single, guarded implementation to avoid code duplication and ensure consistent behavior.
🤖 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/mdx.ts#L184-L189
Potential issue: The pull request introduces runtime guards in `src/mdx.ts` to prevent
MDX compilation on Vercel, but fails to add these same guards to duplicate functions
(`getDocsFrontMatter`, `getDevDocsFrontMatter`) in `src/frontmatter.ts`. Two runtime
endpoints, `app/sitemap.ts` and `app/api/source-map/route.ts`, import these functions
from the unguarded `src/frontmatter.ts` file. As a result, when these endpoints are
executed on Vercel, they will still attempt to scan the read-only filesystem, leading to
unhandled `ENOENT` errors and causing the endpoints to crash. This negates the primary
goal of the pull request.
Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID: 8440713
| if (isVercelRuntime) { | ||
| return Promise.reject( | ||
| new Error( | ||
| `[MDX Runtime Error] Attempted to scan develop-docs frontmatter at Vercel runtime. ` + | ||
| `This should not happen - the doc tree should be pre-computed during CI. ` + | ||
| `If you're seeing this error, the requested path may not exist or was not included in generateStaticParams().` |
There was a problem hiding this comment.
Bug: The app/api/source-map/route.ts endpoint lacks error handling for the new runtime guards, causing an unhandled promise rejection that will crash the API on Vercel.
Severity: CRITICAL
🔍 Detailed Analysis
The new runtime guard added to getDocsFrontMatter and getDevDocsFrontMatter intentionally rejects a promise when executed on Vercel. However, the app/api/source-map/route.ts API route, which calls these functions, does not wrap the call in a try/catch block. Because Next.js API routes execute at runtime, an unhandled promise rejection will occur when this endpoint is accessed on Vercel, causing the API to crash and return a 500 error. While other parts of the code were updated to handle this new error condition, this specific API route was overlooked.
💡 Suggested Fix
Wrap the call to getDevDocsFrontMatter() and getDocsFrontMatter() within the GET handler in app/api/source-map/route.ts with a try/catch block. The catch block should handle the rejected promise gracefully, preventing the API route from crashing, similar to the error handling implemented in src/components/include.tsx.
🤖 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/mdx.ts#L294-L299
Potential issue: The new runtime guard added to `getDocsFrontMatter` and
`getDevDocsFrontMatter` intentionally rejects a promise when executed on Vercel.
However, the `app/api/source-map/route.ts` API route, which calls these functions, does
not wrap the call in a `try/catch` block. Because Next.js API routes execute at runtime,
an unhandled promise rejection will occur when this endpoint is accessed on Vercel,
causing the API to crash and return a 500 error. While other parts of the code were
updated to handle this new error condition, this specific API route was overlooked.
Did we get this right? 👍 / 👎 to inform future reviews.
Reference ID: 8440713
Summary
This PR blocks MDX compilation from running at Vercel runtime, which was causing:
release-registry.services.sentry.io, causing traffic spikes that crashed the registryRoot Cause Analysis
When users hit URLs that don't exist (404s) or certain edge cases in Next.js routing, the page component tries to render before falling back to not-found. This triggers:
getFileBySlugWithCache()is called for a non-existent pathCIenv var is set)bundleMDX()remarkVariablesplugin fetches from release registry/var/task/public/mdx-images(read-only on Lambda)The Fix
Add a guard at the start of
getFileBySlug()that throws immediately if we're at Vercel runtime:Why This Is Safe
force-static+generateStaticParams)Fixes