Skip to content

fix: keep server-only modules out of remote SSR containers - #37

Open
dmchoi77 wants to merge 1 commit into
module-federation:mainfrom
dmchoi77:fix/container-server-only-imports
Open

dmchoi77 wants to merge 1 commit into
module-federation:mainfrom
dmchoi77:fix/container-server-only-imports

Conversation

@dmchoi77

@dmchoi77 dmchoi77 commented Dec 5, 2026

Copy link
Copy Markdown
Contributor

Problem

A remote's SSR container (remoteEntry.ssr.js for Vite, remoteEntry.ssr.cjs for Rsbuild, plus their chunks) is written to dist/client, so it is published with the browser assets. It is compiled as a server environment, though, so TanStack Start's import protection for the browser does not apply to it. A *.server.* module, or one that imports @tanstack/<framework>-start/server or /server-only, could end up in the public container. With it went whatever server-only code it held, such as secrets or internal endpoints.

Changes

  • createServerOnlyModuleCheck (server-container.ts) checks each module that reaches the container after Start's compiler has run. It flags an app module that:

    • is a *.server.* file, or
    • has a value import or re-export (static or dynamic) of @tanstack/{react,solid,vue}-start/server or /server-only.

    It ignores:

    • node_modules
    • import type
    • mentions in comments and strings
  • On a match, the build fails. Rsbuild dev only warns (api.logger.warn).

  • The Vite and Rsbuild adapters run the check in their existing server container transforms.

  • Imports that only createServerFn handlers use are gone by the time the check runs, so they still pass.

  • README: the "SSR federation" bullet no longer suggests the container holds everything. A new paragraph says what stays out and how to call server-only code.

  • The example remotes now read their greeting from greeting.server.ts, which only a server function handler uses.

Tests

  • Unit (test/release-contracts.test.mjs) covers:
    • *.server.* files and server/server-only imports throw
    • handler-only and node_modules modules pass
    • import type, comments and strings pass
    • dev mode warns instead of throwing
  • Real builds (Vite and Rsbuild): a copy of each example remote that exposes greeting.server.ts directly fails to build.

Verification

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

Notes

  • Remotes that may stop building: remotes that import a *.server.* module or /server-only inside a createServerOnlyFn, createIsomorphicFn().server() or createMiddleware().server() body. Those bodies are not removed from the container yet. The follow-up PR compiles them away. Ideally both ship in the same release.
  • Out of scope: a *.server.* module that is itself listed in exposes.

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.
@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@37

commit: 10c874a

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