Speed up your MDX builds, shrink your bundles, cache compilations, lazy load heavy components, and ship faster docs sites.
Your MDX site started fast. Ten pages, quick builds, snappy loads. Then it grew to hundreds of pages, and now builds take forever, bundles balloon, and users wait. Let's fix that.
We'll cover every layer — compilation, bundling, runtime rendering, and caching.
MDX Compilation Optimization
Understanding the Compilation Pipeline
Every MDX file goes through this chain:
Each step takes time. You can't optimize what you can't measure, so let's start there.
The biggest easy win: stop loading plugins you don't need for every file.
If only 5% of your pages have math, why is every file paying the cost of remarkMath?
Example
// ❌ SLOW: Loading unnecessary plugins for every file
export const slowConfig = {
remarkPlugins: [
remarkGfm,
remarkMath, // Only 5% of pages use math
remarkMermaid, // Only 3% of pages use diagrams
remarkEmbedder, // Only 10% of pages embed content
remarkReadingTime,
remarkToc,
],
rehypePlugins: [
rehypeSlug,
rehypeAutolinkHeadings,
rehypeKatex, // Only needed if remarkMath found math
rehypeMermaid, // Only needed if remarkMermaid found diagrams
rehypeHighlight,
rehypeImageSize,
],
};
// ✅ FAST: Conditional plugin loading based on content
export function getOptimizedConfig(source, frontmatter) {
const remarkPlugins = [
remarkGfm,
remarkReadingTime,
];
const rehypePlugins = [
rehypeSlug,
rehypeAutolinkHeadings,
rehypeHighlight,
];
// Only add math plugins if content contains math
if (source.includes('$') || source.includes('\\[')) {
remarkPlugins.push(remarkMath);
rehypePlugins.push(rehypeKatex);
}
// Only add mermaid if content has mermaid blocks
if (source.includes('```mermaid')) {
remarkPlugins.push(remarkMermaid);
}
// Only add TOC plugin if frontmatter requests it
if (frontmatter?.toc !== false) {
remarkPlugins.push([remarkToc, { maxDepth: 3 }]);
}
return { remarkPlugins, rehypePlugins };
}
The difference is real:
Approach
500 files build time
Per-file average
All plugins always
45s
90ms
Conditional plugins
28s
56ms
Improvement
38% faster
38% faster
Caching Strategies
File-Based Compilation Cache
Why recompile a file that hasn't changed? Hash the source + config, and skip compilation on cache hits.
// Only recompile changed files in development
import { createHash } from 'crypto';
const hashCache = new Map();
function hasFileChanged(filePath, content) {
const hash = createHash('md5').update(content).digest('hex');
const previousHash = hashCache.get(filePath);
if (hash === previousHash) return false;
hashCache.set(filePath, hash);
return true;
}
Performance Checklist
Example
### Before Deploying, Verify:
- [ ] **Bundle size** — Is total JS under 200KB gzipped?
- [ ] **Largest page** — Does any page exceed 500KB total?
- [ ] **Code splitting** — Are heavy components lazy-loaded?
- [ ] **Images** — Do all images have width/height and lazy loading?
- [ ] **Build time** — Is full build under 5 minutes?
- [ ] **Incremental** — Do single-file changes rebuild in < 5 seconds?
- [ ] **Cache** — Is compilation output cached between builds?
- [ ] **Plugins** — Are plugins conditionally loaded based on content?
- [ ] **Fonts** — Are code fonts subsetted and preloaded?
- [ ] **Static** — Are all possible pages statically generated?
Summary Metrics
Optimization
Impact
Effort
Compilation caching
60-80% faster rebuilds
Medium
Conditional plugins
30-40% faster per file
Low
Lazy loading components
50-150KB less initial JS
Low
Static generation
0ms response time
Medium
Image optimization
50-70% smaller images
Low
Parallel compilation
3-6x faster full builds
Low
Bundle analysis + tree-shaking
20-40% smaller bundles
Medium
Golden Rule: Measure first, optimize second. Use Lighthouse, WebPageTest, and your build logs to find the actual bottlenecks. Nine times out of ten, the biggest win is switching from runtime compilation to static generation — it removes the largest latency source entirely.
Start with static generation and conditional plugins (high impact, low effort). Then layer in caching, code splitting, and lazy loading as your site grows. A well-tuned MDX site serves thousands of pages with sub-100ms loads and builds in minutes, not hours.
Exam Focus
Revise definitions, diagrams, examples, and short-answer points for Performance Optimization for MDX.
Interview Use
Prepare one clear explanation, one practical example, and one common mistake for this MDX Tutorial topic.