Version: 5.x

Layout Engine

Github repository

The single-spa-layout npm package is an optional add-on to single-spa. The layout engine provides a routing API that controls your top level routes, applications, and dom elements. Using single-spa-layout makes it easier to accomplish the following:

  • DOM placement and ordering of applications.
  • Loading UIs when applications are downloaded.
  • Default routes for Not Found / 404 pages.
  • Transitions between routes (implementation pending).
  • Server side rendering of single-spa applications
  • Error pages

In the browser, the layout engine performs two major tasks:

  1. Generate single-spa registration config from an HTML Element and/or JSON object.
  2. Listen to routing events to ensure that all DOM elements are laid out correctly before the single-spa applications are mounted.

On the server, the layout engine performs two tasks:

  1. Construct a server layout object from an HTML template.
  2. Send an HTML document (HTTP response headers and body) to the browser, based on the server layout object and current route.

single-spa-layout is 3.2kb gzipped (9kb ungzipped).

You only need to install the layout engine into your root config (not in any other application).

  1. npm install --save single-spa-layout
  2. # or
  3. yarn add single-spa-layout

single-spa-layout works in all browsers supported by single-spa, including IE11. On the server, all NodeJS versions that support ESM are supported.

You must use single-spa@>=5.4.0 in order for the layout engine to work. Additionally, you may not provide custom domElementGetter functions for any of your single-spa applications, as those override the configuration within single-spa-layout.

In your root html file, add a <template> element to the head. It should have a <single-spa-router> element that contains <route> elements, <application> elements, and any other dom elements:

  1. <html>
  2. <head>
  3. <template id="single-spa-layout">
  4. <single-spa-router>
  5. <nav class="topnav">
  6. <application name="@organization/nav"></application>
  7. </nav>
  8. <div class="main-content">
  9. <route path="settings">
  10. <application name="@organization/settings"></application>
  11. </route>
  12. <route path="clients">
  13. <application name="@organization/clients"></application>
  14. </route>
  15. </div>
  16. <footer>
  17. <application name="@organization/footer"></application>
  18. </footer>
  19. </single-spa-router>
  20. </template>
  21. </head>
  22. </html>

Then inside of your root-config’s JavaScript code, add the following:

  1. import { registerApplication, start } from 'single-spa';
  2. import {
  3. constructApplications,
  4. constructRoutes,
  5. constructLayoutEngine,
  6. } from 'single-spa-layout';
  7. const routes = constructRoutes(document.querySelector('#single-spa-layout'));
  8. const applications = constructApplications({
  9. routes,
  10. loadApp({ name }) {
  11. return System.import(name);
  12. },
  13. });
  14. const layoutEngine = constructLayoutEngine({ routes, applications });
  15. applications.forEach(registerApplication);
  16. start();

Overview - 图1Edit this page