Skip to main content

2026-11-30 · 4 min read

One backtick blocked a deploy: compiling every MDX post before publishing, and two failures that never throw

Next.jsDebugging

Translated from the Chinese original: read it in Chinese

The blog on this site is MDX: each post is an .mdx file under content/blog/, compiled by next-mdx-remote on the post page. On the evening of 2026-11-30, after I published a post about a Kaohsiung traffic accident map, the deploy failed. The live site did not go down (deploys alternate between two slots, so a broken build never gets swapped in), but it stayed on the old version until the next fix was pushed.

Which line broke

The post wanted to show a React pattern, and the original text was:

Change it to a single string (`{`${...}`}`) and it works.

The intent was one piece of inline code. But Markdown inline code ends at a backtick run of the same length, so it was actually cut into three pieces:

`{`        <- code
${...}     <- plain text
`}`        <- code

The middle piece is not in code, and braces in MDX are JavaScript expressions. ... is not a valid expression, so the compiler, acorn, failed to parse it:

Error: Could not parse expression with acorn

The fix is to wrap it in two backticks with a space on each side, so the single backticks inside do not end it early:

Change it to a single string (`` {`${...}`} ``) and it works.

Why publishing did not catch it

The publish flow runs ESLint, the TypeScript type check, and the unit tests in src/lib/content. None of those compile MDX: an .mdx file is compiled for the first time only when next build generates the post pages, which is after the push, once the deploy has started.

So I added a unit test that compiles every post with the same settings as the post page. To make sure they really are the same, I moved the settings that used to live inside the page file into src/lib/content/mdx-options.ts. The post page and the test both import from there, so when a plugin is added to the page later, the test follows automatically.

const { content: element } = await compileMDX({ source: content, options });
renderToStaticMarkup(element);

The test lives under src/lib/content/, which the publish flow already runs. Compiling all 54 posts takes under a second.

Gotcha 1: the error message has no line number

The first time I made it fail, the message was just this:

[next-mdx-remote] error compiling MDX:
Could not parse expression with acorn

No line number and no code snippet. Reading the source of next-mdx-remote 6.0.0: it re-wraps the error thrown by the compiler and takes the line number from error.position. But the vfile-message 4 used by this MDX version puts the fields on place, line and column, with no position, so the line number is lost in the wrapping.

My workaround in the test: on failure, compile again with the same copy of @mdx-js/mdx that sits under next-mdx-remote, only to get line and column. Under pnpm, @mdx-js/mdx is an indirect dependency and cannot be imported directly from the project, so it has to be resolved from the location of next-mdx-remote:

const mdxPath = createRequire(requireHere.resolve("next-mdx-remote/package.json")).resolve("@mdx-js/mdx");

One more detail: the compiler counts lines from after the frontmatter, so the frontmatter's line count has to be added back to match the editor. The final message looks like this, and clicking it jumps to that line:

content/blog/kaohsiung-traffic-accidents-bad-coordinates.mdx:49:182 Could not parse expression with acorn

Gotcha 2: valid braces do not throw, the text just vanishes

If the braces happen to contain valid JavaScript, for example:

Write each cell as {h}:00 onward

compilation succeeds and the build succeeds, but the page shows only "Write each cell as :00 onward". The reason is that next-mdx-remote 6 has blockJS on by default: for safety, it deletes every JS expression in a post, with no error and no warning. A line-initial import or export is treated as module syntax and deleted the same way.

That is worse than a failed build: nothing turns red, and a reader has to notice the missing words. So the test also mounts a small plugin, ordered after the post page's plugins and before next-mdx-remote deletes anything, that records where these nodes are and fails if there are any. Right now none of the 54 posts and 12 drafts has this problem.

Gotcha 3: a missing component only breaks at render time

If a post uses a capitalized tag like Foo with no matching component, compilation succeeds and the error is thrown only at render:

Expected component `Foo` to be defined: you likely forgot to import, pass, or provide it.

The real build also fails only while generating pages, so after compiling, the test actually renders once with renderToStaticMarkup.

Verification

  • I put the old version of the post from the broken commit back into content/blog/: the test failed, reporting line 49 column 182, exactly the ... inside the braces
  • With it removed, all 54 posts pass; when the drafts folder exists, it is scanned too
  • The test file also has four small cases: single backticks wrapping a backtick fail, the double-backtick fix passes, a valid expression in braces is caught, and a missing component reports a line number. They make sure the check itself really checks

Type to search projects and posts too