Microfrontend Application using Single-SPA framework
Single SPA allows several teams to develop and manage their dedicated JavaScript applications independently from each other. Individual applications work together as a whole, serving same user experience. Microfrontend is a Microservice based approach for web development.
Single-SPA is one of the best frameworks used for microfrontend apps. It has full support for Angular, React, Vue like technology and by using a single-spa framework, it is possible to create a loosely coupled microfrontend application.
Using Single-SPA framework, applications built in Angular & React are deployed separately, and run all micro applications into one application
Single-SPA framework has an official site you can check the deployed code and examples as well there.
I will build a microfrontend application using Single-SPA, the applications to be deployed are:
- Main Container Application (Vanilla JavaScript)
- Navigation Application for routing control (Angular)
- Dummy Application 1 (React)
- Dummy Application 2 (React)
Installing Single-SPA framework
I am going to use the CLI to setup my microfrontends. Therefore make sure you have NodeJS (>= 10.3.0) installed on your computer as well, if you wish to follow along. NodeJS installation is going to install the Node Package Manager (npm) which is needed to install the single-spa CLI:
We will create our microfrontends in one repository for demo purposes, but with a production application, it is advisable to create a standalone git repository for each application.
Step 1: Creation of navigation application
The navigation application will be built using Angular and its responsible for routing control, for the entire microfrontend. Run the below command in the microfrontend-application directory:
Update the microfrontend-application/navigation/package.json file to serve the application on port 9001:
In application main routing module, set up a wildcard route and add a provider:
Step 2: Creation of dummy application 1
The first dummy application will be built using React and we will set this as the landing page of our microfrontend. Run the below command in the microfrontend-application directory:
Update the microfrontend-application/dummy-1/package.json file to serve the application on port 9002:
Step 3: Creation of dummy application 2
The second dummy application will be built using React and we will set this as a details application page of our microfrontend. Run the below command in the microfrontend-application directory:
Update the microfrontend-application/dummy-2/package.json file to serve the application on port 9003:
Step 4: Creation of container application
The container application is responsible to load configuration, common decencies, design system, and sub applications built using Angular, React, Vue etc. Run the below command in the microfrontend-application directory:
There's a few configurations that need to be done to add the sub applications to the container application. Let's go through them:
- Update the microfrontend-application/container/package.json file to serve the application on port 9000:
- Let's create helper functions in the
- file to help with registering the sub applications:
- Update the src/thabo-root-config.js file assign paths for our sub applications:
- Single-spa provides a layout engine that allows us to define where to render our applications on the dom. Modify the src/microfrontend-layout.html file:
<single-spa-router>
<div style="height:98vh; display: flex;">
<div style="height: 100%; width: 400px; background-color: aqua;">
<application name="@thabo/navigation"></application>
</div>
<div style="width: 100%; background-color: pink;">
<application name="@thabo/dummy-1"></application>
<application name="@thabo/dummy-2"></application>
</div>
</div>
</single-spa-router>
See it in action
I will start up all the apps and visit the browser on http://localhost:9000 where the container app is being served on my machine:
This was just a basic setup of our microfrontend application, if you are having any issues please check the microfrontend-application repo on GitHub.
Summary
There you have it, we just created a microfrontend architecture composed of a one Angular application, two React applications and a Vanilla JavaScript app. I will build on this solution to add more functionality and deploy the final product to AWS!