Micro Frontend and implementation using single-spa-react framework and SystemJS — Part 02

A diagram showing 3 applications independently going from source control, through build, test and deployment to production


 This is the continuation of the article https://mytechlearns.blogspot.com/2022/06/micro-frontend-and-implementation-using.html, where we had developed the single-spa standalone application (Remote app in terms of micro frontend),


Now we are going to create the Host application which integrates the remote application we developed earlier.


create a new application using single spa as show below



this time we selected the "single-spa root config" as the type to generate.


based on the type of the application, single-spa framework is going to create required templates and files.

Lets check the application by running npm start

if everything goes well you will see the default welcome screen provided by single-spa framework.




Lets modify the config to integrate the screen we developed in the previous tutorial in the host application



add lines from 52 to 54 as shown above

Line 52: location of our remote application js file which contains single-spa lifecycle methods

Line 53 & 54: react and react-dom are the external mentioned in webpack config hence we need to load them externally here


if we don't add line 53 & 54 we get the below error:

Uncaught Error: application '@testorg/testorg-mf01' died in status LOADING_SOURCE_CODE: Unable to resolve bare specifier 'react' from http://localhost:7081/testorg-mf01.js 


2 different ways to load our remote module

    1. change at line 22 in microfrontend-layout.html file like shown below

     


    2. Register the remote application as show in below image in testorg-root-config.js file



registerApplication function takes 4 arguments

1. appName -- name of the application with in the host

2. loadingFunction -- the function to load the remote module which contains single-spa lifecycle methods.

3. activityFunction -- this function tells the application when it should be active, the function will get location object. here we mentioned it should be active when the url starts with /, generally we mention the routes when the app should be active.

Line 9: start function should be called to start the single-spa application.

if everything goes well you should see output as shown in the below screen based on the name you specified in line 4.



As shown in the network tab the module is loaded from remote app,

Any changes in the remote app will be reflected in the host app as shown below



The code discussed here is uploaded to Github in the below url

https://mytechlearns.blogspot.com/2022/06/micro-frontend-and-implementation-using_27.html

Comments

Popular posts from this blog

How shared libraries work in ModuleFederationPlugin.

Single SPA using single-spa-react and ModuleFederationPlugin

Micro Frontend and implementation using single-spa-react framework and SystemJS