Micro frontends is one of several architectural approaches that help developers to break down large web applications into smaller, independent parts.
Each micro frontend application represents a self-contained feature or module that can be developed, tested, and deployed separately from the other micro frontend applications.
This article provides a practical guide for organizations wishing to adopt micro frontends. For further reading on micro frontends, read our previous post “Microfrontends: Microservices for the Frontend”.
Slicing the Monolithic ApplicationThe main challenge with adopting the micro frontends architecture isn’t technical. Instead, the discussion often involves the product team and relates to your vision of the application.
Luca Mezzalira defines two ways of slicing monolithic applications:
To demonstrate the complexity behind this decision, let’s envision a simple e-commerce site that we split into two domains:
Splitting the site vertically makes perfect sense here, as the two domains are relatively self-contained.
Let’s imagine that after a few years the CEO wants to expand the company by allowing other sellers to offer their products through the site. This can be easily achieved by creating a new vertical slice for a partner’s portal domain.
A more complex change would be adding a new line of products to the store, which would require some changes from the current lineup. For this we can split the storefront into two new slices, i.e. books and perishable goods sections.
We can duplicate some code to ensure that the domains remain isolated but, as they grow, the cost of maintaining duplicate code can quickly outgrow its benefits.
To handle this, we can adopt horizontal slicing by defining domains based on user flows, i.e. the discovery process and purchase.
Each domain will include components that can exist on one or more pages and allow the related teams to own those user journeys.
In summary, vertical slices offer:
On the other hand, horizontal slices offer:
While there are strong technical benefits from using vertical slices, the decision needs to consider product needs and future growth.
Implementing vertical slices without clear independent domains becomes very hard to maintain, while the cost of horizontal slicing can easily exceed its short to medium value. This means that you have to choose which you want to implement carefully.
Incremental Upgrades?One of the main selling points for micro services and micro frontends architecture is the easier upgrade path you gain by managing multiple small applications that can be changed independently.
While this is a significant benefit, there are two additional considerations to discuss for micro frontends:
As a result, some companies chose to implement micro frontends using a single library such as React. While this might sound counterintuitive, it simplifies the adoption process, reduces bundle sizes, and can remain relevant for many years.
Micro Frontends ImplementationsiframesIframes are by far the simplest implementation for micro frontends, which ticks all the required boxes:
Developers who wish to use an existing solution should take advantage of Luigi, an open-source library created by SAP, as it handles some of the more complex use cases such as cross iframe communication, 3rd party cookies, etc.
It’s worth noting that iframes work best for vertical slicing and that, despite the benefits, it has not gained wide traction in the community. As such, this option should be considered for smaller projects that need to get running quickly.
Module FederationModule federation is a relatively new feature in bundles, such as Webpack, that allows separate JavaScript applications to share and consume code at runtime.
It offers two exciting possibilities for developers:
A great Github resource demonstrates how to use Webpack module federation with different setups that provide a good starting point.
Module federation works best with single library projects that rely on vertical slicing. Horizontal slicing is possible but requires a more complex setup.
Single SPASingle spa is a popular library for managing more complex micro frontend applications.
It supports a sophisticated router called the Layout Engine that allows developers to easily combine multiple micro frontend applications that use different libraries within the same layout. This provides much better flexibility than standard vertical slices while avoiding much of the complexity of horizontal slicing. At the same time, it supports true stand-alone components called Parcels but advises limiting their use as they are rather complicated to manage. Single spa is a great solution for most complex applications that do not rely heavily on horizontal slicing.
Bit.devBit.dev is a tool for composing component-driven applications where a single component can be as small as a button or as big as an entire page. Bit.dev aims to fulfill the promise made by Web Components by allowing developers to freely combine components from different libraries into a single application while avoiding many of the hurdles that exist with Web Components. Unlike previous solutions, Bit requires a dedicated server where components are managed, organized into scopes, and shared across projects. It’s an exciting solution that is gaining popularity in companies like Tesla and Ebay but may not be ready for large enterprise-grade projects.
ConclusionMicro frontends can provide a valuable tool for organizations looking to scale their frontend applications. Adopting the right tool for the job, as with anything else, will have a lasting effect. Organizations should allocate time for technical investigation and align the technical domains with the product vision. Following the KISS design process is recommended when investigating technical solutions. Avoid selecting more complex solutions such as single spa and bit.dev that require deeper integration with the development process unless it’s clear that your organization will use its unique capabilities.
The post Starting With Microfrontends appeared first on Semaphore.