Frontend Development 14 min read

Understanding single-spa: Core Principles, Lifecycle Management, and Integration with qiankun

This article examines the single-spa micro‑frontend framework, explains its core mechanisms such as registerApplication, reroute, and lifecycle handling, demonstrates how it powers the qiankun solution, and provides practical code examples for integrating various front‑end stacks.

TAL Education Technology
TAL Education Technology
TAL Education Technology
Understanding single-spa: Core Principles, Lifecycle Management, and Integration with qiankun

The article explores the principles of single-spa, a JavaScript micro‑frontend framework, and shows how it underpins the qiankun solution.

Background: A unified operation platform requires shared login state, technology‑stack independence, independent deployment, and seamless navigation between modules.

Problems to solve: shared login, stack‑agnostic, independent maintenance, and user‑experience parity with single‑app routing.

Relationship: qiankun is built on top of single-spa; understanding single-spa is prerequisite for mastering qiankun.

What is single-spa: It aggregates multiple SPA applications, allowing different frameworks to coexist without page reload, supporting independent deployment and lazy loading.

Core API – registerApplication: registers a child app with four parameters (appNameOrConfig, appOrLoadApp, activeWhen, customProps). Example code: registerApplication('myApp', async () => { /* load script */ return window['singleCapacity']; }, () => location.pathname.startsWith('/vue'), { /* props */ })

Lifecycle handling: single-spa expects each child to expose async bootstrap, mount, unmount (and optionally unload) functions, which are invoked by the framework during routing changes.

Routing mechanism: start() triggers reroute(), which evaluates activeWhen rules, loads required apps via loadApp, and calls lifecycle functions (bootstrap, mount, unmount) based on URL changes, hashchange, popstate, and patched pushState/replaceState.

Code flow: getAppChanges determines which apps should be active; loadApps registers micro‑tasks to fetch scripts; performAppChanges orchestrates unmounting, loading, and mounting; reasonableTime finally calls the specific lifecycle hook.

Integration with frameworks: single-spa-react, single-spa-vue, single-spa-angular, etc., provide wrappers that map framework‑specific mount/unmount to the generic lifecycle. Example Vue wrapper code: export function mount(props) { return vueLifecycles.mount(props); }

Conclusion: While single-spa requires manual lifecycle wiring, qiankun abstracts this complexity, offering out‑of‑the‑box isolation, CSS/JS sandboxing, and easier configuration.

JavaScriptmicro-frontendfrontend architectureqiankunRoutinglifecyclesingle-spa
TAL Education Technology
Written by

TAL Education Technology

TAL Education is a technology-driven education company committed to the mission of 'making education better through love and technology'. The TAL technology team has always been dedicated to educational technology research and innovation. This is the external platform of the TAL technology team, sharing weekly curated technical articles and recruitment information.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.