Developing and Deploying Micro-Frontends with single-spa

Tyler Hawkins - Jul 28 '20 - - Dev Community

Micro-frontends are the future of frontend web development. Inspired by microservices, which allow you to break up your backend into smaller pieces, micro-frontends allow you to build, test, and deploy pieces of your frontend app independently of each other. Depending on the micro-frontend framework you choose, you can even have multiple micro-frontend apps -- written in React, Angular, Vue, or anything else -- coexisting peacefully together in the same larger app!

In this article, we're going to develop an app composed of micro-frontends using single-spa and deploy it to Heroku. We'll set up continuous integration using Travis CI. Each CI pipeline will bundle the JavaScript for a micro-frontend app and then upload the resulting build artifacts to AWS S3. Finally, we'll make an update to one of the micro-frontend apps and see how it can be deployed to production independently of the other micro-frontend apps.


Overview of the Demo App

Demo app - end result

Demo app - end result

Before we discuss the step-by-step instructions, let's get a quick overview of what makes up the demo app. This app is composed of four sub-apps:

  1. A container app that serves as the main page container and coordinates the mounting and unmounting of the micro-frontend apps
  2. A micro-frontend navbar app that's always present on the page
  3. A micro-frontend "page 1" app that only shows when active
  4. A micro-frontend "page 2" app that also only shows when active

These four apps all live in separate repos, available on GitHub, which I've linked to above.

The end result is fairly simple in terms of the user interface, but, to be clear, the user interface isn't the point here. If you're following along on your own machine, by the end of this article you too will have all the underlying infrastructure necessary to get started with your own micro-frontend app!

Alright, grab your scuba gear, because it's time to dive in!


Creating the Container App

To generate the apps for this demo, we're going to use a command-line interface (CLI) tool called create-single-spa. The version of create-single-spa at the time of writing is 1.10.0, and the version of single-spa installed via the CLI is 4.4.2.

We'll follow these steps to create the container app (also sometimes called the root config):

mkdir single-spa-demo

cd single-spa-demo

mkdir single-spa-demo-root-config

cd single-spa-demo-root-config

npx create-single-spa
Enter fullscreen mode Exit fullscreen mode

We'll then follow the CLI prompts:

  1. Select "single spa root config"
  2. Select "yarn" or "npm" (I chose "yarn")
  3. Enter an organization name (I used "thawkin3," but it can be whatever you want)

Great! Now, if you check out the single-spa-demo-root-config directory, you should see a skeleton root config app. We'll customize this in a bit, but first let's also use the CLI tool to create our other three micro-frontend apps.


Creating the Micro-Frontend Apps

To generate our first micro-frontend app, the navbar, we'll follow these steps:

cd ..

mkdir single-spa-demo-nav

cd single-spa-demo-nav

npx create-single-spa
Enter fullscreen mode Exit fullscreen mode

We'll then follow the CLI prompts:

  1. Select "single-spa application / parcel"
  2. Select "react"
  3. Select "yarn" or "npm" (I chose "yarn")
  4. Enter an organization name, the same one you used when creating the root config app ("thawkin3" in my case)
  5. Enter a project name (I used "single-spa-demo-nav")

Now that we've created the navbar app, we can follow these same steps to create our two-page apps. But, we'll replace each place we see "single-spa-demo-nav" with "single-spa-demo-page-1" the first time through and then with "single-spa-demo-page-2" the second time through.

At this point we've generated all four apps that we need: one container app and three micro-frontend apps. Now it's time to hook our apps together.


Registering the Micro-Frontend Apps with the Container App

As stated before, one of the container app's primary responsibilities is to coordinate when each app is "active" or not. In other words, it handles when each app should be shown or hidden. To help the container app understand when each app should be shown, we provide it with what are called "activity functions." Each app has an activity function that simply returns a boolean, true or false, for whether or not the app is currently active.

Inside the single-spa-demo-root-config directory, in the activity-functions.js file, we'll write the following activity functions for our three micro-frontend apps.

Next, we need to register our three micro-frontend apps with single-spa. To do that, we use the registerApplication function. This function accepts a minimum of three arguments: the app name, a method to load the app, and an activity function to determine when the app is active.

Inside the single-spa-demo-root-config directory, in the root-config.js file, we'll add the following code to register our apps:

Now that we've set up the activity functions and registered our apps, the last step before we can get this running locally is to update the local import map inside the index.ejs file in the same directory. We'll add the following code inside the head tag to specify where each app can be found when running locally:

Each app contains its own startup script, which means that each app will be running locally on its own development server during local development. As you can see, our navbar app is on port 9001, our page 1 app is on port 9002, and our page 2 app is on port 9003.

With those three steps taken care of, let's try out our app!


Test Run for Running Locally

To get our app running locally, we can follow these steps:

  1. Open four terminal tabs, one for each app
  2. For the root config, in the single-spa-demo-root-config directory: yarn start (runs on port 9000 by default)
  3. For the nav app, in the single-spa-demo-nav directory: yarn start --port 9001
  4. For the page 1 app, in the single-spa-demo-page-1 directory: yarn start --port 9002
  5. For the page 2 app, in the single-spa-demo-page-2 directory: yarn start --port 9003

Now, we'll navigate in the browser to http://localhost:9000 to view our app. We should see... some text! Super exciting.

Demo app - main page

Demo app - main page

On our main page, the navbar is showing because the navbar app is always active.

Now, let's navigate to http://localhost:9000/page1. As shown in our activity functions above, we've specified that the page 1 app should be active (shown) when the URL path begins with "page1." So, this activates the page 1 app, and we should see the text for both the navbar and the page 1 app now.

Demo app - page 1 route

Demo app - page 1 route

One more time, let's now navigate to http://localhost:9000/page2. As expected, this activates the page 2 app, so we should see the text for the navbar and the page 2 app now.

Demo app - page 2 route

Demo app - page 2 route

Making Minor Tweaks to the Apps

So far our app isn't very exciting to look at, but we do have a working micro-frontend setup running locally. If you aren't cheering in your seat right now, you should be!

Let's make some minor improvements to our apps so they look and behave a little nicer.

--

Specifying the Mount Containers

First, if you refresh your page over and over when viewing the app, you may notice that sometimes the apps load out of order, with the page app appearing above the navbar app. This is because we haven't actually specified where each app should be mounted. The apps are simply loaded by SystemJS, and then whichever app finishes loading fastest gets appended to the page first.

We can fix this by specifying a mount container for each app when we register them.

In our index.ejs file that we worked in previously, let's add some HTML to serve as the main content containers for the page:

Then, in our root-config.js file where we've registered our apps, let's provide a fourth argument to each function call that includes the DOM element where we'd like to mount each app:

Now, the apps will always be mounted to a specific and predictable location. Nice!


Styling the App

Next, let's style up our app a bit. Plain black text on a white background isn't very interesting to look at.

In the single-spa-demo-root-config directory, in the index.ejs file again, we can add some basic styles for the whole app by pasting the following CSS at the bottom of the head tag:

Next, we can style our navbar app by finding the single-spa-demo-nav directory, creating a root.component.css file, and adding the following CSS:

We can then update the root.component.js file in the same directory to import the CSS file and apply those classes and styles to our HTML. We'll also change the navbar content to actually contain two links so we can navigate around the app by clicking the links instead of entering a new URL in the browser's address bar.

We'll follow a similar process for the page 1 and page 2 apps as well. We'll create a root.component.css file for each app in their respective project directories and update the root.component.js files for both apps too.

For the page 1 app, the changes look like this:

And for the page 2 app, the changes look like this:


Adding React Router

The last small change we'll make is to add React Router to our app. Right now the two links we've placed in the navbar are just normal anchor tags, so navigating from page to page causes a page refresh. Our app will feel much smoother if the navigation is handled client-side with React Router.

To use React Router, we'll first need to install it. From the terminal, in the single-spa-demo-nav directory, we'll install React Router using yarn by entering yarn add react-router-dom. (Or if you're using npm, you can enter npm install react-router-dom.)

Then, in the single-spa-demo-nav directory in the root.component.js file, we'll replace our anchor tags with React Router's Link components like so:

Cool. That looks and works much better!

Demo app - styled and using React Router

Demo app - styled and using React Router

Getting Ready for Production

At this point we have everything we need to continue working on the app while running it locally. But how do we get it hosted somewhere publicly available? There are several possible approaches we can take using our tools of choice, but the main tasks are 1) to have somewhere we can upload our build artifacts, like a CDN, and 2) to automate this process of uploading artifacts each time we merge new code into the master branch.

For this article, we're going to use AWS S3 to store our assets, and we're going to use Travis CI to run a build job and an upload job as part of a continuous integration pipeline.

Let's get the S3 bucket set up first.


Setting up the AWS S3 Bucket

It should go without saying, but you'll need an AWS account if you're following along here. If we are the root user on our AWS account, we can create a new IAM user that has programmatic access only. This means we'll be given an access key ID and a secret access key from AWS when we create the new user. We'll want to store these in a safe place since we'll need them later. Finally, this user should be given permissions to work with S3 only, so that the level of access is limited if our keys were to fall into the wrong hands.

AWS has some great resources for best practices with access keys and managing access keys for IAM users that would be well worth checking out if you're unfamiliar with how to do this.

Next we need to create an S3 bucket. S3 stands for Simple Storage Service and is essentially a place to upload and store files hosted on Amazon's servers. A bucket is simply a directory. I've named my bucket "single-spa-demo," but you can name yours whatever you'd like. You can follow the AWS guides for how to create a new bucket for more info.

AWS S3 bucket

AWS S3 bucket

Once we have our bucket created, it's also important to make sure the bucket is public and that CORS (cross-origin resource sharing) is enabled for our bucket so that we can access and use our uploaded assets in our app. In the permissions for our bucket, we can add the following CORS configuration rules:

In the AWS console, it ends up looking like this after we hit Save:

CORS configuration

CORS configuration

Creating a Travis CI Job to Upload Artifacts to AWS S3

Now that we have somewhere to upload files, let's set up an automated process that will take care of uploading new JavaScript bundles each time we merge new code into the master branch for any of our repos.

To do this, we're going to use Travis CI. As mentioned earlier, each app lives in its own repo on GitHub, so we have four GitHub repos to work with. We can integrate Travis CI with each of our repos and set up continuous integration pipelines for each one.

To configure Travis CI for any given project, we create a .travis.yml file in the project's root directory. Let's create that file in the single-spa-demo-root-config directory and insert the following code:

This implementation is what I came up with after reviewing the Travis CI docs for AWS S3 uploads and a single-spa Travis CI example config.

Because we don't want our AWS secrets exposed in our GitHub repo, we can store those as environment variables. You can place environment variables and their secret values within the Travis CI web console for anything that you want to keep private, so that's where the .travis.yml file gets those values from.

Now, when we commit and push new code to the master branch, the Travis CI job will run, which will build the JavaScript bundle for the app and then upload those assets to S3. To verify, we can check out the AWS console to see our newly uploaded files:

Uploaded files as a result of a Travis CI job

Uploaded files as a result of a Travis CI job

Neat! So far so good. Now we need to implement the same Travis CI configuration for our other three micro-frontend apps, but swapping out the directory names in the .travis.yml file as needed. After following the same steps and merging our code, we now have four directories created in our S3 bucket, one for each repo.

Four directories within our S3 bucket

Four directories within our S3 bucket

Creating an Import Map for Production

Let's recap what we've done so far. We have four apps, all living in separate GitHub repos. Each repo is set up with Travis CI to run a job when code is merged into the master branch, and that job handles uploading the build artifacts into an S3 bucket. With all that in one place, there's still one thing missing: How do these new build artifacts get referenced in our container app? In other words, even though we're pushing up new JavaScript bundles for our micro-frontends with each new update, the new code isn't actually used in our container app yet!

If we think back to how we got our app running locally, we used an import map. This import map is simply JSON that tells the container app where each JavaScript bundle can be found. But, our import map from earlier was specifically used for running the app locally. Now we need to create an import map that will be used in the production environment.

If we look in the single-spa-demo-root-config directory, in the index.ejs file, we see this line:

Opening up that URL in the browser reveals an import map that looks like this:

That import map was the default one provided as an example when we used the CLI to generate our container app. What we need to do now is replace this example import map with an import map that actually references the bundles we're using.

So, using the original import map as a template, we can create a new file called importmap.json, place it outside of our repos and add JSON that looks like this:

You'll note that the first three imports are for shared dependencies: react, react-dom, and single-spa. That way we don't have four copies of React in our app causing bloat and longer download times. Next, we have imports for each of our four apps. The URL is simply the URL for each uploaded file in S3 (called an "object" in AWS terminology).

Now that we have this file created, we can manually upload it to our bucket in S3 through the AWS console. (This is a pretty important and interesting caveat when using single-spa: The import map doesn't actually live anywhere in source control or in any of the git repos. That way, the import map can be updated on the fly without requiring checked-in changes in a repo. We'll come back to this concept in a little bit.)

Import map manually uploaded to the S3 bucket

Import map manually uploaded to the S3 bucket

Finally, we can now reference this new file in our index.ejs file instead of referencing the original import map.


Creating a Production Server

We are getting closer to having something up and running in production! We're going to host this demo on Heroku, so in order to do that, we'll need to create a simple Node.js and Express server to serve our file.

First, in the single-spa-demo-root-config directory, we'll install express by running yarn add express (or npm install express). Next, we'll add a file called server.js that contains a small amount of code for starting up an express server and serving our main index.html file.

Finally, we'll update the NPM scripts in our package.json file to differentiate between running the server in development mode and running the server in production mode.


Deploying to Heroku

Now that we have a production server ready, let's get this thing deployed to Heroku! In order to do so, you'll need to have a Heroku account created, the Heroku CLI installed, and be logged in. Deploying to Heroku is as easy as 1-2-3:

  1. In the single-spa-demo-root-config directory: heroku create thawkin3-single-spa-demo (changing that last argument to a unique name to be used for your Heroku app)
  2. git push heroku master
  3. heroku open

