XenonStack Recommends

Micro Frontend Architecture and Best Practices

Acknowledging Data Management
          Best Practices with DataOps


XenonStack White Arrow Image

Introduction to Micro Frontend

Micro Frontend is a Microservice Testing approach to front-end web development. The current trend is to build a powerful and feature-rich web application that resides on top of a Microservice architecture. Over time the Micro Frontend Architecture becomes a part of the application, often it is developed by a separate team, grows, and gets more difficult to maintain, this type of application is called Frontend Monolith. To solve this problem, the concept of the Micro Frontend came into the picture.
Micro Frontend is the right choice unless we examine the complexity of our frontend ecosystem. Source: How to integrate dot js Frameworks to Micro Frontend?

A team is cross-functional and develops end-to-end features, from the user interface to the database. Micro Frontend is a more friendly and less bulky one. This type of Micro Frontend Architecture split the entire application by business domain across the entire stack. This enables front-end teams the same level of flexibility, testability, and velocity that backend teams get from Microservices.

How Micro Frontend Works?

Micro Frontend best practices, strategies, and recipes to build a modern web application with multiple teams using different JavaScript frameworks. Micro Frontend Architecture The main Concept behind Micro Frontend Architecture is as follows -

Be Technology Independent

Each team should choose and upgrade the stack without coordinating with other teams. Custom elements help to hide implementation details while providing a neutral interface to others.

Isolate Team Code

Never share a runtime, even if teams use the same framework. Build an independent application self-contained. Do not rely on shared state or global variables.

Create Team Prefixes

Use naming conventions where isolation not possible yet. Namespace CSS, Local Storage, Events, and Cookies to avoid collisions and clarify ownership.

Favor Native Browser Features over Custom APIs

Instead of building a global PubSub system, use browser events for communication. If there is a need to build a cross-team API, try to keep it as simple as possible.

Build a Resilient Web design

The features should be useful, even if JavaScript unable to execute. To improve perceived performance, use universal rendering and progressive enhancement.

What are the Best Practices of Micro-Frontend?

Different practices to implement Micro Frontend Architecture-
  1. The Single SPA meta-framework combines multiple frameworks on the same page without refreshing the page, such a React, Vue, Angular 1, Angular 2, etc.
  2. Multiple single page application lives at different URLs. For shared functionality applications, use NPM or Bower components.
  3. Isolating Micro apps into Iframes using Windows. Post Message APIs and libraries to coordinate. IFrames share APIs exposed by their parent window.
  4. Different modules to communicate over a shared events bus. Each module is working on its own framework, as long as it handles incoming and outgoing events.
Component Libraries depending on the main app's stack, the different components and app sections are developed as libraries and “required” into the main app. Hence, the main app is a composition of different components.
D3.js stands for Data-Driven Documents is a front-end visualization library in javascript for creating interactive and dynamic web-based data visualizations. Source: Introduction to D3.js Library

How to Adopt Micro Frontend Architecture?

This is how you can adopt Micro Frontend architecture and implement Microservices testing with Web components:

Integration in the Browser

Web components provide a way to create fragments of Front-End imported into Web applications. Those fragments can be packaged into Microservices together with the back-end. Services built, completed with both logic and visual representation packed together. Using this approach, Front-End applications reduced to routing make decisions involving which set of components displayed and orchestration of events between different web components.

Web Components

Web components allow the creation of reusable components imported into Web applications. These are like widgets imported into any Web page. These are currently working in browsers such as Chrome, Opera, and Firefox. If the browser does not support web components natively, compatibility is accomplished using JavaScript Polyfills. Web components consist of 4 main elements used separately or altogether -
  1. Custom Elements
  2. Shadow DOM
  3. HTML Imports
  4. HTML Templates

Custom Elements

Create custom HTML tags and elements with Custom Elements. Each element has its own CSS styles and scripts. Create own tags, apply CSS styles and add behaviors through scripts. The only standard required to put hyphen to avoid conflicts with new HTML elements. For example, create a list of check-outs, both with custom elements and custom tags, results in Web components and element lifecycle callbacks. These lifecycle callbacks allow defining behaviors specific to the component development. Lifecycle callbacks with custom elements are as follows -
  1. CreatedCallback - It defines behavior that occurs when the component got registration.
  2. AttachedCallback - It defines behavior that occurs when the component is inserted into the DOM.
  3. DetachedCallback - It defines behavior that occurs when the element is not present in the DOM.
  4. AttributeChangedCallback - It defines behavior that occurs when an attribute is added, changed, or removed.
The best Example of Custom Elements

class CheckoutBasket extends HTMLElement {

constructer (){...} is created

connectedCallback (){...}  attached to DOM

attributeChangedCallback (attr , oldVal , newVal)  someone changed an attribute

disconnecteCallback () {...}  removed from DOM, cleanup events that have been registered
Custom elements by default - stencil, svelte, SkateJS, AngularElements, hyperHTML Elements etc.

Shadow DOM

Shadow DOM the DOM is the API that combines HTML, CSS, and JavaScript inside a Web Component separated from the main document's DOM when these are inside a component. This separation is similar to the one user while building API services, and the consumer of an API service does not know about its internals, the only thing that matters for a consumer is API requests. Such a service does not have access to the outside world except to request APIs of other services. Similar features were presents in web components. Their internal behavior is not accessed outside, except when allowed by design, nor does it affect the DOM document they reside in. The main method of communication between web components is by firing events.

HTML Imports

For web components, HTML imports are the packaging mechanism. HTML imports tell DOM the location of a Web Component. In the context of Microservices, import remote location of service contains the component to use. HTML imports is a method to reuse and include HTML documents via other HTML documents. Predefined components as HTML imports, where each of them includes own styles and scripts, decide on the top level that HTML import presents in DOM at the moment, and the imported document handles the rest of things.
  • Shell is a top-level wrapper that consists of a container for component and component picker. It should include controllers or views which allow user manipulating components.
  • The container is the actual root place where the HTML code of nested applications should be injected. For all nested apps, it should have a single entry point.
  • Component Picker allows managing of nested applications active at the moment.

Cloud migration involves moving essential data, applications, and business parts from an organization’s on-premises computers to cloud computing environments. Source: Key Elements for a Successful Cloud Migration

HTML Templates

The HTML template element holds client-side content not rendered when a page is loaded. Let's try to understand the Implementation of Micro Frontend Architecture with the following React Js app example - It is good when web applications are developed independently to change to the element done without being blocked by others and breaking others. That's why in this example new react app needed to build, run, and deploy separately, treating others that have to communicate with it as services. Example: The example below creates a header for a web page. In this example, React.js used. The modern generation is using it nowadays. Let's use create-react-app for fast bootstrap -

npm install -g create-react-app

create-react-app head

cd head/

npm start
Now let’s add server-side rendering real quick. Later it will help when joining apps for SEO and performance reasons. It is easier to attach this at the beginning. micro fronted Then, create a file called server.js at the root of the project that will start an express server, and server-side render react –

const path = require('path');

const fs = require('fs');

const express = require('express');

const React = require('react');

const App = require('./transpiled/App.js').default;

const { renderToString } = require('react-dom/server');

const server = express();

server.get('/', (req, res) => {

const htmlPath = path.resolve(__dirname, 'build', 'index.html');

 fs.readFile(htmlPath, 'utf8', (err, html) => {

const rootElem = '
const renderedApp = renderToString(React.createElement(App, null));  res.send(html.replace( rootElem, rootElem + renderedApp) );  server.use(express.static(‘build’)); const port = process.env.PORT || 8080; server.listen(port, () => {  console.log(`App listening on port ${port}`);  }); This script takes root to react element (App), renders it to a string, and pushes it into the HTML before serving it to the user. React will later mount on top of that already rendered component. But, that runs on NodeJS, and Node JS doesn't understand JSX or other newer syntaxes like import, so babel used to transpile it before running the server -

npm install --dev babel-cli babel-preset-es2015
Add two tasks to the scripts section in package.json to run that -

"transpile": "NODE_ENV=production babel src --out-dir transpiled --presets es2015,react-app",

"start:prod": "NODE_ENV=production node server.js"
That's it. Now run the header with -

npm run build

npm run transpile

npm run start:prod

Why Micro Frontend Matters?

In the Modern Era, with new web apps, the front end is becoming bigger and bigger, and the back end is getting less important. Most of the code is the Micro Frontend Architecture. And the Monolith approach doesn't work for a larger web application. A monolithic approach to a large front-end application becomes unwieldy. There needs to be a tool for breaking it up into smaller modules that act independently. The solution to the problem is the Micro frontend. Micro Frontend code is written only in pure JavaScript and any of the javascript frameworks used or migrated from one framework to another.
Effectively representing data is one of the critical tasks in data visualization, in JavaScript, various libraries are open source to perform tasks easily. Source: Best Data Visualization JavaScript Libraries

What are the Benefits of Micro Frontend Architecture?

Every day a new JavaScript technology is invented, and these are increasing faster than a speeding bullet. That sometimes, it can be frustrating as every JavaScript technology has its own pros and cons. And while selecting a particular technology, everyone considers the maximum benefit and minimum risk. Micro frontend best practices help to use different techniques for different services. Here are some benefits of Micro Frontend -
  • Support code and style isolation, an individual development team can choose their own technology. The development and deployment speed is very fast.
  • It helps in Continuous Deployment.
  • The testing becomes very simple, and for every small change, you don’t have to go and touch the entire application.
  • Front-end renovation - Improving new things becomes easier.
  • High Resilience and Better Maintenance.
  • Support code and style isolation.

Concluding Micro Frontend Architecture

Make sure the core and the integration are as simple as possible in Micro Frontend Architecture. One of the critical problems is to standardize the UI/UX principles. A universal solution is to use a style guide, for example - Material Design, Bootstrap, among others. Communication with the team is the key to ensure everything is running smoothly, create some standards and rules to minimize conflicts with the difference of teams working on a product. All these Micro Frontend Architecture best practices help to solve one problem, scalability. An application that tends to grow significantly introduces numerous issues and conflicts, splitting the code into teams and applying the right logistics to deliver good quality, technology trending, and fast solutions to the world.
Build Stream Analytics Systems
Want to scale frontend development to make your teams work together? Contact our Microservices Experts

Related blogs and Articles

Overview of The New Component-Driven Responsive Design

Enterprise Digital Platform

Overview of The New Component-Driven Responsive Design

Introduction Responsive Design Today, when we talked about the term: "responsive design," the first thought that came to our mind was using media queries to change layout when resizing a design from desktop size to tablet size to mobile size. But shortly, this insight of responsive design could also be considered outdated using tables for page layout.