diff --git a/apps/README.md b/apps/README.md index 01e656f..385327d 100644 --- a/apps/README.md +++ b/apps/README.md @@ -41,7 +41,9 @@ have a **Visitor** page, linked from the navigation bar. `head` links them, so the cards are styled before JavaScript runs. The `rsbuild-remote` card mounts after hydration. Every remote sits behind `RemoteBoundary`, an error boundary with a fallback card, and the loader - ignores failed loads, so an offline remote never fails the route. + ignores failed loads, so an offline remote never fails the route. Like the + Rsbuild hosts, it uses `shareStrategy: "loaded-first"`, so it loads a remote + when a card first needs it instead of every remote at startup. - **Rsbuild host.** `RemoteCardSlot` calls `loadRemote()` in an effect and shows "Remote unavailable" when a remote fails. The host uses `shareStrategy: "loaded-first"`, so it does not fetch every remote's diff --git a/apps/vite-host/vite.config.ts b/apps/vite-host/vite.config.ts index 83efc08..b4bc588 100644 --- a/apps/vite-host/vite.config.ts +++ b/apps/vite-host/vite.config.ts @@ -8,6 +8,8 @@ export default defineConfig({ tanstackStartModuleFederation({ name: "tanstack_vite_host", dts: false, + // Load each remote when it is first used, not every remote at startup. + shareStrategy: "loaded-first", shared: { // The root route renders the provider synchronously, so a Vite host must provide // the package eagerly. "workspace:*" is not a semver range, hence requiredVersion. diff --git a/packages/tanstack/README.md b/packages/tanstack/README.md index d2e734a..c0c13db 100644 --- a/packages/tanstack/README.md +++ b/packages/tanstack/README.md @@ -31,6 +31,7 @@ export default defineConfig({ plugins: [ tanstackStartModuleFederation({ name: "host", + shareStrategy: "loaded-first", remotes: { remote: { type: "module", @@ -139,9 +140,14 @@ Defaults: load its chunks from the remote. TanStack Start otherwise derives `/` from `server.base`. An absolute asset prefix, such as a CDN URL, is kept. -For hosts, set `shareStrategy: "loaded-first"`. With the default -`version-first` strategy, the host fetches every remote's manifest during -startup, and a single offline remote fails the whole page. +Hosts on either bundler should set `shareStrategy: "loaded-first"` (a top-level +option of the Vite wrapper, `federation.shareStrategy` here), so a remote loads +when the host first uses it. With Module Federation's default `version-first` +strategy, a host loads every remote's manifest and entry when it starts, and a +single offline remote breaks it: an Rsbuild host leaves every remote loading, +an Rsbuild host with `server: true` answers every request with a 500, and a +Vite host renders the other remotes but logs a federation error for each shared +package. ### SSR federation @@ -334,6 +340,57 @@ A timeout used to reject with the raw `AbortError`. It now rejects with a too when a server entry cannot be fetched, so check `code` instead of `name === "AbortError"`. +## Deferring remote hydration + +TanStack Start's `` works around remote components. The server renders +the remote into the HTML as before; the browser keeps that markup and loads the +remote's code only when the `when` trigger fires, such as `visible()` for a +remote below the fold: + +```tsx +import { lazyRemote } from "@module-federation/tanstack/runtime"; +import { createFileRoute } from "@tanstack/react-router"; +import { Hydrate } from "@tanstack/react-start"; +import { visible } from "@tanstack/react-start/hydration"; + +const Reviews = lazyRemote(() => import("remote/Reviews")); + +export const Route = createFileRoute("/product")({ + // Link the remote's stylesheets from `head` as in the previous section. + loader: () => (typeof window === "undefined" ? Reviews.preload().catch(() => null) : null), + component: () => ( + + + + + + ), +}); +``` + +- **Share strategy.** The host must use `shareStrategy: "loaded-first"`. With + `version-first`, it loads every remote's manifest and entry at startup, and + only the exposed module's own chunks wait for the trigger. +- **Loader.** The browser does not run the loader on the first page load, but + it does after a client-side navigation, where an unconditional `preload()` + loads the remote before the boundary is visible. Preloading only on the + server, as above, keeps the remote inline in the server response and leaves + the browser to load it when the boundary hydrates. +- **Stylesheets.** Keep linking the remote's stylesheets from `head`: the + server-rendered markup needs them before any JavaScript runs. +- **Prefetching.** A strategy passed as `prefetch`, such as + `visible({ rootMargin: "1200px" })`, loads only the host's code-split chunk + for the boundary, not the remote. To load the remote ahead of hydration, pass + a function that calls `preload()`: + `prefetch={async ({ waitFor }) => { await waitFor(idle()); await Reviews.preload().catch(() => null); }}`. + Hydration waits for the function, and a rejection reaches the nearest error + boundary. `idle()`, `interaction()`, `media()`, and `condition()` call it; + `visible()` ignores a function. +- **Inside a remote.** An exposed module can use `` too. The adapter + compiles the remote's server container with TanStack Start's transform, like + its browser build, so the server and the browser agree on each boundary, and + the boundary's chunk loads from the remote's origin. + ## Remote server functions A remote's exposed modules can call the remote's `createServerFn` @@ -451,6 +508,10 @@ the remote modules, as the examples here do. host's share scope. - A Vite dev server injects CSS from JavaScript, so its manifest lists no stylesheets and `getRemoteStylesheets` returns none for it in development. +- In development, an Rsbuild host cannot render a Vite remote on the server + when the remote's exposed modules import `@tanstack/react-start`, for example + for ``: the host resolves `@tanstack/router-core` subpaths to the + package's main entry. Production builds are not affected. ## Requirements