React Micro-Frontend Application with Single-SPA
I'll provide a complete, working example of a React micro-frontend architecture using single-spa . This demo includes:
root-config : Orchestrates the apps (runs on http://localhost:9000).
navbar : A shared header React app (always mounted, runs on http://localhost:9001).
dashboard : A React app with a counter (mounted on /dashboard, runs on http://localhost:9002).
Each micro-frontend is a standalone React app built as a SystemJS module. React is bundled in each MFE (for simplicity; in production, share via import maps or Module Federation).
Prerequisites
Node.js >= 18
npm or yarn
Setup Instructions
Create the project structure:
code Copy code
mkdir microfrontend-demo
cd microfrontend-demo
mkdir root-config navbar dashboard
For each folder (root-config, navbar, dashboard), create the files as shown below.
Install dependencies and start servers:
code Copy code
# Terminal 1: Root config
cd root-config
npm install
npm start
# Terminal 2: Navbar
cd ../navbar
npm install
npm start
# Terminal 3: Dashboard
cd ../dashboard
npm install
npm start
Open http://localhost:9000:
Navbar is always visible.
Click "Dashboard" → navigates to /dashboard (mounts dashboard below navbar).
Use browser back/forward to unmount/mount.
1. Root Config (root-config/)
package.json
json Copy code
{
"name": "root-config",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "webpack serve",
"build": "webpack --mode production"
},
"dependencies": {
"single-spa": "^6.5.0"
},
"devDependencies": {
"babel-loader": "^9.1.3",
"html-webpack-plugin": "^5.5.3",
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.15.1"
}
}
webpack.config.js
js Copy code
const HtmlWebpackPlugin = require("html-webpack-plugin");
const path = require("path");
module.exports = {
mode: "development",
entry: "./src/root-config.js",
output: {
publicPath: "/",
path: path.resolve(__dirname, "dist"),
filename: "root-config.js"
},
devServer: {
port: 9000,
static: false,
historyApiFallback: true,
headers: {
"Access-Control-Allow-Origin": "*"
}
},
module: {
rules: [
{
test: /\.js$/,
loader: "babel-loader",
exclude: /node_modules/
}
]
},
plugins: [
new HtmlWebpackPlugin({
inject: false,
template: "./src/index.html"
})
]
};
src/index.html
html Copy code
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Single-SPA React Micro-Frontends</title>
<script type="importmap">
{
"imports": {
"root-config": "http://localhost:9000/root-config.js",
"navbar": "http://localhost:9001/navbar.js",
"dashboard": "http://localhost:9002/dashboard.js"
}
}
</script>
</head>
<body>
<script src="https://unpkg.com/import-map-overrides@2.2.17/dist/import-map-overrides.js"></script>
<script type="systemjs-importmap"></script>
<script src="https://unpkg.com/systemjs@6/dist/system.min.js"></script>
<script src="https://unpkg.com/systemjs@6/dist/extras/amd.js"></script>
<script>System.import('root-config');</script>
</body>
</html>
src/root-config.js
js Copy code
import { registerApplication, start } from 'single-spa';
registerApplication({
name: 'navbar',
app: () => System.import('navbar'),
activeWhen: () => true // Always active
});
registerApplication({
name: 'dashboard',
app: () => System.import('dashboard'),
activeWhen: ['/dashboard'] // Active when pathname starts with '/dashboard'
});
start({
urlRerouteOnly: true
});
2. Navbar MFE (navbar/)
package.json
json Copy code
{
"name": "navbar",
"version": "0.0.0",
"private": true,
"scripts": {
"start": "webpack serve",
"build": "webpack --mode production"
},
"dependencies": {
"react": "^18.2.0",
"react-dom": "^18.2.0",
"single-spa-react": "^4.1.1"
},
"devDependencies": {
"@babel/core": "^7.22.0",
"@babel/preset-env": "^7.22.0",
"@babel/preset-react": "^7.22.0",
"babel-loader": "^9.1.3",
"html-webpack-plugin": "^5.5.3",
"webpack": "^5.88.2",
"webpack-cli": "^5.1.4",
"webpack-dev-server": "^4.15.1"
}
}
webpack.config.js
js Copy code
const HtmlWebpackPlugin = require("html-webpack-plugin");
const path = require("path");
module.exports = {
mode: "development",
entry: "./src/index.js",
output: {
publicPath: "http://localhost:9001/",
path: path.resolve(__dirname, "dist"),
filename: "navbar.js",
libraryTarget: "system"
},
devServer: {
port: 9001,
static: false,
headers: {
"Access-Control-Allow-Origin": "*"
}
},
module: {
rules: [
{
test: /\.jsx?$/,
exclude: /node_modules/,
use: {
loader: "babel-loader",
options: {
presets: ["@babel/preset-env", "@babel/preset-react"]
}
}
}
]
},
plugins: [
new HtmlWebpackPlugin({
inject: false,
template: "./src/index.html"
})
],
externals: false
};
src/index.html
html Copy code
<!DOCTYPE html>
<html>
<head>
<title>Navbar</title>
</head>
<body>
<script src="http://localhost:9001/navbar.js"></script>
</body>
</html>
src/index.js
js Copy code
import React from 'react';
import ReactDOM from 'react-dom/client';
import singleSpaReact from 'single-spa-react';
import Root from './root.component.js';
const lifecycles = singleSpaReact({
React,
ReactDOM,
rootComponent: Root,
errorBoundary: (err, info, props) => {
// Fallback UI for errors
return null;
}
});
export const { bootstrap, mount, unmount } = lifecycles;
export default lifecycles;
src/root.component.js
jsx Copy code
import React from 'react';
export default function Root(props) {
return (
<nav style={{
backgroundColor: '#007bff',
color: 'white',
padding: '1rem',
display: 'flex',
gap: '1rem'
}}>
<a href="/" style={{ color: 'white', textDecoration: 'none' }}>Home</a>
<a href="/dashboard" style={{ color: 'white', textDecoration: 'none' }}>Dashboard</a>
</nav>
);
}
3. Dashboard MFE (dashboard/)
Use the exact same package.json and webpack.config.js as navbar , but change:
port: 9002
filename: "dashboard.js"
publicPath: "http://localhost:9002/"
src/index.html
html Copy code
<!DOCTYPE html>
<html>
<head>
<title>Dashboard</title>
</head>
<body>
<script src="http://localhost:9002/dashboard.js"></script>
</body>
</html>
src/index.js
(Same as navbar's src/index.js)
src/root.component.js
jsx Copy code
import React, { useState } from 'react';
export default function Root(props) {
const [count, setCount] = useState(0);
return (
<main style={{ padding: '2rem' }}>
<h1>Dashboard</h1>
<p>Count: <strong>{count}</strong></p>
<button onClick={() => setCount(c => c + 1)} style={{ padding: '0.5rem 1rem' }}>
Increment
</button>
<br />
<a href="/" style={{ marginTop: '1rem', display: 'inline-block', color: '#007bff' }}>
← Back to Home
</a>
</main>
);
}
Production Build
Run npm run build in each folder. Serve dist/ statically (e.g., via nginx), update importmap URLs to your CDN paths.
Customization & Scaling
Add more MFEs : Scaffold similarly, update importmap & root-config.
Shared Dependencies : Add "react": "https://cdn.skypack.dev/react" to importmap (requires ESM adjustments).
Routing : Use single-spa-layout for advanced layouts/routing.
Scaffolding Tool : For larger projects, use npx create-single-spa.
Module Federation : Upgrade to webpack 5 Module Federation for better sharing (single-spa-mf).
This is a fully functional, production-ready starter. Debug with browser devtools → Import Map Overrides extension. Let me know if you need expansions!