<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Mattia D'Argenio</title>
    <description>The latest articles on DEV Community by Mattia D'Argenio (@darge98).</description>
    <link>https://dev.to/darge98</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F982581%2F16eb1a0e-6f8a-434c-b36b-78457958f4a6.jpeg</url>
      <title>DEV Community: Mattia D'Argenio</title>
      <link>https://dev.to/darge98</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/darge98"/>
    <language>en</language>
    <item>
      <title>Discover the power of microfrontends: A revolution in frontend development</title>
      <dc:creator>Mattia D'Argenio</dc:creator>
      <pubDate>Mon, 24 Feb 2025 21:52:08 +0000</pubDate>
      <link>https://dev.to/darge98/discover-the-power-of-microfrontends-a-revolution-in-frontend-development-33he</link>
      <guid>https://dev.to/darge98/discover-the-power-of-microfrontends-a-revolution-in-frontend-development-33he</guid>
      <description>&lt;h2&gt;
  
  
  Discover the power of microfrontends: A revolution in frontend development
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmhb00aqzrj7qhf78dr4o.jpeg" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2Fmhb00aqzrj7qhf78dr4o.jpeg" alt="Generated by Adobe Firefly" width="800" height="457"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;In recent years, a new architectural concept has emerged in front-end architecture: &lt;strong&gt;microfrontends&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;This approach is a radical change in the way we think about a front-end application. In recent years, we have been used to thinking of front-end applications as large, single monoliths; with this new concept, we will break these monoliths into &lt;strong&gt;smaller&lt;/strong&gt;, &lt;strong&gt;independently&lt;/strong&gt; &lt;strong&gt;deployable modules&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Each module — or microfrontend — encapsulates a specific feature of the application, allowing teams to work autonomously and deliver quickly without impacting other parts of the system.&lt;/p&gt;

&lt;p&gt;But… does this sound familiar?&lt;/p&gt;

&lt;p&gt;If the first thing that comes to mind is “&lt;em&gt;microservices&lt;/em&gt;”, you’ve got it. 🎉&lt;/p&gt;

&lt;p&gt;The essence of microfrontends is to apply the principles of microservices architecture. Just as microservices promote back-end modularity and autonomy, &lt;em&gt;microfrontends&lt;/em&gt; extend this functionality to the front-end, enabling teams to develop, test, and deploy components independently.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Ispect microfrontend principles&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;So, what are the principles of a microfrontend architecture?&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Contracts&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;Working with contracts is essential in the development of microfrontend architectures. Individual components should hide their implementation details, exposing only the minimum necessary to interact with each other.&lt;br&gt;
This practice allows teams to focus on the component without worrying about the work of other teams, and to be sure that the work of one team does not interfere with the work of another one.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Independency&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With monoliths, we have to deploy the entire system every time we make a small change. This has a high risk of failure and much longer deployment and rollback times. A &lt;em&gt;microfrontend&lt;/em&gt; architecture overcomes this problem by allowing individual components to be deployed independently.&lt;br&gt;
Teams can focus on their work without having to wait for dependencies on external features or worry about what other teams are doing.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Automation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;If you have not already done, I recommend reading this other article&lt;br&gt;
&lt;a href="https://medium.com/@mattia.darge/the-continuous-approach-in-software-lifecycle-33bc650286c9" rel="noopener noreferrer"&gt;&lt;strong&gt;The “Continuous” approach in software lifecycle&lt;/strong&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Here I explained how automation is critical to software development and delivery. Concepts such as &lt;em&gt;continuous integration&lt;/em&gt; and &lt;em&gt;continuous delivery/deployment&lt;/em&gt; are now fundamental in modern times.&lt;/p&gt;

