Skip to content

fix(markdown): keep "use client" on interactive components - #435

Merged
gitfromwildan merged 1 commit into
mainfrom
fix/markdown-use-client
Oct 31, 2026
Merged

gitfromwildan merged 1 commit into
mainfrom
fix/markdown-use-client

Conversation

@gitfromwildan

Copy link
Copy Markdown
Member

Summary

pnpm build printed 20 Rolldown MODULE_LEVEL_DIRECTIVE warnings ("the module level directive "use client" … may not be preserved when bundling"). In @docubook/markdown they pointed at a real bug: the package was bundled into one dist/index.js, so the "use client" directive on 12 interactive modules was dropped from the published build. In @docubook/ui-react the warnings were noise, because the directive already survived. This PR keeps the directive in markdown and silences the warning in both packages. The build is now warning-free.

Why

markdown lost its client boundaries

Rolldown can only keep a module-level directive when the module is emitted as its own chunk. markdown's lib build had a single entry and fileName: () => "index.js", so all 23 modules were concatenated and the directive was stripped from:

Directory Modules with "use client"
src/components AccordionMdx, AccordionsMdx, Callout, CardMdx, ImageMdx, MermaidMdx, TabsMdx, TooltipMdx, TreeMdx
src/utils AccordionGroupContext, CopyButton, ExpandableCode

In a React Server Components framework such as the Next.js App Router, importing createMdxComponents from the published package therefore treated these hook-using components as server components. Flame renders without RSC, so it was not affected.

ui-react was already correct

ui-react builds one lib entry per component in src/base, and the published dist/drawer.js, dist/modal.js and the others already start with "use client". Rolldown warns about every directive it sees, including directives it keeps.

Changes

  • packages/markdown/vite.config.ts
    • output.preserveModules: true with preserveModulesRoot: "src": one output file per source module (dist/index.js, dist/registry/index.js, dist/components/*.js, dist/utils/*.js), so each client module keeps its directive
    • fileName now uses the entry name, because a fixed index.js would make every preserved module collide on one file name
    • checks.moduleLevelDirective: false
  • packages/ui-react/vite.config.ts: checks.moduleLevelDirective: false. No output change.
  • .changeset/markdown-keep-use-client.md: @docubook/markdown patch

The public surface is unchanged. exports["."] still points at dist/index.js, styles.css is untouched, and the .d.ts layout from tsc is the same as before. External dependencies (react, react-dom, mermaid, lucide-react, react-icons) are still not inlined. Every relative import in the emitted JS keeps its .js extension, so Node ESM resolves the tree.

Verification

  • pnpm build --force: 5 of 5 packages build with 0 warnings (was 20). In markdown, all 12 modules listed above start with "use client" in dist/.
  • pnpm lint, pnpm typecheck and pnpm test pass (1,124 tests across 5 packages).
  • Flame equivalence: flame was built with the old and the new markdown dist/, on Bun (bun .docu/node/build.ts) and on Node (FLAME_RUNTIME=node node bin/cli.js build after compile:lib). Each build produced 27 pages. After normalizing asset hashes and CSP nonces, all 30 HTML files are identical between old and new on both runtimes. The client bundle differs only in minified module layout.
  • Browser: pnpm test:browser-smoke passes on both the Bun and Node builds. A Playwright pass over all 29 doc pages found HTTP 200 on every page, every page hydrated, and no console or page errors. All 7 Mermaid diagrams render, matching the old build.
  • Dev server: bun bin/cli.js dev serves the tabs, tooltips and mermaid pages with no errors. The workspace watcher (packageFingerprint in dev-watch.ts) already hashes dist/ recursively, so it picks up the per-module files.
  • node -e 'import("@docubook/markdown")' loads the package and createMdxComponents() returns 30 components.

Not tested: the Deno runtime (not installed in the test environment), and a real Next.js App Router consumer.

Impact on consumers

  • Flame: same rendered HTML, with hydration and interactivity unchanged.
  • RSC frameworks (for example the Next.js App Router) now get correct client boundaries for markdown's interactive components.
  • Deep imports into @docubook/markdown/dist/... were never part of exports, so they remain unsupported.

Out of scope

Flame's client-bundle cache key (hashMdxSources in .docu/node/cache-key.ts) does not include the content or version of the @docubook/* packages it bundles. A project that upgrades @docubook/markdown can therefore keep its previous client bundle until an MDX or CSS input changes. This was already the case before this PR and does not affect it, because the previous bundle still works. It should be fixed in a separate PR.

Vite 8 (Rolldown) printed 20 MODULE_LEVEL_DIRECTIVE warnings on every
build. In markdown they were real: the library was bundled into a
single dist/index.js, so the "use client" directive at the top of 12
modules (Accordion, Tabs, Tooltip, Mermaid, CopyButton, ...) was
dropped. React Server Components frameworks such as the Next.js App
Router then treated those components as server components.

- markdown: emit one file per source module (preserveModules, rooted
  at src), so each client module keeps its directive. dist/index.js
  stays the only public entry; relative imports keep their .js
  extension, so Node ESM resolves the tree as before
- markdown and ui-react: turn off Rolldown's moduleLevelDirective
  check. ui-react already emits each component as its own entry and
  kept the directive; Rolldown warns even when it is preserved

Flame builds on Bun and Node produce the same HTML for all 30 pages
as before (after normalising asset hashes and CSP nonces), and every
docs page hydrates without console errors, including the 7 Mermaid
diagrams. Lint, typecheck, unit tests and the browser smoke pass.

Co-authored-by: Claude Opus 5.5 <[email protected]>
@vercel

vercel Bot commented Oct 31, 2026 •

Copy link
Copy Markdown

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

1 Skipped Deployment
Project Deployment Actions Updated
docu Ignored Ignored Oct 31, 2026 5:06am UTC

@pullfrog pullfrog Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

✅ No new issues found.

Reviewed changes

I reviewed the switch of @docubook/markdown to per-module output and the warning suppression in both library builds (mode: standard). I also rebuilt both packages locally to check the output.

  • markdown per-module output: preserveModules + preserveModulesRoot: "src" emits 22 JS files. All 12 client modules (components/{Accordion,Accordions,Callout,Card,Image,Mermaid,Tabs,Tooltip,Tree}Mdx.js and utils/{AccordionGroupContext,CopyButton,ExpandableCode}.js) now start with "use client". Externals are still external, and the .d.ts layout from tsc matches the JS layout. import("./dist/index.js") works under Node ESM, and createMdxComponents() returns 30 entries.
  • fileName by entry name: required so the preserved modules don't all collide on index.js. exports["."] still resolves to dist/index.js.
  • checks.moduleLevelDirective: false: both builds now finish without warnings. ui-react's output is unchanged: dist/index.js re-exports from the per-entry files, and the client entries keep their directive.
  • Changeset: @docubook/markdown patch, which fits a build-output fix with an unchanged public API.

Assessment: ready to merge. Top priorities: none blocking. The note below is optional.

ℹ️ No guard on the directive now that the warning is silenced

The Rolldown warning was what exposed this bug, and both configs now turn it off. If the markdown build ever goes back to a single bundle, or a new client module ends up merged into another chunk, the directive will be dropped again with no warning. The repo convention is that a bug fix comes with a regression test, but this would only be a small post-build check. It's fine to merge without it.

Technical details
# Guard "use client" survival in markdown dist

## Affected sites
- packages/markdown/vite.config.ts:13 — `checks.moduleLevelDirective: false` removes the only build-time signal

## Required outcome
- A check fails when any `src/**/*.tsx` file that starts with `"use client"` maps to a `dist/**/*.js` file that does not start with it

## Suggested approach (optional)
- A small vitest or node script run after `vite build` that globs `src` for the directive and asserts the mirrored `dist` path keeps it (the layout is 1:1 thanks to `preserveModulesRoot: "src"`)

Pullfrog  | View workflow run | Using claude-opus-5-5 | 𝕏

@gitfromwildan
gitfromwildan merged commit 0871c3d into main Oct 31, 2026
16 checks passed
@gitfromwildan
gitfromwildan deleted the fix/markdown-use-client branch October 31, 2026 05:10
@github-actions github-actions Bot mentioned this pull request Oct 31, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant