Micro-frontends with Module Federation: Building scalable and modular web applications

Jan 24, 2024 by Luis Cameroon

 

  

In brief

  • Delve into micro-frontends combined with Module Federation for creating modular web apps with Luis Cameroon, Zoreza Global Technical Director 
  • Understand the terminologies of Module Federation such as Host, Bidirectional Hosts, and Remote 
  • Discover the benefits of this architecture, including scalability, independent deployment and enhanced performance 
  • Learn step-by-step implementation using React and Vite, and gain insights into challenges and best practices 

  

  

Introduction

 

As web applications grow in complexity, maintaining a monolithic codebase becomes challenging. Micro-frontends offer a solution by breaking down an application into smaller, self-contained modules. Integrating micro-frontends with Module Federation allows developers to build scalable, modular and efficient web applications. In this article, we will explore the concept of micro-frontends with Module Federation and demonstrate its implementation using React and Vite. 

  

What are micro-frontends?

 

Micro-frontends are an architectural approach that breaks down a complex web application into smaller, independently deployable and scalable modules. Each module represents a self-contained feature or functionality, developed by different teams or using different technologies. This approach enables teams to work autonomously, promotes code reusability and simplifies maintenance. 

   

Introducing Module Federation

 

Module Federation is a powerful feature provided by Webpack, Vite/Rollup, and Snowpack, allowing the dynamic loading and sharing of JavaScript modules between applications. By combining micro-frontends with Module Federation, we can create a distributed architecture where modules can be loaded on-demand, sharing resources and dependencies as required. 

Module Federation introduces several terminologies that are essential to understand how modules interact and communicate with each other. Let’s delve into the key terminologies: Host, Bidirectional Hosts and Remote. 

 

Module Federation terminology 

1. Host: In Module Federation, the Host is the application that consumes and integrates remote modules exposed by other applications. It acts as the main container for the entire application and is responsible for orchestrating the integration of remote modules. The Host application can dynamically load and render remote modules, allowing seamless integration and composition of functionality from multiple sources. 

2. Bidirectional Hosts: Bidirectional Hosts refer to Host applications that can both consume and expose modules. In these scenarios, the Host application functions as both the consumer and provider of modules. This means that the Host application can consume remote modules from other applications while also exposing its own modules for consumption by other Host or Remote applications. Bidirectional Hosts enable a more cohesive and collaborative approach, allowing modules to be shared across different applications in a bidirectional manner. 

3. Remote: A Remote module is a standalone application or module that exposes certain components, functions, or assets for consumption by other applications. It is developed and deployed independently from the Host application. Remote modules can be hosted on different domains or served from separate servers. The Host application can consume and render the exposed components or utilize the functionality provided by the Remote module.

The interaction between these terminologies can be summarized as follows: The Host application loads the Remote modules at runtime, using Module Federation’s dynamic loading capabilities. The Host can consume the components or utilize the functionality provided by the Remote module, integrating it seamlessly into the Host application. This allows for the composition of different modules from multiple sources, enabling a modular and scalable frontend architecture. 

It is important to note that the concept of Host, Remote, and Bidirectional Hosts is specific to Module Federation and may vary in different implementations or frameworks. However, the underlying principle remains the same, where the Host consumes and integrates modules from Remote sources to create a cohesive application experience. 

 

Benefits of micro-frontends with Module Federation

 

There are many benefits to using micro-frontends with Module Federation, including: 

  • Scalability: Micro-frontends allow teams to scale their applications effortlessly by adding or removing modules as needed. With Module Federation, modules can be easily composed and reused across different applications, leading to a more scalable and modular architecture 
  • Independent development and deployment: Micro-frontends enable teams to work independently on their modules, reducing dependencies and enabling faster development cycles. With Module Federation, teams can deploy their modules separately, allowing for independent releases and reducing the risk of affecting other parts of the application 
  • Improved team autonomy: Micro-frontends promote team autonomy, as each team can focus on a specific module without being dependent on other teams. Module Federation further enhances this autonomy by enabling teams to choose their own technology stack, making it easier to adopt new technologies or frameworks within their modules 
  • Enhanced performance: Module Federation enables lazy-loading of modules, which improves initial load times by loading only the necessary modules on demand. Additionally, shared dependencies are loaded once and cached, reducing duplicate code and improving overall performance 
  • Code reusability: With Module Federation, modules can expose components, functions, or assets that can be consumed by other modules. This promotes code reusability, reduces duplication, and encourages the creation of shared libraries or UI components that can be utilized across multiple micro-frontends

  

Implementation with React and Vite

 

To demonstrate the implementation of micro-frontends with Module Federation, we will use React, a popular JavaScript library for building user interfaces, and Vite, a fast development server and build tool. 

Step 1: Set up the main application 

Create a new React application using Vite: 

 

 

Install the necessary dependencies: 

 

 

Step 2: Configure Module Federation 

In the main application’s  vite.config.js  file, import the necessary dependencies: 

 

Add the Module Federation plugin configuration to the  plugins  array: 

 

 

Step 3: Create micro-frontends 

  • Create separate React applications for each micro-frontend: 
    npx create-vite module1 — template react 
    and 
    npx create-vite module2 — template react 
  • Repeat "Step 2" for each micro-frontend’s  vite.config.js  file, with different names and URLs 

Step 4: Load micro-frontends in the main application 

In the main application’s React component, import the necessary dependencies: 

 

 

Import and render the micro-frontend modules: 

 

Step 5: Build and run the applications 

  • Build the micro-frontends:  npm run build  in each micro-frontend’s directory 
  • Build the main application:  npm run build  in the main application’s directory 
  • Serve the applications:  npm run serve  in each application’s directory 

 

Micro-frontend architecture 

 

Implementation challenges

  

There are some challenges to using micro-frontends with Module Federation: 

  • Cohesion: It can be difficult to maintain the cohesion of a micro-frontend application. This is because each micro-frontend is a separate unit, and it can be easy for them to become decoupled from each other 
  • Communication: It is sometimes challenging to communicate between different micro-frontends. This is because each micro-frontend is a separate unit, and they may not have a shared understanding of the application’s state 
  • Deployment: It can be complex to deploy micro-frontends. This is because each micro-frontend is a separate unit and they may need to be deployed to different servers 

 

Best practices for micro-frontends

 

Here are some best practices for micro-frontends with Module Federation: 

  • Keep micro-frontends small and focused: Micro-frontends should be small and focused so that they are easy to understand and maintain 
  • Use a common API: Micro-frontends should use a common API so that they can communicate with each other 
  • Use a centralized configuration: Micro-frontends should use a centralized configuration so that they can share settings and data 

Micro-frontends should be version controlled so changes can be tracked and managed. 

 

Conclusion

 

Micro-frontends with Module Federation offer a scalable and modular approach to building web applications. By breaking down applications into independent modules, developers can work autonomously and leverage the power of dynamic module loading.  

At Zoreza Global's Smashing Ideas, we're passionate about empowering businesses to stay at the forefront of technology and innovation. If you're interested in harnessing the benefits of micro-frontends and Module Federation for your web applications or have questions about how this approach can support your business's unique needs, our expert Development team is here to help. 

Don't miss out on the opportunity to create scalable, modular, and efficient web applications. Get in touch with us today, visit our website and let's explore how we can tailor these cutting-edge solutions to elevate your digital presence.  

 

 

 

Luis Cameroon , Technical Director

Luis Cameroon author linkedin

Technical Director

Luis has been designing and developing high-performing, scalable and innovative web-based applications for clients in the financial, energy, and media industries for more than 15 years. He’s fascinated by human-computer interaction and is a keen proponent of user experience design.