And with that, we are up and running in production! Upon running the heroku open command, you should see your app open in your browser. Try navigating between pages using the nav links to see the different micro-frontend apps mount and unmount.

Demo app - up and running in production

Demo app - up and running in production

Making Updates

At this point, you may be asking yourself, "All that work for this? Why?" And you'd be right. Sort of. This is a lot of work, and we don't have much to show for it, at least not visually. But, we've laid the groundwork for whatever app improvements we'd like! The setup cost for any microservice or micro-frontend is often a lot higher than the setup cost for a monolith; it's not until later that you start to reap the rewards.

So let's start thinking about future modifications. Let's say that it's now five or ten years later, and your app has grown. A lot. And, in that time, a hot new framework has been released, and you're dying to re-write your entire app using that new framework. When working with a monolith, this would likely be a years-long effort and may be nearly impossible to accomplish. But, with micro-frontends, you could swap out technologies one piece of the app at a time, allowing you to slowly and smoothly transition to a new tech stack. Magic!

Or, you may have one piece of your app that changes frequently and another piece of your app that is rarely touched. While making updates to the volatile app, wouldn't it be nice if you could just leave the legacy code alone? With a monolith, it's possible that changes you make in one place of your app may affect other sections of your app. What if you modified some stylesheets that multiple sections of the monolith were using? Or what if you updated a dependency that was used in many different places? With a micro-frontend approach, you can leave those worries behind, refactoring and updating one app where needed while leaving legacy apps alone.

But, how do you make these kinds of updates? Or updates of any sort, really? Right now we have our production import map in our index.ejs file, but it's just pointing to the file we manually uploaded to our S3 bucket. If we wanted to release some new changes right now, we'd need to push new code for one of the micro-frontends, get a new build artifact, and then manually update the import map with a reference to the new JavaScript bundle.

Is there a way we could automate this? Yes!


Updating One of the Apps

Let's say we want to update our page 1 app to have different text showing. In order to automate the deployment of this change, we can update our CI pipeline to not only build an artifact and upload it to our S3 bucket, but to also update the import map to reference the new URL for the latest JavaScript bundle.

Let's start by updating our .travis.yml file like so:

The main changes here are adding a global environment variable, installing the AWS CLI, and adding an after_deploy script as part of the pipeline. This references an after_deploy.sh file that we need to create. The contents will be:

This file downloads the existing import map from S3, modifies it to reference the new build artifact, and then re-uploads the updated import map to S3. To handle the actual updating of the import map file's contents, we use a custom script that we'll add in a file called update-importmap.mjs.

Note that we need to make these changes for these three files in all of our GitHub repos so that each one is able to update the import map after creating a new build artifact. The file contents will be nearly identical for each repo, but we'll need to change the app names or URL paths to the appropriate values for each one.


A Side Note on the Import Map

Earlier I mentioned that the import map file we manually uploaded to S3 doesn't actually live anywhere in any of our GitHub repos or in any of our checked-in code. If you're like me, this probably seems really odd! Shouldn't everything be in source control?

The reason it's not in source control is so that our CI pipeline can handle updating the import map with each new micro-frontend app release. If the import map were in source control, making an update to one micro-frontend app would require changes in two repos: the micro-frontend app repo where the change is made, and the root config repo where the import map would be checked in. This sort of setup would invalidate one of micro-frontend architecture's main benefits, which is that each app can be deployed completely independent of the other apps. In order to achieve some level of source control on the import map, we can always use S3's versioning feature for our bucket.


Moment of Truth

With those modifications to our CI pipelines in place, it's time for the final moment of truth: Can we update one of our micro-frontend apps, deploy it independently, and then see those changes take effect in production without having to touch any of our other apps?

In the single-spa-demo-page-1 directory, in the root.component.js file, let's change the text from "Page 1 App" to "Page 1 App - UPDATED!" Next, let's commit that change and push and merge it to master. This will kick off the Travis CI pipeline to build the new page 1 app artifact and then update the import map to reference that new file URL.

If we then navigate in our browser to https://thawkin3-single-spa-demo.herokuapp.com/page1, we'll now see... drum roll please... our updated app!

Demo app - successfully updating one of the micro-frontend apps

Demo app - successfully updating one of the micro-frontend apps

Conclusion

I said it before, and I'll say it again: Micro-frontends are the future of frontend web development. The benefits are massive, including independent deployments, independent areas of ownership, faster build and test times, and the ability to mix and match various frameworks if needed. There are some drawbacks, such as the initial set up cost and the complexity of maintaining a distributed architecture, but I strongly believe the benefits outweigh the costs.

Single-spa makes micro-frontend architecture easy. Now you, too, can go break up the monolith!

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
Terabox Video Player