Skip to content

fix!: compile remote SSR containers for the browser before the server - #38

Open
dmchoi77 wants to merge 2 commits into
module-federation:mainfrom
dmchoi77:fix/container-server-only-transforms
Open

dmchoi77 wants to merge 2 commits into
module-federation:mainfrom
dmchoi77:fix/container-server-only-transforms

Conversation

@dmchoi77

@dmchoi77 dmchoi77 commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Depends on #37. This branch includes that PR's commit, so review only the last commit (f4e8bea).

Problem

A remote's public SSR container is compiled as a server environment, so it kept code that TanStack Start removes from the browser build:

  • createServerOnlyFn bodies
  • createIsomorphicFn().server() implementations
  • createMiddleware().server() handlers

All of these were published with the browser assets in dist/client.

Before this change, the example remotes' probes were confirmed to leak:

  • Vite: dist/client/assets/server-functions-*.js
  • Rsbuild: dist/client/ssr/*.js

Changes

  • createContainerClientCompile (server-container.ts) runs TanStack Start's own StartCompiler over the container's modules. It runs as a client compile (env: "client") and only for the ServerOnlyFn, IsomorphicFn and Middleware kinds. It runs before Start's server compile, which turns server function handlers into RPC callers.
    • Results: createServerOnlyFn functions throw with Start's own message, isomorphic functions keep .client(), and middleware drops .server() and .validator(), all exactly as in the browser build.
    • Left alone: server functions, which the server compile handles as before, and TanStack's own packages (node_modules/@tanstack/), whose runtime the container needs on the server.
    • Cache: the compiler's module cache is dropped before every build, including watch rebuilds.
    • When Start's compiler can't be loaded: it is loaded from the app's @tanstack/start-plugin-core. If that fails and a module uses one of these factories, the build fails instead of publishing their server code.
  • Vite: a pre plugin, placed before Start's compiler plugins, applied to the mf_server environment in build.
  • Rsbuild: a pre transform on mf-server, registered after Start's transform so it runs first. It resolves through ctx.resolve, reads through the compiler's inputFileSystem, and adds dependencies for watch.
  • README: documents the browser compile and what it means while a host renders the remote on its server.

BREAKING CHANGE

While a host renders a remote on its server, the remote's exposed code now gets the browser versions of TanStack Start's server-only factories:

  • createServerOnlyFn functions throw.
  • createIsomorphicFn functions run their .client() implementation.
  • createMiddleware runs without .server() and .validator().

Exposed code that needs server behavior during the host's render must call a server function.

Tests

  • Example remotes expose probes: a server-only function, an isomorphic function and a middleware on getRemoteGreeting. The example build asserts that the probe markers appear in dist/server and not in dist/client. This test failed before the fix.
  • Unit (test/release-contracts.test.mjs) covers:
    • an aliased import through a re-export
    • a split isomorphic chain
    • middleware
    • server function handlers left intact
    • non-TanStack node_modules compiled, @tanstack/ skipped
    • a missing Start install fails the build
    • a factory name that appears only in a comment passes
  • Watch rebuilds were checked by hand for both bundlers. Without the cache reset, an edited probe leaked again.

Verification

  • pnpm build
  • pnpm test:core (98/98)
  • pnpm test:e2e (23/23)
  • pnpm typecheck
  • pnpm lint
  • pnpm format:check

Notes

  • Start internals: this relies on @tanstack/start-plugin-core/dist/esm/start-compiler/{compiler,config}.js, which Start does not export. If they move, the build fails rather than leaking.
  • .validator(): middleware loses .validator() too, because Start's client handler strips it. This PR accepts that behavior rather than changing it.
  • Upstream Rsbuild issue: Start's Rsbuild compiler fails with PackagePathNotExported when a module it compiles also has a top-level createMiddleware() call. This happens in Start's own ssr environment too. The Rsbuild example therefore keeps its middleware in probe-middleware.ts.

A remote's public SSR container is compiled as a server environment, so
TanStack Start's import protection does not apply to it. Fail the build
(warn in Rsbuild dev) when a *.server.* app module, or one importing
@tanstack/<framework>-start/server or /server-only, reaches the container
after Start's compiler has run. Imports only server function handlers use
are removed by then and still pass.
A remote's public SSR container is compiled as a server environment, so it
kept createServerOnlyFn bodies, createIsomorphicFn().server() and
createMiddleware().server(). Run TanStack Start's own StartCompiler, as a
client compile without server functions, over the container's modules
(except TanStack's own packages) before Start's server compile, and drop its
cache on every (watch) build. Fail the build when Start's compiler cannot be
loaded and a module uses those factories.

The example remotes expose probes, and the example build asserts that only
their own server output contains them.

BREAKING CHANGE: While a host renders a remote on its server, the remote's
exposed code now gets the browser versions of TanStack Start's server-only
factories: createServerOnlyFn functions throw, createIsomorphicFn functions
run their .client() implementation, and createMiddleware runs without
.server() and .validator(). Exposed code that needs server behavior during
the host's render must call a server function.
@pkg-pr-new

pkg-pr-new Bot commented Dec 5, 2026

Copy link
Copy Markdown

Open in StackBlitz

npm i https://pkg.pr.new/module-federation/tanstack/@module-federation/tanstack@38

commit: f4e8bea

This branch has not been deployed

No deployments
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