This repo demonstrates how to implement SolidJS SSR with module federation. For this example we're using a React based app to expose a component.
We're using the OriginJS's Module Federation plugin for Vite. @originjs/vite-plugin-federation
And vite-plugin-ssr for SSR.
The host is a SolidJS SSR app which wraps the entire application. This is what the end-user would interact with.
Key files to look at:
/host/vite.config.js
- this implements thefederation
plugin and defines theremotes
it wants to access./host/src/components/RemoteNav.tsx
- this imports theNav
component from the remote./host/pages/index.page/tsx
- This is the homepage - uses theRemoteNav
component.
Diagram below demonstrates the architecture of the app:
The remote is a React (although other frameworks are supported by the Federation plugin) app which exposes a Nav
component.
Key files to look at:
/remote/src/components/Nav.jsx
- this is the component to be exposed/remote/vite.config.js
- this implements thefederation
plugin and exposes the component. It also sharesreact
andreact-dom
libraries.
Firstly with need to build and start the remote, the remote will start on port 5001.
From the root directory run:
cd remote && pnpm i && pnpm build && pnpm serve
Now, let's start the host app, this will start on port 3000.
From the root directory run:
cd host && pnpm i && pnpm dev
Navigate to http://localhost:3000/ and the app should be running.
An issue exists with the Module Federation plugin that might prevent some React components with hooks from being able to be imported - originjs/vite-plugin-federation#294
Thanks to Rom for his example of SolidJS SSR.