MDX is a game-changer for content. By letting you seamlessly blend JSX components with Markdown, it transforms static documents into rich, interactive experiences. But as its adoption grows, a critical question emerges for developers: What's the best way to compile it?
Traditionally, MDX compilation happens locally during your site's build process. It's a robust method for static sites. But what if your content isn't static? What if it comes from a database, a user input form, or a headless CMS after your site has been deployed?
This is where the paradigm shifts from local compilation to "Compilation as a Service." In this post, we'll compare the traditional local approach with an on-demand API like mdx.do, focusing on which is right for your dynamic content workflows.
Local compilation is the method most developers encounter first. Using tools like next-mdx-remote or Webpack/Vite loaders, you process your .mdx files when you build your application.
This approach is excellent for:
However, its static nature is also its biggest limitation. Local compilation breaks down when content needs to be dynamic. If your content is generated by users, served from a CMS, or needs to be updated without a new deployment, the build-time model simply doesn't work.
This is where mdx.do introduces a new workflow. Instead of compiling content at build-time, you compile it on-demand with a simple API call.
Think of it as CONTENT AS CODE, delivered dynamically.
Here's how simple the implementation is with the official SDK:
import { mdx } from '@do-sdk/mdx';
// Your dynamic MDX string from any source
const dynamicContent = `
---
title: Hello Dynamic World!
author: 'CMS'
---
# This content was loaded on the fly!
It can even include interactive components like this <LivePreview />.
Imagine the possibilities for user-generated tutorials or live documentation.
`;
// Compile your MDX string into a renderable component
const { component, frontmatter } = await mdx.compile({
content: dynamicContent
});
// `component` can now be rendered in your React app,
// and `frontmatter` contains your metadata.
// { title: 'Hello Dynamic World!', author: 'CMS' }
This on-demand approach is tailor-made for modern, dynamic applications:
Feature | Local Compilation | mdx.do (On-Demand) |
---|---|---|
Primary Use Case | Static content (blogs, docs) | Dynamic content (UGC, CMS) |
Workflow | Compile at build-time | Compile via API call at runtime |
Setup Complexity | Requires build tool configuration | Simple SDK/API call, no build config |
Content Source | Local project files | Any source (DB, API, user input) |
Flexibility | Low (Content is fixed after build) | High (Content can change anytime) |
App Dependencies | MDX compilers, loaders, plugins | A lightweight SDK or fetch |
The choice between local and on-demand MDX compilation isn't about which is "better"—it's about which is right for your specific use case.
Choose local compilation if you're building a classic static site where content and code are deployed together. The performance is unbeatable, and the ecosystem is mature.
Choose mdx.do if you're building a next-generation application where content is fluid and dynamic. If your content lives in a database, comes from your users, or needs to update in real-time, offloading compilation to a dedicated service simplifies your architecture and unlocks powerful new features.
Ready to move beyond the limitations of the build step? Transform your content workflows with mdx.do and start compiling MDX on demand.
Q: What is MDX?
A: MDX is an authorable format that lets you seamlessly write JSX in your Markdown documents. It's a powerful way to embed interactive components directly within your content, transforming static documents into rich applications.
Q: How does mdx.do work?
A: mdx.do provides a simple API endpoint. You send an MDX string to the service, and it returns a compiled, ready-to-render React component, along with any frontmatter metadata. This offloads the compilation process from your application's build environment.
Q: Why use mdx.do instead of a local library?
A: Using mdx.do is ideal for dynamic content scenarios where you can't rely on a static build step. It's perfect for user-generated content, CMS-driven pages, or any situation where you need to compile MDX on-demand without complicating your stack.
Q: What is frontmatter?
A: Frontmatter is metadata (like title, author, or tags) written in languages like YAML at the beginning of a Markdown or MDX file. mdx.do automatically parses this for you and returns it as a structured JSON object alongside your component.