You can use the same MDX compiler locally and on the server and still preview something different from what production receives. The compiler is only one step in the pipeline. A formatting pass before it can turn a valid document into an invalid one.
Here is a small example. A nested HTML table compiles. A helper inserts line breaks to make its markup more readable. The transformed document fails with a closing-tag error. Applying the helper twice produces the same output, so an idempotence check passes too.
Download the minimal reproduction. It uses synthetic content and a deliberately broken formatter, not customer documents or a production parser. The recorded behavior uses Node 22.23.2 and @mdx-js/mdx 3.1.0. It compiles the fixture but never evaluates the generated JavaScript.
A table that compiles before we touch it
The entire fixture is three lines:
1<table><tbody>2<tr><td>Status</td><td><div><span>Ready</span><span>Owner</span></div></td></tr>3</tbody></table>There is nothing clever in the content. One cell contains a small wrapper and two spans. The tags are balanced, and the raw source compiles with the pinned MDX version.
Now suppose a preparation helper inserts newlines around container tags. It might be intended to handle an imported table whose compact layout interacts badly with the renderer.
The following function is an intentionally broken simplification. Do not use it as an HTML or MDX parser:
1function splitContainers(value) {2 return value3 .replace(4 /(<\/?(?:div|table|thead|tbody|tfoot|tr)\b[^>]*>)(?=<)/g,5 "$1\n",6 )7 .replace(8 />(?=<\/(?:div|table|thead|tbody|tfoot|tr)\b)/g,9 ">\n",10 );11}Among other boundaries, it splits the div inside the cell. The result looks tidy if you think of it only as HTML:
1<table>2<tbody>3<tr>4<td>Status</td><td><div>5<span>Ready</span><span>Owner</span>6</div>7</td>8</tr>9</tbody>10</table>But that is not the same input to an MDX parser.
The error is introduced after the successful check
Compile the original and transformed strings separately:
1import { compile } from "@mdx-js/mdx";2
3await compile(source); // Compiles in this reproduction4await compile(splitContainers(source)); // Throws in this reproductionThe second call reports a closing-tag failure involving div before the end of a paragraph. Exact coordinates depend on the fixture's whitespace. You can see the full error reason by running the download.
This is not evidence that the original author's tags were unbalanced. The document was valid before the preparation step changed it. Adding a closing tag to the saved source is the wrong response to a mutation introduced later in the read path.
MDX combines Markdown and JSX. Markdown parsing is sensitive to line structure, while the HTML-like fragments follow JSX syntax. A line break can affect whether content is interpreted in a text or flow context. Counting opening and closing tags is not enough to establish that the combined document is valid.
The MDX troubleshooting guide covers related closing-token and container errors. The important diagnostic step here is to inspect the string that actually reaches the compiler, not only the original file.
Why idempotence does not save this formatter
An idempotent preparation function has a useful property: running it again does not keep changing the output. That matters when content passes through more than one layer.
It is not the same as preserving meaning or preserving validity.
In this reproduction:
1const prepared = splitContainers(source);2
3console.log(splitContainers(prepared) === prepared);4// trueThe helper is stable after its first pass. It has stably produced a document that no longer compiles.
The useful checks are separate:
| Question | What it establishes | What it does not establish |
|---|---|---|
| Is preparation idempotent? | Repeated application stops changing the string | The first result is correct |
| Does raw input compile? | The original document is valid under this configuration | A later transformation preserves it |
| Does prepared input compile? | The compiler accepts the actual prepared string | Components work correctly at runtime |
| Does the rendered page preserve the task? | The reader can use the important content | Every untested page or environment is equivalent |
Treating one of those as a substitute for the others is how a useful local check becomes false confidence.
Share the pipeline, not just the dependency name
Compare these two paths:
1Local preview:2 saved source -> compiler -> component registry -> page3
4Hosted reader:5 saved source -> preparation -> compiler -> component registry -> pagePinning the same compiler version does not remove the extra preparation step. The two paths are still compiling different strings.
A better boundary is an explicit content pipeline that both callers use. It should define any necessary source preparation, the compiler version and options, plugins, supported components and relevant output behavior. The hosted reader can fetch the source remotely while local preview reads a folder; that difference does not require two formatting implementations.
Prefer one shared implementation or versioned artifact where practical. Copying the same function into two repositories can reduce a particular mismatch, but it is not proof that future releases stay aligned. You still need an explicit version relationship and a way to detect drift.
There are legitimate environment differences: asset hosts, routing, authentication and backend-backed components may not behave identically in an offline preview. Document those limits rather than labeling every local success “production-equivalent.”
Fix the smallest boundary that is actually wrong
For the toy fixture, the simplest fix is to remove the unnecessary transformation. The source already compiles. Both paths can compile the unchanged string successfully.
That does not mean every real import should skip all normalization. Some imported content needs an explicit conversion. The distinction is whether the transformation has a defined input contract and preserves the constructs you support.
If you must normalize compact table markup, nested cell content is a boundary worth protecting. A structural change around the table container is different from inserting Markdown-significant breaks into the middle of a cell's JSX. A broad tag regular expression does not understand that difference.
For complex conversions, use an appropriate parser and a documented representation instead of gradually expanding a regex until it resembles one. For a narrow compatibility repair, define exactly which shape it handles, preserve the original source, and reject or leave unsupported shapes explicit. Do not market a small compatibility function as a general MDX parser.
Keep a representative failure corpus
A useful corpus includes more than the smallest successful table. Choose the structures your actual documentation uses:
- Nested markup inside both header and data cells.
- A cell that spans multiple source lines.
- A table inside a Markdown list item.
- Fenced examples containing literal table tags.
- Inline code containing angle brackets or backticks.
- Attributes containing characters a simple tag matcher might misread.
- Supported custom components and the runtime configuration they require.
Compare the original source, prepared source and compilation result. Where a transformation is intended to be narrowly structural, verify that cell contents and examples remain unchanged. Then inspect the rendered behavior that matters to a reader.
Do not repair the stored document just to make one reader-specific formatter happy. That can break another consumer that correctly reads the original. Keep preparation at an explicit boundary and make the source of an error visible.
Compilation parity is not a security boundary
MDX can contain JavaScript, not just prose. The compiler package's documentation distinguishes compiling MDX from evaluating or running its generated code. The downloadable example uses compilation only.
Matching preview and hosted behavior does not make arbitrary untrusted MDX safe to execute. Preserve the renderer's trust model, supported-component restrictions and access controls. Do not solve a compatibility problem by removing protections that kept imported content from executing code.
Likewise, a shared parser does not establish that a user may read a private document. Source authorization, asset access and content preparation are separate responsibilities.
What to record when preview and production disagree
Record the exact source version, prepared-input fingerprint, compiler and plugin versions, relevant configuration and the phase that failed. Use sanitized fixtures when sharing a bug report. Avoid putting private document bodies, credentials or internal URLs into logs or public issues.
For the reproduction, the conclusion is small and specific: raw input compiled; a formatting step changed it; the prepared input did not compile. The compiler did not see the same document twice.
If you are moving documentation between platforms, include this distinction in the migration acceptance checks. A successful import and a successful raw-source compilation are useful evidence. Neither proves the hosted reader received the same input.