-
Problem(s):Contex: I have a setup with two routers, one in a host MFE, and one in a remote MFE, both using rsbuild and Module Federation 2.0.
What I would expect:
What I've tried:
Extra thoughtsThis could be user error, due to lack of knowledge with this package. if so I apologize, I read the docs all last night, but it's possible I missed some key details. Screenshots and code snippets are below. Package versions: ScreenshotsClick me to see screenshots(Context for screenshot: What you're seeing is the remote MFE loaded (I've scrolled down the page to see it) and the remote MFE's router's RouterTrace table. At the very bottom of the page is the RouterTrace for the Host MFE's router) Navigated to the route that loads my remote MFE and clicked the Overview link to navigate to the overview page. As you can see, the url path is correct, but the remote router doesn't match in the RouterTrace, and the component displayed is the Fallback. Clicking the Underview link has the same behavior: url changes to 'http://localhost:3000/svelte/underview, but remote router doesn't match, and the component displayed is fallback.
Screenshot of the error when navigating away from and back to the remote component with the nested router. Code (Host):<script lang="ts">
// Host App.svelte
import Navbar from "./components/navbar.svelte";
import { Router, Route, Fallback, RouterTrace, init } from '@svelte-router/core'
import { onMount } from "svelte";
import Home from "./pages/Home.svelte";
import Documents from "./pages/Documents.svelte";
import DynamicRoute from "./pages/DynamicRoute.svelte";
let routerInitialized = $state(false);
onMount(() => {
init({});
routerInitialized = true;
});
</script>
<main>
<div class="content">
<h1>Rsbuild with Svelte</h1>
<p>Start building amazing things with Rsbuild.</p>
{#if routerInitialized}
<Router id="host-router">
<Navbar />
<Route key="root" path="/">
<div>root</div>
</Route>
<Route key="home" path="/home">
<Home />
</Route>
<Route key="documents" path="/documents">
<Documents />
</Route>
<Route key="svelte" path="/svelte">
<DynamicRoute name="svelte" basePath="/svelte" entry="http://localhost:3001/mf-manifest.json" />
</Route>
<Fallback>
<div>fallback</div>
</Fallback>
<RouterTrace />
</Router>
{/if}
</div>
</main><script lang="ts">
// Host - DynamicRoute.svelte
import { loadRemote, registerRemotes } from "@module-federation/enhanced/runtime";
import { type Component } from "svelte";
let { name, basePath, entry } = $props();
let RemoteComponent: Component | null = $state(null);
$effect(() => {
try {
registerRemotes([
{
name: `${name}-remote`,
entry: entry,
},
]);
loadRemote( `${name}-remote/App`).then((mod: any) => {
if (!mod || !mod?.default) {
throw new Error("Remote module missing default component");
}
RemoteComponent = mod.default;
});
} catch (err) {
console.log(err);
}
});
</script>
{#if RemoteComponent}
<RemoteComponent {basePath}/>
{/if}<script lang="ts">
// Host - Navbar.svelte
import { Link } from "@svelte-router/core";
</script>
<div>
<Link href="/home">Home</Link>
<Link href="/documents">Documents</Link>
<Link href="/svelte">Svelte Remote</Link>
</div>Code (Remote):<script lang="ts">
// Remote - App.svelte
import { Router, Route, Fallback, RouterTrace, init } from '@svelte-router/core';
import Overview from './pages/Overview.svelte';
import Navbar from './components/Navbar.svelte';
import { onMount } from 'svelte';
import Underview from './pages/Underview.svelte';
let { basePath } = $props();
let routerInitialized = $state(false);
onMount(() => {
init({});
routerInitialized = true;
});
</script>
<main>
<div class="content">
<h1>Rsbuild with Svelte</h1>
<p>Start building amazing things with Rsbuild.</p>
<div>Hello from Remote!</div>
{#if routerInitialized}
<Router id="svelte-remote-router" {basePath}>
<Navbar {basePath} />
<Route key="overview" path="/overview">
<Overview />
</Route>
<Route key="underview" path="/underview">
<Underview />
</Route>
<Fallback>
<div>Fallback</div>
</Fallback>
<RouterTrace />
</Router>
{/if}
</div>
</main><script lang="ts">
// Remote - Navbar.svelte
import { Link } from "@svelte-router/core";
let { basePath } = $props();
</script>
<div>
<Link href={basePath + "/overview"}>Overview</Link>
<Link href={basePath + "/underview"}>Underview</Link>
</div> |
Beta Was this translation helpful? Give feedback.
Replies: 5 comments 7 replies
-
|
Hello. The In the host MFE, make sure you use inexact path matching by using the For the host, I recommend moving the call to After doing this, I think it should work. Docs References: https://svelte-router.dev/docs/library-initialization |
Beta Was this translation helpful? Give feedback.
-
|
Oh nice! I didn't realize there was a cleanup function returned from init, that solved the error of navigating away from and back to the remote, thanks! I implemented your suggestion of using the Thanks for the super quick and clear support! I appreciate it. |
Beta Was this translation helpful? Give feedback.
-
|
Ok. First, switching to Discussion. Second: Can you post this in a repo so I can experiment? Third: Could we chat live, maybe via Google Meet? |
Beta Was this translation helpful? Give feedback.
-
|
Working on getting repos setup now and I'll send the links when those are ready. |
Beta Was this translation helpful? Give feedback.
-
|
Solution:
|
Beta Was this translation helpful? Give feedback.




Solution:
Cannot override current location error: init can only be run once, if you need to run it twice, make sure to deinit, from the returned function by calling init, first before calling init a second time!
MFE remote router not matching route properly: If you externalize the svelte runtime in your Module Federation configuration, make sure to externalize this package too! Once you do they should connect up nicely!