Fun with Micro-frontend in a single-spa way💡
Tags: javascript, microfrontend, angular, react
Published: 2021-07-11 13:52:02


🏃‍♂️
To give everyone the context this post will be based on the experiments which I used to run the multiple front-end applications under a vanilla js container app.

## What is a Micro-frontend application?

This application is based on a [single-spa](https://single-spa.js.org/) framework that allows you to run multiple front-end applications under the same route. Things to notice related to the Micro-frontend app

- Deploy the micro front-end applications independently.
- Without re-writing the existing application, you can create a new application with the new framework.
- Lazy loading the code base for improved performance.

## Pre-requisites

- Knowledge of any JavaScript framework
- JavaScript
- [single-spa](https://single-spa.js.org/docs/create-single-spa) should be globally installed in your machine

## Build the application

We will create three applications using the [single-spa](https://single-spa.js.org/) framework that are as follow:-

- root-config
- react-app
- angular-app

We will render the react and angular apps based on the routes.
So if the route is

- **/react** then react app will load
- **/angular** then the angular app will load

### Step 1:
Create a folder where you want to put all three applications together.
Navigate to the root of the folder and run the following command and follow the instructions as provided in the image:
```js
$ npx create-single-spa
```
![rootConfig](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/lx31qtibhcdfpf9gkc9g.PNG)
The root-config setup will complete in sometime.

### Step 2:
Now we will create an react application using the single-spa. So in the same root folder run the following command and follow the instructions as provided in the image:
```js
$ npx create-single-spa
```
![reactApplication](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/f4g81c3xyctowwcses5m.PNG)

### Step 3:
Now we will create an angular application using the single-spa. So in the same root folder run the following command and follow the instructions as provided in the image:
```js
$ npx create-single-spa
```
![angularApplication](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/tdwlc3r9skiau169kkvp.PNG)
Few more agreements for the angular app 😀
![angularApp2](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aj8opzsl26inbr7avbo2.PNG)
You need to additionally install few dependencies for the angular project
```js
$ npm i -D @angular-builders/custom-webpack single-spa
```

### Step 4:
Now we will add few minor tweaks in the files present in our newly created projects :-
- root-config
  - Update the imports in the index.ejs
    ```js
     {
      "imports": {
        "@orgname/root-config": "//localhost:9000/orgname-root-config.js",
        "@orgname/react-app": "//localhost:8080/orgname-react-app.js",
        "angular-app": "//localhost:4200/main.js"
      }
    }
    ```
    ![indexJS](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/of3fydm8ubixf0tbd9q6.PNG)
  - Update the configuration file named as orgname-root-config.ts
    ![configRoot](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pnpor0a8xsvp599xz448.PNG)
  - Add the navigation bar in index.ejs inside the body 
    ```js
    <style>
    .navbar {
      position: fixed;
      top: 0;
      left: 0;
      background-color: #111D4A;
      color: white;
      font-size: 18px;
      font-weight: bold;
      width: 100%;
      height: 75px;
      display: flex;
      align-items: center;
    }

    .navbar ul {
      display: flex;
      align-items: center;
      list-style-type: none;
      height: 100%;
    }

    .navbar li {
      padding-right: 24px;
      height: 100%;
    }

    .navbar li:hover {
      opacity: 0.8;
    }

    .navbar a {
      text-decoration: none;
      color: white;
    }
  </style>
  <div class="navbar">
    <ul>
      <a onclick="singleSpaNavigate('/react')">
        <li>
          React
        </li>
      </a>
      <a onclick="singleSpaNavigate('/angular')">
        <li>
          Angular
        </li>
      </a>
    </ul>
  </div>
    ```
- react-app
  - Update the root.component.tsx as below
    ![reactApp](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/r3d12eomu1qrnv7ljskv.PNG)
- angular-app
  - Import the zonejs as a dependency in main.single-spa.ts
   ![zone](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ke3qnf31d0rpf93fhu1i.PNG)
  - Add { provide: APP_BASE_HREF, useValue: '/angular' } to the providers array in the app.module.ts and import APP_BASE_HREF from @angular/common module
   ![appHref](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2dn0a2rkszgwee9c2dxz.PNG)  
   

### Step 5:
Run the applications 
- root-config
```js
$ npm start
```
The root-config app will be running on http://localhost:9000

- react-app
```js
$ npm start
```
The react-app will be running on http://locahost:8080

- angular-app
```js
npm run serve:single-spa:angular-app
```
The angular-app will be running on http://localhost:4200

Now when you navigate to http://localhost:9000 you should see
![root](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2i0rf6agkmy88usybohh.PNG)

When you click on React you should see
![react](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/2nkug5xo68aqwwnvship.PNG)

When you click on Angular you should see
![Angular](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/emq8t4ybg43viboo1aoh.PNG)

>When the route changes the new application mounts and the previously loaded application unmounts.

## GitHub Repositories for different applications
- [root-config](https://github.com/nitinreddy3/root-config)
- [react-app](https://github.com/nitinreddy3/react-app)
- [angular-app](https://github.com/nitinreddy3/angular-app)
- [react-ng-spa-app](https://github.com/nitinreddy3/react-ng-spa-app)

## Resources to catch up with single-spa
- [single-spa](https://single-spa.js.org/)
- [YouTube](https://www.youtube.com/playlist?list=PLLUD8RtHvsAOhtHnyGx57EYXoaNsxGrTU)

Do let me know about your experience with single-spa, till then keep on learning new stuffs and enjoy!!!
📖
{% user nitinreddy3 %}