Mastering Microfrontends with React and Module Federation

  • Microfrontends architecture parallels microservices for frontend applications.
  • Vertical and horizontal splits aid in managing frontend monoliths.
  • Module Federation with Vite allows seamless integration of dependencies.
  • CSS strategies help maintain consistent styling across microfrontends.
  • Effective state management strategies reduce coupling in microfrontends.

Microfrontends represent an architectural style that extends the principles of microservices to frontend applications. Just as microservices break down a monolithic server architecture into smaller, manageable services, microfrontends divide a frontend monolith into separate, cohesive units. This approach allows different teams to work on distinct parts of a frontend application, enhancing scalability and maintainability.

A frontend monolith can be sliced in various ways, primarily through vertical and horizontal splits. A vertical split assigns a microfrontend to each page or set of pages, allowing teams to focus on specific sections like homepages or checkout processes. Alternatively, horizontal splitting divides the UI components, with different teams managing separate elements like the menu or dashboard. This flexibility in the architecture provides options to tailor the setup according to project needs.

While the microfrontend architecture is beneficial for large companies, smaller applications can also leverage it, especially when transitioning from legacy systems. By gradually replacing parts of an old application, teams can modernize their codebase without a complete overhaul. However, challenges exist, including ensuring a consistent look and feel across different microfrontends. This can be likened to renovating parts of a house, where a modern kitchen might clash with outdated bathrooms.

The composition of microfrontends can occur on the server side, at the edge with CDNs, or on the client side. Module Federation, particularly with Vite, plays a crucial role in client-side composition. This method allows sharing of dependencies between microfrontends, ensuring that common libraries are not duplicated, which optimizes performance. The Vite Module Federation configuration closely resembles the Webpack approach, making it easier for developers familiar with Webpack to adopt.

Maintaining a consistent style across microfrontends is a significant concern. One approach is using Vanilla CSS, where stylesheets are injected into the page in a specific order to manage conflicts. However, this can lead to clashes if not handled carefully. Tools like PostCSS Prefix Wrap for Webpack or PostCSS Prefixer for Vite help prevent these issues by adding prefixes to CSS classes. CSS Modules offer another solution, scoping styles locally to avoid conflicts, while CSS-in-JS allows styles to be colocated with components, improving manageability.

State sharing in microfrontends is another complex challenge. While custom events and broadcast channels offer basic solutions, more sophisticated approaches like shared state managers or custom PubSub implementations can be employed. The key is to share minimal information to maintain the independence of each microfrontend, thus preserving the core advantages of decoupled deployment and development.

Overall, microfrontends, when implemented effectively, can transform the way frontend applications are developed and maintained. By leveraging tools like Module Federation and adopting best practices for styling and state management, developers can build scalable and maintainable frontend architectures that support the dynamic needs of modern applications.

08 Oct, 2024

Check out more articles and videos

We constantly think of articles and videos that might spark Git people interest / skill us up or help building a stellar career

