Harnessing the Power of Single-spa for Micro Frontend Architecture

Harnessing the Power of Single-spa for Micro Frontend Architecture


In the modern world of web development, micro frontends have emerged as a popular architectural pattern to build scalable and maintainable applications. One of the critical components for implementing micro frontends is the use of a library or framework that can handle the seamless integration of multiple applications into a single cohesive unit. Single-spa is one such library that offers developers the ability to build and manage micro frontend applications with ease. In this article, we will dive deep into the world of Single-spa and learn how to leverage its power to create a robust micro frontend architecture.

What is Single-spa?

Single-spa is a JavaScript library that enables developers to build and manage multiple applications, or «micro frontends,» within a single web page. It allows you to develop, deploy, and maintain independent applications that can be combined into a larger, cohesive user interface. Single-spa works with various frontend frameworks, including React, Angular, Vue, and more, making it a versatile solution for integrating multiple applications built with different technologies.

Key Features of Single-spa

Some of the key features of Single-spa include:

  1. Framework Agnostic: Single-spa works with multiple frontend frameworks, including React, Angular, Vue, and others. This allows you to use the best tools for each application while still being able to combine them into a single interface.
  2. Lazy Loading: Single-spa enables lazy loading of applications, ensuring that only the required code is loaded for a particular route or section of the application. This results in faster load times and improved performance.
  3. Independent Deployment: Each application in a Single-spa setup can be independently developed, tested, and deployed without affecting other applications. This helps improve development speed and allows for easier maintenance.
  4. Shared Dependencies: Single-spa allows you to share dependencies between applications, reducing the overall size of your bundles and improving performance.

Getting Started with Single-spa

To get started with Single-spa, you can follow these steps:

1. Install Single-spa using npm or yarn:

  1. npm install single-spa


  1. yarn add single-spa

2. Create a root configuration file that will be responsible for registering and initializing your micro frontend applications. This file should import Single-spa and any necessary utilities for your chosen framework.

For example, if you are using React:

  1. import * as singleSpa from 'single-spa';
  2. import * as singleSpaReact from 'single-spa-react';
  4. // Register your applications here

3. Register your applications using the registerApplication function. You will need to provide the name of the application, a function that loads the application, and a function that determines when the application should be active.

  1. singleSpa.registerApplication({
  2. name: 'my-app',
  3. app: () => import('./path/to/my-app'),
  4. activeWhen: '/my-app',
  5. });

4. Start Single-spa using the start function:

  1. singleSpa.start();

5. Update your applications to export a lifecycle object with the necessary functions for mounting, unmounting, and updating your application. These functions will be called by Single-spa when the application is active.

  1. export const bootstrap = async () => {
  2. // Initialization logic goes here
  3. };
  5. export const mount = async () => {
  6. // Code to mount your application goes here
  7. };
  9. export const unmount = async () => {
  10. // Code to unmount your application goes here
  11. };

6. Configure your build system to ensure that each application is built as a separate bundle. This may require updating your Webpack, Rollup, or other build tool configurations.

7. Deploy your applications and root configuration file to your chosen hosting platform. Ensure that the root configuration file is loaded before any of your applications.

Implementing Navigation in Single-spa

One essential aspect of a micro frontend architecture is the ability to navigate between applications seamlessly. With Single-spa, you can implement navigation by using the `activeWhen` property when registering your applications. This property accepts a function that determines when the application should be active based on the current URL.

For example, you can use the `pathToRegexp` utility to create a function that matches specific routes:

  1. import { pathToRegexp } from 'path-to-regexp';
  3. const activeWhen = (pathname) => {
  4. const routeRegExp = pathToRegexp('/my-app');
  5. return routeRegExp.test(pathname);
  6. };
  8. singleSpa.registerApplication({
  9. name: 'my-app',
  10. app: () => import('./path/to/my-app'),
  11. activeWhen,
  12. });

You can also use the built-in `singleSpa.pathToActiveWhen` helper for common use cases:

  1. import * as singleSpa from 'single-spa';
  3. singleSpa.registerApplication({
  4. name: 'my-app',
  5. app: () => import('./path/to/my-app'),
  6. activeWhen: singleSpa.pathToActiveWhen('/my-app'),
  7. });

Handling CSS in Single-spa

When working with Single-spa, it's important to isolate the CSS for each application to prevent styles from leaking between applications. One approach to achieve this is by using CSS Modules or a similar scoped CSS solution.

Another option is to use a CSS-in-JS library like styled-components or Emotion. These libraries generate unique class names for each component, ensuring that styles are scoped to the specific application.


Single-spa is a powerful and flexible library that simplifies the process of building and managing micro frontend applications. By using Single-spa, developers can create modular, scalable, and maintainable frontend architectures that can grow and evolve with the needs of their projects. With its framework-agnostic nature, lazy loading capabilities, and support for independent deployments, Single-spa is an excellent choice for implementing a micro frontend architecture in your next project.

We use cookies to improve your browsing experience. By continuing to use this website, you consent to our use of cookies. Learn More