Skip to content

Comments

fix: Resolve serverless runtime errors for mdx-bundler and esbuild#16109

Closed
jaffrepaul wants to merge 4 commits intomasterfrom
fix/serverless-bundle-runtime-deps
Closed

fix: Resolve serverless runtime errors for mdx-bundler and esbuild#16109
jaffrepaul wants to merge 4 commits intomasterfrom
fix/serverless-bundle-runtime-deps

Conversation

@jaffrepaul
Copy link
Contributor

Summary

Fixes three Sentry production errors caused by serverless function bundle issues introduced by the Vercel bundle size optimization work (PR #15452):

  • DOCS-A0W (~4,379 events): require() of ES Module not supported when loading mdx-bundler/client
  • DOCS-A3H (~1,492 events): @esbuild/linux-x64 could not be found on API documentation pages
  • DOCS-A3F (~105 events): ENOENT: no such file or directory, scandir on /sitemap.xml

Changes

1. Local getMDXComponent implementation (DOCS-A0W)

  • Created src/getMDXComponent.ts - a minimal 50-line implementation that replaces mdx-bundler/client
  • Eliminates CJS/ESM interop issues at runtime
  • Allows full exclusion of mdx-bundler from serverless bundles

2. Build-time API markdown compilation (DOCS-A3H)

  • Modified src/build/resolveOpenAPI.ts to pre-compile markdown descriptions to HTML during build
  • Updated src/components/apiPage/index.tsx to render pre-compiled HTML
  • Removes runtime dependency on bundleMDX/esbuild for API pages

3. Graceful sitemap fallback (DOCS-A3F)

  • Added try/catch in app/sitemap.ts around getDocsRootNode()
  • Returns minimal sitemap (homepage only) when doctree is unavailable
  • Prevents 500 errors; full sitemap still served from CDN cache

Bundle Impact

  • ✅ No new runtime dependencies added
  • ✅ Full node_modules/mdx-bundler/**/* exclusion now possible
  • ✅ remark/unified plugins only used at build time (already bundled)
  • ✅ No increase in serverless function size

Test Plan

  • Verify /sitemap.xml returns valid XML
  • Verify API docs pages render correctly (e.g., /api/events/)
  • Verify regular docs pages render MDX content
  • Confirm no new Sentry errors after deployment

Fixes DOCS-A0W, DOCS-A3H, DOCS-A3F

Co-Authored-By: Claude noreply@anthropic.com

jaffrepaul and others added 3 commits January 21, 2026 18:21
Fixes DOCS-A0W - "Cannot find module 'mdx-bundler/client'" and
"require() of ES Module not supported" errors in Vercel serverless.

Root cause: mdx-bundler has CJS/ESM compatibility issues where
client/index.js uses require() to load ../dist/client.js, but the
parent package has "type": "module" causing Node.js to reject the
require() call at runtime.

Solution: Inline the getMDXComponent function (~30 lines) which only
depends on React. This eliminates the runtime dependency on
mdx-bundler/client entirely, allowing full exclusion of mdx-bundler
from serverless bundles.

Changes:
- Add src/getMDXComponent.ts with local implementation
- Update 4 files to import from local module
- Simplify next.config.ts exclusion to 'node_modules/mdx-bundler/**/*'

Bundle impact: Better - can now fully exclude mdx-bundler instead of
partial 'dist/!(client*)' pattern which may not work reliably.

Co-Authored-By: Claude <noreply@anthropic.com>
Fixes DOCS-A3H - "The package @esbuild/linux-x64 could not be found"
errors on API documentation pages.

Root cause: ApiPage used bundleMDX() at runtime to compile markdown
descriptions, but bundleMDX requires esbuild which is excluded from
serverless bundles to save space.

Solution: Pre-compile markdown to HTML at build time in resolveOpenAPI.ts
using remark/rehype (already available). Store as `descriptionHtml` and
render with dangerouslySetInnerHTML in ApiPage.

Changes:
- Add compileMarkdownToHtml() in resolveOpenAPI.ts using unified/remark
- Add descriptionHtml field to API type
- Compile markdown during API data processing
- Update ApiPage to render pre-compiled HTML
- Remove bundleMDX, getMDXComponent, mdxComponents imports from ApiPage

Bundle impact: Better - removes runtime dependency on esbuild entirely
for API pages. remark/unified only used at build time.

Co-Authored-By: Claude <noreply@anthropic.com>
Fixes DOCS-A3F - "ENOENT: no such file or directory, scandir" errors
on /sitemap.xml endpoint.

Root cause: When serverless function doesn't have doctree.json bundled,
getDocsRootNode() throws an error causing 500 response on /sitemap.xml.

Solution: Wrap sitemap generation in try/catch and return minimal
sitemap (homepage only) when doctree isn't available. The full static
sitemap is served from CDN cache for most requests anyway.

Changes:
- Add try/catch around getDocsRootNode() in sitemap()
- Return [{url: baseUrl}] as fallback instead of crashing
- Add JSDoc explaining the fix

Bundle impact: None - no new dependencies, no bundle size change.

Co-Authored-By: Claude <noreply@anthropic.com>
@vercel
Copy link

vercel bot commented Jan 21, 2026

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

Project Deployment Review Updated (UTC)
develop-docs Ready Ready Preview, Comment Jan 22, 2026 0:05am
sentry-docs Ready Ready Preview, Comment Jan 22, 2026 0:05am

Request Review

Copy link

@cursor cursor bot left a comment

Choose a reason for hiding this comment

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

Cursor Bugbot has reviewed your changes and found 1 potential issue.

Bugbot Autofix is OFF. To automatically fix reported issues with Cloud Agents, enable Autofix in the Cursor dashboard.

)}
{param.description && parseMarkdown(param.description)}
{/* Parameter descriptions are simple text, render directly */}
{param.description}
Copy link

Choose a reason for hiding this comment

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

Parameter descriptions render as plain text, not markdown

Medium Severity

API parameter descriptions are now rendered as plain text instead of being processed as markdown. The main API descriptionMarkdown field is pre-compiled to HTML at build time in resolveOpenAPI.ts, but parameter descriptions (path, query, body) receive no markdown processing. OpenAPI specs support CommonMark in description fields, so any markdown formatting like backticks, links, or emphasis in parameter descriptions will display as raw syntax instead of rendered HTML. This is inconsistent with how apiCategoryPage.tsx processes category descriptions with markdown2Html.

Fix in Cursor Fix in Web

@jaffrepaul jaffrepaul marked this pull request as draft January 22, 2026 15:12
@jaffrepaul jaffrepaul closed this Jan 30, 2026
@jaffrepaul jaffrepaul deleted the fix/serverless-bundle-runtime-deps branch January 30, 2026 22:33
@github-actions github-actions bot locked and limited conversation to collaborators Feb 15, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant