fix(islands): hydrate the client components MDX content imports

Merged
KI
14d ago
+119-2

Problem

In islands mode, a "use client" component that an MDX file imports never becomes an island when a content collection loads the file with import("./guide.mdx"). It renders as server HTML that never hydrates. If it passes a function to a package island, the page returns 500:

Button (/.farm/client-references/base-ui+react+button/Button.tsx) cannot get its props in the browser: functions are not serializable

Found on the assistant-ui docs (671 MDX pages, Farm 0.1.75 and 0.1.77). 78 pages import an app client component. All of those were dead in the browser, and 16 returned 500. The same pages work on Next.js. For example, the part-grouping toggle collapses on Next and does nothing on Farm.

Root cause

collectIsolatedClientBoundaries (utils/client-component.ts) decides which client modules become islands. It follows only static imports of .ts/.tsx/.js modules, so it never reaches the MDX a collection loads with import(). A static import Doc from "./doc.mdx" was read with the JavaScript tokenizer, which trips on prose and frontmatter and also picks up the import lines of fenced code examples. No test covered an MDX file importing a client component.

Change

  • The planner follows import() into .md/.mdx modules from any module it visits. Other dynamic imports are unchanged.
  • An MDX file is read as its top-level import/export blocks (an ESM block runs to a blank line). Frontmatter, prose and fenced code are skipped. The result is memoized in the source snapshot, as other parses are.

Everything after that is the existing path. The boundaries go into the same plan, the transform wraps them with createIsland, and the client entry adds one lazy loader per boundary.

Safety and compatibility

  • A visited MDX file contributes its imports only. The JSX checks (parser-sensitive containers, element props, cardinality) don't run on MDX bodies, because they would need the MDX compiler. That's acceptable for async server pages, which already allow element props and dynamic cardinality.
  • An MDX file that imports a client module with an unsupported export shape (export *) now fails farm build with the existing islands error and hint, instead of rendering the component dead.
  • Code that calls components outside a render and detects client components by React's react.client.reference marker (assistant-ui's MDX-to-Markdown route does) needs farming-labs/islx#84 too. Without it, those 33 Markdown mirrors fail on the islands' hooks.
  • page.mdx routes are unchanged: their components come from mdx.components, which the planner already reads.

Verification

  • pnpm exec vitest run src/__tests__/islands-mode.test.tsx: 27 passed. The new test (isolates the client components an MDX module imports, as a content collection loads it) fails without the change: expected [] to deeply equal [counter.tsx, tabs.tsx].
  • pnpm exec vitest run on client-component, package-client-boundary-cache, production-build-plan-reads, route-manager, app-markdown and islands-mode: 210 passed.
  • tsc --noEmit in packages/farm: passes. oxfmt and oxlint on the changed files: clean.
  • Production build of the assistant-ui docs, using core 0.1.77 with this commit (main's core needs a dependency that install doesn't have), Node 24.21.0, macOS:
    • .farm/.output server chunks for the specimens and tool-ui samples now contain createIsland (they didn't before).
    • The 16 routes that returned 500 return 200. A crawl of all 730 routes gives 728 × 200 and 2 × 500, which are an unrelated SVG Image import.
    • Chrome: the part-grouping toggle collapses (76 → 24 characters), the same as Next.

Not verified: a fumadocs-mdx collection, which adds ?collection= to its imports. The planner doesn't resolve query strings.

Documentation and release

docs/.../server-rendering/islands/page.md now has an MDX bullet under "How Farm uses islx". No release changes.


Summary by cubic

Fixes islands mode so that "use client" components an MDX file imports become islands, whether the MDX file is imported statically or loaded with import() by a content collection.

Previously, the islands planner only followed static imports of JavaScript modules, so MDX-loaded client components rendered as server HTML that never hydrated. Pages could also 500 when one of those components passed a function to a package island (found on the assistant-ui docs, 671 MDX pages). The planner now follows import() into .md/.mdx modules and reads an MDX file's top-level import/export blocks, skipping frontmatter, prose, and fenced code examples. The parse is memoized in the source snapshot.

  • MDX files contribute only their imports; the JSX-specific island checks still skip MDX bodies, which is acceptable for async server pages.
  • An MDX file that imports a client module with an unsupported export shape now fails farm build with the existing islands error, instead of rendering the component dead.
  • Known gap: fumadocs-mdx collections that add ?collection= to their imports aren't resolved yet.
  • Sites that detect client components outside the render path (like assistant-ui's MDX-to-Markdown route) need farming-labs/islx#84 for full hydration.

Verified with a new regression test, the islands/planner/route-manager/markdown suites, and a production build of the assistant-ui docs: the 16 erroring routes now return 200 and the part-grouping toggle hydrates correctly.