Skip to content

Commit 2033429

Browse files
authored
fix: preserve Router context across SSR bundlers (#6)
1 parent 20e7379 commit 2033429

24 files changed

Lines changed: 592 additions & 52 deletions

‎.changeset/tidy-router-shares.md‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@module-federation/tanstack": patch
3+
---
4+
5+
Share TanStack Router runtimes by default so router-aware remote components can preserve the host Router context across SSR bundlers.

‎apps/rsbuild-host/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"dependencies": {
1414
"@module-federation/runtime": "2.9.2",
1515
"@tanstack/react-router": "1.170.41",
16+
"@tanstack/router-core": "1.171.34",
1617
"@tanstack/react-start": "1.168.60",
1718
"example-host-context": "workspace:*",
1819
"react": "19.3.0",

‎apps/rsbuild-host/rsbuild.config.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ export default defineConfig({
1414
dts: false,
1515
name: "tanstack_rsbuild_host",
1616
// Workspace dependencies declare "workspace:*", which is not a semver range.
17-
shared: { "example-host-context": { requiredVersion: "*", singleton: true } },
17+
shared: {
18+
"example-host-context": { requiredVersion: "*", singleton: true },
19+
},
1820
// Resolve remotes on demand so one offline remote cannot block host startup.
1921
shareStrategy: "loaded-first",
2022
remotes: {

‎apps/rsbuild-host/src/routes/index.tsx‎

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,10 @@ function Home() {
1818
fallback="Loading the Vite remote…"
1919
remote="tanstack_vite_remote/StatusCard"
2020
/>
21+
<RemoteCardSlot
22+
fallback="Loading the Router-aware remote…"
23+
remote="tanstack_vite_remote/RouterCard"
24+
/>
2125
</div>
2226
</main>
2327
);

‎apps/rsbuild-ssr-host/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
"@module-federation/runtime": "2.9.2",
1515
"@module-federation/vite": "1.23.3",
1616
"@tanstack/react-router": "1.170.41",
17+
"@tanstack/router-core": "1.171.34",
1718
"@tanstack/react-start": "1.168.60",
1819
"example-host-context": "workspace:*",
1920
"react": "19.3.0",

‎apps/rsbuild-ssr-host/rsbuild.config.ts‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ export default defineConfig({
1414
dts: false,
1515
name: "tanstack_rsbuild_ssr_host",
1616
// Workspace dependencies declare "workspace:*", which is not a semver range.
17-
shared: { "example-host-context": { requiredVersion: "*", singleton: true } },
17+
shared: {
18+
"example-host-context": { requiredVersion: "*", singleton: true },
19+
},
1820
shareStrategy: "loaded-first",
1921
remotes: {
2022
tanstack_rsbuild_ssr_remote:

‎apps/rsbuild-ssr-host/src/remote.d.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,10 @@ declare module "tanstack_vite_remote/StatusCard" {
1111
const StatusCard: ComponentType;
1212
export default StatusCard;
1313
}
14+
15+
declare module "tanstack_vite_remote/RouterCard" {
16+
import type { ComponentType } from "react";
17+
18+
const RouterCard: ComponentType;
19+
export default RouterCard;
20+
}

‎apps/rsbuild-ssr-host/src/routes/index.tsx‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import { RemoteBoundary } from "../components/RemoteBoundary";
66
// outage renders the remote once it is back.
77
const RsbuildStatusCard = lazyRemote(() => import("tanstack_rsbuild_ssr_remote/StatusCard"));
88
const ViteStatusCard = lazyRemote(() => import("tanstack_vite_remote/StatusCard"));
9+
const ViteRouterCard = lazyRemote(() => import("tanstack_vite_remote/RouterCard"));
910

1011
// Manifests of the remotes this route renders on the server.
1112
const serverRenderedManifests = [
@@ -25,6 +26,7 @@ export const Route = createFileRoute("/")({
2526
).then((lists) => lists.flat()),
2627
import("tanstack_rsbuild_ssr_remote/StatusCard").catch(() => null),
2728
import("tanstack_vite_remote/StatusCard").catch(() => null),
29+
import("tanstack_vite_remote/RouterCard").catch(() => null),
2830
]);
2931
return { stylesheets };
3032
},
@@ -51,6 +53,9 @@ function Home() {
5153
<RemoteBoundary fallback="Loading the Vite remote…" name="Vite remote">
5254
<ViteStatusCard />
5355
</RemoteBoundary>
56+
<RemoteBoundary fallback="Loading the Router-aware remote…" name="Router-aware remote">
57+
<ViteRouterCard />
58+
</RemoteBoundary>
5459
</div>
5560
</main>
5661
);

‎apps/vite-host/package.json‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313
"dependencies": {
1414
"@module-federation/runtime": "2.9.2",
1515
"@tanstack/react-router": "1.170.41",
16+
"@tanstack/router-core": "1.171.34",
1617
"@tanstack/react-start": "1.168.60",
1718
"example-host-context": "workspace:*",
1819
"react": "19.3.0",

‎apps/vite-host/src/remote.d.ts‎

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ declare module "tanstack_vite_remote/StatusCard" {
55
export default StatusCard;
66
}
77

8+
declare module "tanstack_vite_remote/RouterCard" {
9+
import type { ComponentType } from "react";
10+
11+
const RouterCard: ComponentType;
12+
export default RouterCard;
13+
}
14+
815
declare module "tanstack_rsbuild_remote/StatusCard" {
916
import type { ComponentType } from "react";
1017

0 commit comments

Comments
 (0)