&lt;p&gt;The ability to break the front-end into smaller, self-contained modules facilitates the automation of deploying these independent units across environments. This is also a key concept of microservices architecture. A strong culture of automation is essential to move faster and more reliably.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Isolate Failures&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In &lt;em&gt;microfrontend&lt;/em&gt; architectures, the user-interface is usually composed at runtime. This can lead to network errors or even 404s if resources are not available. Techniques for displaying different content or hiding a specific part of the application can be used to improve the user experience. &lt;br&gt;
With this approach, a failure in one component will not affect the entire application.&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;Where to start with microfrontend architectures ?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;In order to build a &lt;em&gt;microfrontend&lt;/em&gt; architecture, it is important to understand how to apply it. There are two application styles:&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Vertical Split&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;The vertical split provides the closest developer experience to a single-page application.&lt;/p&gt;

&lt;p&gt;In the vertical split, we have an application shell that takes care of mounting and dismounting the microfrontends. In this case the relationship is 1:1, the shell only loads one microfrontend at a time.&lt;/p&gt;

&lt;p&gt;Typically, in these cases, the application routing is split into two parts, a global one that is used to load the individual &lt;em&gt;microfrontend&lt;/em&gt;, and an internal one that is used to navigate between the internal pages of the &lt;em&gt;microfrontend&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;If it is necessary to communicate across different views, the use of query parameters is recommended for volatile data, and web storage/cookie is recommended for persistent data such as tokens or user local settings.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0i40wqm8i4kts4h8wfkg.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F0i40wqm8i4kts4h8wfkg.png" alt="Vertically-split microfrontend approach. The shell loads a single microfrontend determined by routing." width="785" height="666"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Horizontal Split&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;In the horizontal split, we always have one application shell that takes care of loading multiple &lt;em&gt;microfrontends&lt;/em&gt;. In this case, the relationship is 1:N.&lt;/p&gt;

&lt;p&gt;This structure is more difficult than the previous one because it is necessary to determine how &lt;em&gt;microfrontends&lt;/em&gt; should communicate with each other when they are in the same or different views.&lt;/p&gt;

&lt;p&gt;When using horizontal split, you must avoid sharing any state between &lt;em&gt;microfrontends&lt;/em&gt;. In horizontal split, the &lt;em&gt;microfrontend&lt;/em&gt; should communicate through event emitters, custom events or reactive streams using a publish/subscribe pattern. This helps to maintain the decoupling and independence of all of them.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F98uy7qlod77v9gfftbha.png" class="article-body-image-wrapper"&gt;&lt;img src="https://media2.dev.to/dynamic/image/width=800%2Cheight=%2Cfit=scale-down%2Cgravity=auto%2Cformat=auto/https%3A%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Farticles%2F98uy7qlod77v9gfftbha.png" alt="Horizontal-split microfrontend approach. The shell loads a multiple microfrontend determined by routing." width="800" height="674"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  &lt;strong&gt;How can I implement a microfrontend architecture?&lt;/strong&gt;
&lt;/h2&gt;

&lt;p&gt;To implement a &lt;em&gt;microfrontend&lt;/em&gt; architecture we have three different choices:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;Iframes&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;WebComponents&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;Module Federation&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Iframes&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/HTML/Element/iframe" rel="noopener noreferrer"&gt;Iframes&lt;/a&gt; may not be the first choice that comes to mind for a microfrontend solution, but they provide incredible isolation.&lt;/p&gt;

&lt;p&gt;An iframe is an inline frame used inside a webpage to load another HTML document in it. It can communicate with the host page using the &lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Window/postMessage" rel="noopener noreferrer"&gt;postMessage&lt;/a&gt; method. This allows the micro-frontend to notify any user interaction with the shell through events, and the application can then trigger other activities (share the event with other iframes, change part of the user interface in the host, etc.).&lt;/p&gt;

&lt;p&gt;Currently, the implementation of &lt;em&gt;microfrontends&lt;/em&gt; with iframes is mostly done on desktop applications. It is not recommended for web applications because iframes require high CPU consumption, which has a negative impact on performance.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;Web Components&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_components" rel="noopener noreferrer"&gt;Web Components&lt;/a&gt; are a set of web platform APIs that allow you to create custom encapsulated HTML tags for reuse. Web components allow us to encapsulate custom styles without fear of them leaking into the main application.&lt;/p&gt;