Vite: Rethinking Frontend Tooling
JSNation Live 2021JSNation Live 2021
31 min
Vite: Rethinking Frontend Tooling
Top Content
Vite is a next-generation build tool that leverages native ES modules for improved performance. It eliminates the need for bundling and improves hot module replacement. Vite provides an opinionated default configuration while still allowing advanced customization through plugins. It is framework agnostic and can be used for React and other applications. Vite is being adopted by Next.js and Create React App, and integration with Nuxt 3 offers significant speed improvements.
Scaling Up with Remix and Micro Frontends
Remix Conf Europe 2022Remix Conf Europe 2022
23 min
Scaling Up with Remix and Micro Frontends
Top Content
This talk discusses the usage of Microfrontends in Remix and introduces the Tiny Frontend library. Kazoo, a used car buying platform, follows a domain-driven design approach and encountered issues with granular slicing. Tiny Frontend aims to solve the slicing problem and promotes type safety and compatibility of shared dependencies. The speaker demonstrates how Tiny Frontend works with server-side rendering and how Remix can consume and update components without redeploying the app. The talk also explores the usage of micro frontends and the future support for Webpack Module Federation in Remix.
React Compiler - Understanding Idiomatic React (React Forget)
React Advanced 2023React Advanced 2023
33 min
React Compiler - Understanding Idiomatic React (React Forget)
Top Content
Watch video: React Compiler - Understanding Idiomatic React (React Forget)
Joe Savona
Mofei Zhang
2 authors
The Talk discusses React Forget, a compiler built at Meta that aims to optimize client-side React development. It explores the use of memoization to improve performance and the vision of Forget to automatically determine dependencies at build time. Forget is named with an F-word pun and has the potential to optimize server builds and enable dead code elimination. The team plans to make Forget open-source and is focused on ensuring its quality before release.
Welcome to Nuxt 3
Vue.js London Live 2021Vue.js London Live 2021
29 min
Welcome to Nuxt 3
Top Content
Nux3 has made significant improvements in performance, output optimization, and serverless support. Nuxt Bridge brings the Nitro engine for enhanced performance and easier transition between Nuxt 2 and Nuxt Read. Nuxt 3 supports Webpack 5, Bytes, and Vue 3. NextLab has developed brand new websites using Docus technology. Nuxt.js is recommended for building apps faster and simpler, and Nuxt 2 should be used before migrating to Nuxt 3 for stability. DOCUS is a new project that combines Nuxt with additional features like content modules and an admin panel.
Monolith to Micro-Frontends
React Advanced 2022React Advanced 2022
22 min
Monolith to Micro-Frontends
Top Content
Microfrontends are considered as a solution to the problems of exponential growth, code duplication, and unclear ownership in older applications. Transitioning from a monolith to microfrontends involves decoupling the system and exploring options like a modular monolith. Microfrontends enable independent deployments and runtime composition, but there is a discussion about the alternative of keeping an integrated application composed at runtime. Choosing a composition model and a router are crucial decisions in the technical plan. The Strangler pattern and the reverse Strangler pattern are used to gradually replace parts of the monolith with the new application.
Turbopack. Why? How? When? and the Vision...
React Day Berlin 2022React Day Berlin 2022
32 min
Turbopack. Why? How? When? and the Vision...
The Talk discusses TurboPack, a successor to Webpack, aiming to create a framework-independent, flexible, and extensible tool for the open-source community. It addresses performance challenges by integrating SWC into Next.js. The challenges with Next.js and Webpack include orchestration issues, backward compatibility constraints, and cache invalidation problems. TurboEngine and TurboPack provide constant performance in incremental builds, leveraging Rust's predictable performance and parallelism. The Talk also covers topics like dependency tracking, task graphs, cache invalidation, lazy asset graphs, and the integration of TurboPack with Next.js. The future plans involve reconfiguring Webpack and TurboEngine, moving computations to the cloud, providing insights into builds, and facilitating migration and integration with JavaScript projects.

Workshops on related topic

Using CodeMirror to Build a JavaScript Editor with Linting and AutoComplete
React Day Berlin 2022React Day Berlin 2022
86 min
Using CodeMirror to Build a JavaScript Editor with Linting and AutoComplete
Top Content
WorkshopFree
Hussien Khayoon
Kahvi Patel
2 authors
Using a library might seem easy at first glance, but how do you choose the right library? How do you upgrade an existing one? And how do you wade through the documentation to find what you want?
In this workshop, we’ll discuss all these finer points while going through a general example of building a code editor using CodeMirror in React. All while sharing some of the nuances our team learned about using this library and some problems we encountered.
Micro Frontends with Module Federation and React
JSNation Live 2021JSNation Live 2021
113 min
Micro Frontends with Module Federation and React
Workshop
Alex Lobera
Alex Lobera
Did you ever work in a monolithic Next.js app? I did and scaling a large React app so that many teams can work simultaneously is not easy. With micro frontends you can break up a frontend monolith into smaller pieces so that each team can build and deploy independently. In this workshop you'll learn how to build large React apps that scale using micro frontends.
Micro-Frontends with Module Federation and Angular
JSNation Live 2021JSNation Live 2021
113 min
Micro-Frontends with Module Federation and Angular
Workshop
Manfred Steyer
Manfred Steyer
Ever more companies are choosing Micro-Frontends. However, they are anything but easy to implement. Fortunately, Module Federation introduced with webpack 5 has initiated a crucial change of direction.
In this interactive workshop, you will learn from Manfred Steyer -- Angular GDE and Trusted Collaborator in the Angular team -- how to plan and implement Micro-Frontend architectures with Angular and the brand new webpack Module Federation. We talk about sharing libraries and advanced concepts like dealing with version mismatches, dynamic Module Federation, and integration into monorepos.
After the individual exercises, you will have a case study you can use as a template for your projects. This workshop helps you evaluate the individual options for your projects.
Prerequisites:You should have some experience with Angular.