React micro frontends with Module Federation: split a monolith
When micro frontends are worth it, how a shell and its remotes fit together, and the contracts that keep independent deploys from breaking each other.
By M. Adnan Saleem · · Updated · 8 min read
On the Qatar Events Platform I rearchitected a monolithic React frontend into micro frontends using Module Federation. With versioned contracts between the shell and the remotes, release cycle time dropped by about 40% and teams could deploy independently. This is the shape of that kind of split, and the parts that decide whether it helps or hurts.
The Qatar case study includes a conceptual architecture diagram and before/after delivery workflow, with the limits of the reported result. The code below uses illustrative module names and example domains rather than production configuration.
First, check that you have the problem
Micro frontends solve an organisational problem, not a technical one. They are worth the cost when several teams ship into one frontend and block each other: one release train, one long build, one team's unfinished work holding up another team's fix. If one team owns the whole frontend, a well-structured monolith with code splitting is simpler and faster.
The pieces
A shell (also called the host) owns the page frame, routing, authentication and the shared design system. Each remote is a separately built and deployed application that exposes one or more modules. At runtime the shell fetches a small manifest from each remote, called remoteEntry.js, and loads the exposed module from it.
The remote declares what it exposes:
// events/webpack.config.js
new ModuleFederationPlugin({
name: 'events',
filename: 'remoteEntry.js',
exposes: {
'./EventsApp': './src/EventsApp',
},
shared: {
react: { singleton: true, requiredVersion: '^18.0.0' },
'react-dom': { singleton: true, requiredVersion: '^18.0.0' },
},
});The shell declares where to find it:
// shell/webpack.config.js
new ModuleFederationPlugin({
name: 'shell',
remotes: {
events: 'events@https://events.example.com/remoteEntry.js',
},
shared: {
react: { singleton: true, requiredVersion: '^18.0.0' },
'react-dom': { singleton: true, requiredVersion: '^18.0.0' },
},
});The shared block matters most. React must be a singleton: two copies on one page break hooks and context. Declaring a required version makes a mismatch a visible warning, not a silent second copy.
Shared versions, in detail
Three settings on each shared library decide what happens when the shell and a remote were built against different versions. With singleton, webpack keeps one copy and picks the highest version present. requiredVersion is the range this build was tested against. By default a singleton that falls outside that range is still used, with a console warning that nobody reads in production. strictVersion turns that into a runtime error, which is what you want in CI and staging:
shared: {
react: {
singleton: true, // one copy on the page; the highest version wins
requiredVersion: '^18.0.0', // what this build was tested against
strictVersion: true, // a mismatch is an error, not a console warning
},
}Keep the shared list short. Every library in it is negotiated at startup, and every one that is not a singleton can be downloaded twice when versions diverge. React, the router and the design system belong there; a date library used by one remote does not.
The contract is the product
Independent deploys only work if the boundary between shell and remote is treated as a public interface. In practice that means three rules.
- Small, typed surface. A remote exposes a root component with a handful of props (the user, a base path, callbacks for navigation). It does not reach into the shell's store.
- Versioned. A breaking change to the props is a new major version of the contract, and the shell keeps supporting the previous one until every remote has moved.
- Checked in CI. The contract types live in a shared package, so a remote that drifts fails its own build, not production.
Fail small
A remote is a network dependency, so it can fail to load. Load each one lazily behind an error boundary, so a broken remote takes down its own section and not the whole page:
const EventsApp = React.lazy(() => import('events/EventsApp'));
<ErrorBoundary fallback={<SectionUnavailable name="Events" />}>
<Suspense fallback={<SectionSkeleton />}>
<EventsApp user={user} basePath="/events" />
</Suspense>
</ErrorBoundary>The demo at the end of this article has three sections behind boundaries like this one, with a button to break each.
Start the app behind an async boundary
Shared modules are negotiated at runtime: the shell and its remotes compare versions and agree on one copy before any of them uses it. That takes a moment, so the entry file cannot import React directly. If it does, webpack stops with "Shared module is not available for eager consumption". The fix is to move the real entry into a second file and import it dynamically:
// shell/src/index.js: the only line in the entry file
import('./bootstrap');
// shell/src/bootstrap.js: what index.js used to contain
import React from 'react';
import { createRoot } from 'react-dom/client';
import App from './App';
createRoot(document.getElementById('root')).render(<App />);Every shell and every remote needs this, and it is the first thing to check when a new remote fails on start.
Routing: split the path, not the router
Two routers fighting over one URL is the most common early bug. Give the shell the first path segment and give each remote everything below its own prefix:
// The shell owns the first path segment and hands the rest to the remote
<Routes>
<Route path="/" element={<Home />} />
<Route path="/events/*" element={<EventsApp user={user} basePath="/events" />} />
</Routes>The remote builds its links from basePath, never from a hard-coded /events, so the shell can mount it anywhere. Navigation between remotes goes through the URL or through a callback in the contract, not through a shared router instance.
Give the compiler the remote's types
TypeScript cannot see a module that only exists at runtime, so an import from a remote is untyped unless you declare it. Point the declaration at the shared contract package:
// shell/src/remotes.d.ts
declare module 'events/EventsApp' {
import type { ComponentType } from 'react';
import type { EventsAppProps } from '@platform/contracts';
const EventsApp: ComponentType<EventsAppProps>;
export default EventsApp;
}Now a shell that passes the wrong props fails to compile, and a remote that changes its props without a new contract version fails its own build.
Deploys and caching
Independent deploys depend on two caching rules that pull in opposite directions.
remoteEntry.jsmust stay fresh. Its name never changes, so serve it with a short cache lifetime or with revalidation. A shell that holds a stale copy keeps loading last week's remote.- Chunks can be cached for a long time. Give them content hashes in their file names and cache them for as long as you like.
- Keep old chunks on the server for a while. A user with the page already open still holds the previous manifest, which points at the previous chunk names. Delete those on deploy and that user gets a failed load on their next click.
- Rolling back is a redeploy of the previous
remoteEntry.js. Because the old chunks are still there, it takes effect on the next page load.
Module Federation 2.0: manifests and runtime loading
The configuration above is the webpack 5 original and still works. Module Federation 2.0, published as @module-federation/enhanced, keeps the same name, exposes, remotes and shared shape, runs on webpack and Rspack, and adds two things worth having.
// shell/webpack.config.js (rspack.config.js is the same shape)
const { ModuleFederationPlugin } = require('@module-federation/enhanced/webpack');
new ModuleFederationPlugin({
name: 'shell',
remotes: {
// The manifest instead of remoteEntry.js: type hints, preloading, devtools
events: 'events@https://events.example.com/mf-manifest.json',
},
shared: {
react: { singleton: true, requiredVersion: '^18.0.0' },
'react-dom': { singleton: true, requiredVersion: '^18.0.0' },
},
});The first is the manifest. Each remote build emits mf-manifest.json next to remoteEntry.js, describing what it exposes and shares. Point a remote at the manifest instead of the JavaScript entry and the host gets the remote's types without a hand-written declaration file, can preload the remote's assets, and shows up in the Module Federation devtools.
The second is a runtime API. The static import('events/EventsApp') needs the remote's URL at build time. With loadRemote the host loads a remote by name, and registerRemotes adds remotes from configuration or an API response, so one shell build can serve staging and production with different remote URLs:
import { loadRemote, registerRemotes } from '@module-federation/enhanced/runtime';
// Remotes from configuration or an API response, not from the build
registerRemotes([
{ name: 'events', entry: 'https://events.example.com/mf-manifest.json' },
]);
const EventsApp = React.lazy(() =>
loadRemote('events/EventsApp').then((m) => ({ default: m.default })),
);The error boundary and the async entry file do not change. A remote loaded through the runtime API is still a network dependency, and shared modules are still negotiated before the first render.
Working on one remote locally
Nobody should need every application running to change one. Run the remote you are working on locally and point the shell at the deployed versions of the others, with the remote URLs read from environment configuration. A remote should also start on its own, with a small stand-in for the shell that supplies the same props, so its team can develop and test without the rest of the platform.
Rule out the simpler options first
- A monorepo with clear ownership. One build, one deploy, with each team owning its folders and routes split into separate chunks. This solves slow builds and tangled code. It does not solve teams waiting on one release.
- Packages composed at build time. Each team publishes its section as a package and the shell installs it. Simple and type-safe, but every change still needs a shell release.
- Iframes. The strongest isolation there is, at the price of awkward sizing, routing, focus handling and shared sign-in.
Module Federation is the right answer only when independent runtime deploys are the actual requirement.
What goes wrong
- Shared state creep. The moment two remotes share a store, they deploy together again. Pass data through the contract or through the URL.
- CSS leaking across remotes. Use scoped styles (CSS Modules or a prefix per remote) and keep global styles in the shell only.
- Dependency drift. If remotes upgrade shared libraries at different times, users download several versions. Agree an upgrade window for the shared set.
- Slower first load. More requests before the first paint. Preload the remote entry for the route the user is most likely to open.
How to tell it worked
Measure the thing you were trying to fix: time from merge to production for a single team, and how often one team's release waits on another. Release cycle time is the number that moved for us. Bundle size and load time should stay flat or improve; if they got worse, the shared configuration needs another look.
Live demo
Try it live
Each section sits behind its own error boundary. Break one and use the others. All five demos.
- 3
- sections working
- 0
- sections down
- 1
- page still up
- Events
- Tickets
- Reports
Each section keeps its own click count, so you can see the others carry on while one is down.
Quick answers
The short version
What is Module Federation?
A bundler feature, introduced in webpack 5, that lets one JavaScript application load code from another separately built and deployed application at runtime, while sharing dependencies such as React between them.
When are micro frontends worth it?
When several teams ship into one frontend and block each other's releases. With a single team, a well-structured monolith with code splitting is simpler and faster.
How do micro frontends share React?
Each build lists React under shared with singleton set to true, so the page loads one copy and every remote uses it. Two copies on one page break hooks and context.
Does Module Federation work with Rspack and Vite?
Yes. Module Federation 2.0, published as @module-federation/enhanced, runs on webpack and Rspack, and the project ships integrations for Vite, Next.js and others. The configuration keeps the same shape: name, exposes, remotes and shared.
What is mf-manifest.json?
A manifest that Module Federation 2.0 builds emit next to remoteEntry.js. A host that points a remote at the manifest instead of the JavaScript entry gets the remote's types, resource preloading and devtools support.
Can a remote URL be decided at runtime?
Yes. The runtime API loadRemote loads a remote by name, and registerRemotes adds remotes from configuration or an API response, so one shell build can serve environments with different remote URLs.
What happens when a remote fails to load?
If it is loaded lazily behind an error boundary, only that section shows a fallback and the rest of the page keeps working.
Keep reading
Related articles
- A React custom hooks library a team will actually useWhich hooks belong in a shared React library, useSyncExternalStore for browser state, useEffectEvent for stale closures, and testing hooks with renderHook.
- Redux Toolkit, Zustand or Context: where React state belongsServer state, shared client state, local UI state and URL state each have a natural home. When to use TanStack Query, Redux Toolkit, Zustand or Context.
- Rendering 20,000 rows in React without freezing the pageA reproducible React virtualization benchmark with 20,000 synthetic rows, raw runs, median filter delays, fixed-height windowing code and measurement limits.
The project behind this
Read the case study
What I built, where, and the numbers that came out of it.
Related experience: Frontend developer.