I'm now focusing on the user's ultimate goal: understanding the reasoning behind needing the guide. Specifically, why React micro-frontends with single-spa. This involves clarifying the motivation and context.
Defining the Architecture
I've been mapping out the structure, focusing on a guide to build a React application with a micro-frontend architecture using single-spa. I've broken it down into a root config (container), individual React apps, and a shared dependency. Currently, I'm leaning heavily on create-single-spa and webpack as the key tools.
Detailing the Micro-Frontends
I'm now detailing each micro-frontend step-by-step. I'm focusing on the React apps, including app-nav and app-page, and outlining the necessary code modifications to distinguish them. Node.js and CLI installation are pre-requisites. The guide will cover the root config, including microfrontend-layout.html and root-config.js, emphasizing Import Maps for browser file location.
Elaborating on Port Allocation
I'm now detailing the port assignment for local development of each micro-frontend (MFE). Ensuring each has a unique port is crucial, as is how the root config locates individual applications through the import map. I've begun to define the import map, mapping local host ports to their corresponding MFEs. The focus is on the index.ejs, using the layout engine approach, and drafting the <script type="systemjs-importmap">. I've started the code examples for the root config and React app, pointing to main.js and demonstrating bootstrap, mount, unmount. Additionally, I will address common pain points like CORS.
Addressing Dependency Management
I'm now zeroing in on dependency management, specifically shared libraries like React and ReactDOM. Although bundling these internally simplifies the "Hello World" example, I recognize this contradicts micro-frontend best practices, causing bloating and version conflicts. The create-single-spa default externalizes React via the import map, and I'll stick with that approach.
Defining SystemJS Roles
I'm zeroing in on detailing the role of SystemJS as the module loader facilitating in-browser operations. Understanding this is pivotal to grasp the mechanics of React micro-frontends with single-spa. This also involves clarifying the separation between the Root Config (the shell) and the Applications (features), to avoid beginner confusion. This is the crucial part. I also am looking at how to include a "Bonus" section on Routing, Shared State and API calls, and final checks on commands.
This is a comprehensive guide to building a React application using Micro-frontend architecture with single-spa.
We will create three distinct projects:
Root Config: The container (shell) that orchestrates everything.
Navbar App: A React micro-frontend that stays on every page.
Dashboard App: A React micro-frontend that loads on a specific route.
Prerequisites
You need Node.js installed. We will use the official CLI tool create-single-spa.
Install the CLI globally:
bash
npm install -g create-single-spa
Step 1: Set up the Root Config
The Root Config is the HTML page that loads your javascript bundles.
Create a directory for your whole project and enter it:
bash
mkdir my-micro-frontends && cd my-micro-frontends
Generate the root config:
bash
create-single-spa
Select the following options:
Directory: root-config
Type: single-spa root config
Package manager: npm
TypeScript: No (keeping it simple for this tutorial)
Layout Engine: Yes (Recommended)
Organization name: my-org (Remember this name!)
Install dependencies:
bash
cd root-config
npm install
Step 2: Create the Navbar Micro-frontend
Now we create the React application that serves as the navigation bar.
Go back to the main folder:
bash
cd ..
Generate the app:
bash
create-single-spa
Select the following options:
Directory: navbar
Type: single-spa application / parcel
Framework: react
Package manager: npm
TypeScript: No
Organization name: my-org (Must match the root config)
Project name: navbar
Modify the Code: Open navbar/src/root.component.js and make it look like a nav bar:
2. Define the Layout (src/microfrontend-layout.html)
This file defines the DOM structure.
Modify root-config/src/microfrontend-layout.html:
html
<single-spa-router>
<!-- The Navbar is displayed on all routes -->
<application name="@my-org/navbar"></application>
<main>
<!-- The dashboard is only displayed on /dashboard path -->
<route path="dashboard">
<application name="@my-org/dashboard"></application>
</route>
<!-- Default route -->
<route default>
<h1>Welcome to the Home Page (Root)</h1>
</route>
</main>
</single-spa-router>
Step 6: Run the Application
Open a 3rd terminal.
Navigate to root-config.
Start the root config server:
bash
npm start
Open your browser to http://localhost:9000.
The Result
Home Page: You will see the Black Navbar (from the React Navbar App) and the text "Welcome to the Home Page" (from the HTML layout).
Dashboard: Click the "Dashboard" link in the nav. The URL changes to localhost:9000/dashboard. The Navbar stays, and the Dashboard React App loads below it.
How it works fundamentally:
Browser hits port 9000 (Root Config).
Root Config reads the Layout file.
It sees it needs @my-org/navbar.
It looks at the Import Map to find the URL for @my-org/navbar (port 8500).
SystemJS downloads the JS reference from port 8500 and executes it.
React mounts the component into the DOM.
Next Steps (Architecture Tips)
Shared State: If Navbar needs to talk to Dashboard, avoid direct coupling. Use RxJS subjects in a separate "utility" micro-frontend or Custom Browser Events.
CSS Isolation: Use CSS Modules or Styled Components within your React apps to prevent styles from one app bleeding into another.
Deployment: In production, you upload your bundled JS files to a CDN (like AWS S3). You then update the Import Map in your production Root Config to point to the S3 URLs instead of localhost.