&lt;p&gt;Nowadays there are a lot of frameworks and libraries that help to build web components and they’re one of the most used solutions for building &lt;em&gt;microfrontends&lt;/em&gt;.&lt;/p&gt;

&lt;p&gt;Web components consist of three main technologies:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_custom_elements" rel="noopener noreferrer"&gt;Custom elements&lt;/a&gt;: They are an extension of HTML components. We can use them as containers of our &lt;em&gt;microfrontends&lt;/em&gt;.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;a href="https://developer.mozilla.org/en-US/docs/Web/API/Web_components/Using_shadow_DOM" rel="noopener noreferrer"&gt;Shadow DOM&lt;/a&gt;: A set of JavaScript APIs for attaching an encapsulated “shadow” DOM tree to an element, rendered separately from the main DOM. This allows us to style components without fear of collision with other parts of the document.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;HTML templates: The template of the component. It’s the basis of the custom element’s structure.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;&lt;strong&gt;Module Federation&lt;/strong&gt;&lt;/p&gt;

&lt;p&gt;With &lt;a href="https://webpack.js.org/" rel="noopener noreferrer"&gt;Webpack 5&lt;/a&gt;, a new feature has helped microfrontends proliferate: &lt;a href="https://webpack.js.org/concepts/module-federation/" rel="noopener noreferrer"&gt;Module Federation&lt;/a&gt;.&lt;br&gt;
Module Federation allows JavaScript code to be loaded — synchronously or asynchronously — at runtime.&lt;/p&gt;

&lt;p&gt;There are two components in Module Federation:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Host&lt;/em&gt;: It’s the container. It can contain one or more &lt;em&gt;microfrontends&lt;/em&gt; and is responsible for loading libraries.&lt;/p&gt;&lt;/li&gt;
&lt;li&gt;&lt;p&gt;&lt;em&gt;Remote&lt;/em&gt;: It represents the single microfrontend or library to be loaded into the host at runtime. The remote exposes one or more objects that can be used by the host.&lt;/p&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;One of the strengths of this feature is the ability to share external libraries between different remotes without worrying about runtime conflicts. In fact, you can specify which libraries to share, and Module Federation will take care of loading a single version for all &lt;em&gt;microfrontends&lt;/em&gt; that use the library, optimizing the bundle size of the entire application.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;(Stay tuned, there will be a practical example on module federation 🤖)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h2&gt;
  
  
  Microfrontends are the way?
&lt;/h2&gt;

&lt;p&gt;If you have reached this point and are wondering, “Are microfrontends the solution?” well… maybe the answer is *&lt;strong&gt;&lt;em&gt;NO&lt;/em&gt;&lt;/strong&gt;*.&lt;/p&gt;

&lt;p&gt;As with any architectural choice, each project must evaluate its use on a case-by-case basis; not all cases can be addressed with &lt;em&gt;microfrontends&lt;/em&gt;. (Just as not all use cases are suitable for microservices).&lt;/p&gt;

&lt;p&gt;This architecture provides a solution to the major limitations of monoliths. It allows you to break it down into smaller, independent components. It is more scalable. It allows faster development and deployment of individual components. It is technology agnostic and allows you to vertically structure teams around each functionality, from back-end to front-end, without external disruption.&lt;/p&gt;

&lt;p&gt;On the other hand, there are many challenges to overcome. It is a complex architecture that requires careful analysis to determine whether a vertical or horizontal split is more appropriate. It requires guidelines to be shared with teams to decide how components will communicate with each other, and it also requires an eye on bundle size and performance (the ability to use different frameworks could increase bundle size and traffic).&lt;/p&gt;

&lt;p&gt;So, do your analysis and always weigh up the pros and cons of each solution ✨.&lt;/p&gt;

&lt;p&gt;Thank you for reading this article!&lt;/p&gt;

&lt;p&gt;Any questions or suggestions? Feel free to write a comment.&lt;/p&gt;

</description>
    </item>
  </channel>
</rss>
