What is single-spa?
single-spa is a JavaScript framework for front-end microservices. It allows you to build and manage multiple microfrontends that can coexist and interoperate within a single web application. This enables teams to work independently on different parts of a web application, using different frameworks if necessary.
What are single-spa's main functionalities?
Registering Applications
This feature allows you to register multiple applications with single-spa. Each application can be loaded dynamically and activated based on the URL path.
const singleSpa = require('single-spa');
singleSpa.registerApplication({
name: 'app1',
app: () => System.import('app1'),
activeWhen: ['/app1'],
customProps: {}
});
singleSpa.start();
Mounting and Unmounting Applications
This feature allows you to manually mount and unmount applications, giving you fine-grained control over which applications are active at any given time.
const singleSpa = require('single-spa');
singleSpa.registerApplication({
name: 'app2',
app: () => System.import('app2'),
activeWhen: ['/app2'],
customProps: {}
});
singleSpa.start();
// Manually mount an application
singleSpa.mountRootParcel('app2', { domElement: document.getElementById('app2-container') });
// Manually unmount an application
singleSpa.unmountRootParcel('app2');
Custom Events
This feature allows you to dispatch and listen for custom events, enabling communication between different microfrontends.
const singleSpa = require('single-spa');
singleSpa.registerApplication({
name: 'app3',
app: () => System.import('app3'),
activeWhen: ['/app3'],
customProps: {}
});
singleSpa.start();
// Dispatch a custom event
window.dispatchEvent(new CustomEvent('custom-event', { detail: { someData: 'data' } }));
// Listen for a custom event
window.addEventListener('custom-event', (event) => {
console.log(event.detail.someData);
});
Other packages similar to single-spa
qiankun
qiankun is a micro-frontend framework based on single-spa. It provides additional features like sandboxing and lifecycle hooks, making it easier to manage microfrontends in complex applications.
mooa
mooa is a micro-frontend framework inspired by single-spa. It focuses on Angular applications and provides a simpler API for integrating multiple Angular microfrontends.
icestark
icestark is a micro-frontend framework developed by Alibaba. It offers a set of tools and conventions for building and managing microfrontends, with a focus on React and Vue applications.
single-spa
Multiple applications all lazily loaded and mounted/unmounted in the same single page application (SPA). The apps can be deployed independently to your web server of choice, lazy-loaded onto the page independently, and nested.
Ideology
The hope here is that "one SPA to rule them all" will help scale teams and organizations that have complex apps -- this is done by making it easier to split code and then deploy it independently. To explain why single SPA is advantageous, consider the following disadvantages of a multiple SPA approach:
- Implementation details are usually reflected in the URL (different subdomains for different SPAs, or different apps own different prefixes in the window.location.pathname)
- Each SPA tends to become a monolith, since it's easier to add to an existing SPA than it is to figure out how to deploy a new SPA.
- Transitioning between the apps is a full page unload-then-load, which generally provides a worse user experience. It is possible to mitigate this one with server-side routing + rendering (popularly done with React), but single-spa offers an alternative approach.
- Shared functionality can only be accomplished via shared libraries, instead of a service oriented architecture ("Update it and hope library consumers upgrade" vs "Deploy it once and now everyone has it")
How to use it
So right now it's still really alpha and so the best thing to do is look at examples directory. Especially the following files:
Also note that it requires that the root app that loads all other apps to be written with JSPM. The goal is to move away from that towards the whatwg/loader standard or maybe towards no loader standard at all (which would offload all of the loading work to the user).
How to get the examples running
git clone git@github.com:joeldenning/single-spa.git
cd single-spa/examples
npm run setup
python -m SimpleHTTPServer 8000
open localhost:8000