This is a simple React project intended to be used within a microfrontend architecture; it comes with Webpack 5 and TypeScript, as well as React Router to provide navigation and to switch between different microfrontend applications.
Both this project and the single app are based on using the window
object with a defined custom property, named microapps
.
These have to be considered as following:
single app
exposes its React mount and unmount hooks on awindow.microapp.tmp
property;container app
handleswindow.microapp.tmp
(if available) and uses it to sideload multiplesingle app
.
The application itself, as it's just intended as a boilerplate microfrontend container, is quite simple. The app.tsx
bootstraps the application and creates a React Router; it will read microfrontends applications (identified by MicroApp
TypeScript type) and create a base route for each one inside app-routes.tsx
file;
A MicroApp
comes as following:
type MicroApp = {
name: string;
host: string;
basepath?: string;
filename?: string;
mount: (container: string, history: History, basepath?: string) => void;
unmount: (container: string) => void;
};
described as following:
name
is an identifier name, which will be used for the<script/>
tag ID and the microfrontend ReactDOM root element ID;host
is the HTTP url in which the.js
file is located;basepath
is the main React router basepath for this microfrontend;filename
is the name of the.js
file. It needs to be with the.js
extension. Default:build.js
;mount
(defaults tomountApplication
) is the exposed function name to be called when creating the microfrontend, which will take care of mounting react application (passingbasepath
will ensure its routes will be correct);unmount
(defaults tounmountApplication
) is the exposed function name to be called when unmounting the microfrontend.
The useManifest
hook will create both <div>
and <script>
tags which ID will be named after the MicroApp.name
property; the hook will search for window.microapp.tmp
, which will be copied in a specific property named as the microapp.name
property. The hook will be now set and the <MicroappContainer>
will be now ready to serve a specific microfrontend application.
Following here the process flow:
app.tsx
will read the manifest file (manifest.json
by default, but can be everything) and loadMicroApp
objects;app-routes.tsx
will create a single<Route>
for eachMicroApp
, rendering a<MicroappContainer>
with the microfrontend specifics;<MicroappContainer>
will use theuseMicroapp
hook;-
useMicroapp
will create the microfrontend ReactDOM root element and<script>
;
-
- will create a
window.microapps[microapp.name]
by copyingwindow.microapps.tmp
(and unsetting it);
- will create a
<MicroappContainer>
will finally call themountApplication
(described before) and the microfrontend will be up and running.