I'm planning to outline the prerequisites and architecture for microfrontends, specifically focusing on the root-config and different MFEs like navbar, home, and dashboard. I need to explain how to bootstrap these with create-single-spa.
I’ll cover the configuration using webpack, noting that while single-spa primarily uses SystemJS, module federation can integrate with it. I should provide detailed steps starting with ‘npm init single-spa’ and clarifying the latest CLI changes to npx create-single-spa. Plus, I'll show examples of MFEs like navbar and home.Describing micro-frontend setup
It's important to lay out the base index.ejs for hosting the import map, including what scripts to include for loading the micro-frontends correctly. I’ll mention that create-single-spa sets everything up automatically through the root config, but since the user wants app development, I can’t replicate the entire CLI output.
Instead, I'll provide a skeleton structure and highlight the single-spa-react adapter. I will outline steps including project structure, creating the root-config, building the React apps, configuring the import map, and running everything concurrently with npm start. I can also add sample code to illustrate components and address key micro-frontend architecture aspects like independent deploys and shared dependencies.Outlining React Micro-Frontend Development
The user has requested a development guide for a React application using micro-frontend architecture with single-spa. I can’t provide a zipped repo, but I can outline the process along with some code snippets.
I need to find the right balance since micro-frontends can be complex. I’m thinking of organizing the content into sections: an overview of architecture and tools, implementation steps including the root config and React micro-frontends, shared UI layout, and running the app locally.
I’ll also touch on optional advanced topics like module federation and communication. I’ll format this using Markdown because it's coders I’m addressing, and I’ll include clear headings and code blocks. Below is a practical way to build a React micro‑frontend system with single-spa. I’ll use the official create-single-spa CLI and show you a minimal but complete setup: one root-config and two React micro‑frontends.
1. High-level architecture
We’ll build:
Root config: @company/root-config
Loads SystemJS, import maps, and registers all microfrontends.
MFEs (React single-spa applications):
@company/navbar – a nav/header bar
@company/home – main home page content
Each MFE is an independent React app with its own build, dev server, and deployment. The root config wires them together in the browser.
Resulting structure (monorepo style):
text
microfrontends/
root-config/
navbar/
home/
2. Prerequisites
Node.js ≥ 16
npm or yarn
Basic knowledge of React & webpack
Install the CLI:
bash
npm install -g create-single-spa
# or use npx each time: npx create-single-spa
3. Create the root config
From your workspace folder:
bash
mkdir microfrontends
cd microfrontends
create-single-spa
Answer prompts approximately like:
Directory:root-config
Type:single-spa root config
Organization name:company
Use webpack: yes
Use TypeScript: up to you (I’ll assume JS for brevity)
This generates root-config/ with:
src/root-config.js – registers applications
src/index.ejs – HTML template (includes SystemJS, import maps)
webpack.config.js
3.1. Root config registration (key file)
root-config/src/root-config.js will look similar to:
js
import { registerApplication, start } from "single-spa";
registerApplication({
name: "@company/navbar",
app: () => System.import("@company/navbar"),
activeWhen: ["/"], // show on all routes
});
registerApplication({
name: "@company/home",
app: () => System.import("@company/home"),
activeWhen: ["/"], // show on "/"
});
start({
urlRerouteOnly: true,
});
You’ll later add or modify registerApplication entries here.
3.2. Root HTML & import map
create-single-spa gives you src/index.ejs. Key part is the import map:
This compiles to a System.register module that SystemJS can load via the import map.
Run navbar:
bash
cd navbar
npm install
npm start # often defaults to port 8500
5. Create a React micro-frontend: Home
Again from microfrontends/:
bash
create-single-spa
Prompts:
Directory:home
Type:single-spa application / parcel
Framework:react
Organization name:company
Project name:home
Key files:
home/src/company-home.js:
js
import React from "react";
import ReactDOM from "react-dom/client";
import singleSpaReact from "single-spa-react";
import Root from "./root.component";
const lifecycles = singleSpaReact({
React,
ReactDOM,
rootComponent: Root,
errorBoundary(err, info, props) {
return <div>Home error</div>;
},
});
export const bootstrap = lifecycles.bootstrap;
export const mount = lifecycles.mount;
export const unmount = lifecycles.unmount;
home/src/root.component.js:
js
import React from "react";
export default function Root(props) {
return (
<main style={{ padding: "1rem" }}>
<h1>Home micro-frontend</h1>
<p>
This content is served by the <code>@company/home</code> React app.
</p>
<p>
You can build other MFEs (e.g., About, Dashboard, Settings) and
register them from the root-config.
</p>
</main>
);
}
Run:
bash
cd home
npm install
npm start # choose a free port, e.g. 8501
6. Wire MFEs into the root-config
Ensure your root import map (root-config/src/index.ejs) has correct URLs for dev:
And that the root config registration (root-config/src/root-config.js) references these names:
js
import { registerApplication, start } from "single-spa";
registerApplication({
name: "@company/navbar",
app: () => System.import("@company/navbar"),
activeWhen: ["/"], // show on all routes
});
registerApplication({
name: "@company/home",
app: () => System.import("@company/home"),
activeWhen: ["/"], // show on "/"
});
// You can add more apps, for example:
// registerApplication({
// name: "@company/about",
// app: () => System.import("@company/about"),
// activeWhen: ["/about"],
// });
start({ urlRerouteOnly: true });
7. Run everything
In separate terminals:
bash
# Terminal 1
cd microfrontends/root-config
npm start # http://localhost:9000
# Terminal 2
cd microfrontends/navbar
npm start # http://localhost:8500 (serves the navbar bundle)
# Terminal 3
cd microfrontends/home
npm start # http://localhost:8501 (serves the home bundle)
Open:
text
http://localhost:9000
The browser will:
Load @company/root-config via SystemJS.
Root-config will registerApplication for @company/navbar and @company/home.
SystemJS loads @company/navbar and @company/home bundles from their respective ports.
React renders the navbar and home components into the DOM.
8. Extending this setup
Once the basic system works, common next steps:
More MFEs: repeat create-single-spa for @company/about, @company/dashboard, etc., and register them in root-config with different activeWhen routes.
Shared dependencies: configure webpack externals or import them from a shared app to avoid duplicates (e.g., React as a shared external).
Global layout: use a layout engine (e.g., single-spa-layout) to declaratively define which MFE shows where based on route.
Cross-MFE communication:
Use custom events, a shared global store, or URL-based communication.
Independent deployment:
Host each MFE on its own domain/path and update the import map at runtime or via server-side template.
If you tell me whether you prefer JavaScript or TypeScript, and whether you want a monorepo (e.g., with pnpm workspaces or Nx) or separate repos, I can adapt this into a concrete folder structure and config set tailored to your environment.