Implementation of Micro Frontends
Prev article: Theory of Micro Frontends Part - 2
Contents:
There are many ways to implement MicroFrontends. Any Javascript framework can share and consume MFE components and thus revolutionizing the web world by making it framework independent.
As shown below, Multiple components from multiple apps can be shared and used under one container application.
Implementation
Popular Frameworks:
Some of these toolsets are a driving force behind the development, release, and reuse of independent components:
Module Federation gives us a new method of sharing code between front-end applications.
Module Federation is a plugin that is added to Webpack. It adds the flexibility of exposing remote components.
It adds the option to share and integrate components in run-time. Loads the components through remoteEntry.js and wouldn't interfere with the other existing components and linked micro frontends.
Steps to Use
Since Module Federation is a part of webpack and webpack is the primary build tool used for all the existing web apps, Module Federation approach would give us more advantage.
Code Reference:
Following are the step for configuring, sharing and consuming React Components as Micro Frontend components between two different applications.
const webpack = require('webpack');
const { ModuleFederationPlugin } = webpack?.container;
const dependencies = require('./package.json')?.dependencies;
new ModuleFederationPlugin({
name: 'mfe-app-1',
filename: 'remoteEntry.js',
// ? Add the list of components which are to be exported as Microfrontends.
exposes: {
"./CounterAppOne": "./src/components/CounterAppOne",
},
shared: {
react: { singleton: true, eager: true, requiredVersion: deps.react },
"react-dom": {
singleton: true,
eager: true,
requiredVersion: deps["react-dom"],
},
...dependencies // ? Share all the dependencies only if needed.
},
})
new ModuleFederationPlugin({
name: "container",
remotes: {
remote-1: "mfe-app-1@https://localhost:3001/remoteEntry.js" // ? URL Pattern: "<app-name>@<app-url>/remoteEntry.js"
},
}),
///<reference types="react" />
type Props = {
activeTabId: string;
}; // ? Define the Props as per the component's definition.
declare module "remote-1/CounterAppOne" {
const CounterAppOne: React.ComponentType<Props>; // ? Add props as per the component (if needed).
export default CounterAppOne;
}
declare module "remote-1/CounterAppTwo" {
const CounterAppTwo: React.ComponentType; // ? Add props as per the component (if needed).
export default CounterAppTwo;
}
import React from "react";
const CounterAppOne = React.lazy(() => import("amzn/CounterAppOne"));
const CounterAppTwo = React.lazy(() => import("amzn/CounterAppTwo"));
<Suspense fallback={<Spinner size='large' />}>
<CounterAppOne activeTabId={'tab-id-1'} />
</Suspense>
<Suspense fallback={<Spinner size='large' />}>
<CounterAppTwo />
</Suspense>
领英推荐
import('./bootstrap');
export default {};
Best Practices to Deploy Micro Frontend
Can any small piece become a Micro Frontend?
No! Following a set of rules is always needed for better results.
Since Micro Frontend is a new implementation to the existing Monolithic SPA Web apps, following some Design Patterns and best practices while development would help in better implementation.
The following are some:
Styling and CSS scopes isolation – CSS modules to the rescue
Shared Redux store – Requires code sharing, so do not use it
Cross-app communication
Remote entries are down – Error Boundaries
Session management and authentication – Secure HTTP-only cookies
Caching – Unique imported remote entry URL
Metrics & Alarms - Deploying Logs and Alarms
Summary
Isolating use cases according to the features would definitely help teams work independently and efficiently. Scaling features, Managing development teams, Load balancing, and Handling change requests would become an easy part of the development process.
Microfrontends introduced a replica of Microservices into UI, is still a growing adaption in the JS World. Webapps built, controlled, and bootstrapped implementing different trending frameworks like Angular, React, Vue, Nuxt, Tweenmax, and others would eventually fall back to the complied JS code while being rendered to the browser. This gives a good scope to Microfrontnds as a part of the new age Webtech adaption and will eventually become the prime architectural requirement.
Code Repo Reference: https://github.com/PavanAditya/Microfrontend-ModuleFederation-React-Lerna
Follow Me at:
Portfolio: https://pavanaditya.com
GitHub: https://github.com/PavanAditya
Facebook: Pavan Aditya M S