In the beginning there was chaos, now we have microfrontends – it’s a joke, but a perfect sentence to start this article with. If you even heard something about micro in the dev world, it was probably a microservice. Are microfrontends and microservices connected? Yes and no, but first let’s check what microfrontends actually are.
The definition of microfrontends
Previous applications were created as monoliths. To make it simple – it was one project hosted as one application and often developed in one technology. Take a look at this simplified schema, how the monolith frontend looks like in a microservice application:
We have separate services connected to one frontend application. The most popular solution which can be problematic in huge projects – difficult to maintain, test and develop. So what in return?
Take a look at what are the most characteristic parts of microfrontends:
- Each part can be developed independently
- Each part can be deployed independently
- Each part can be tested independently
- Each part can be hosted independently
- All elements are composed into one whole
Let’s rewrite the above schema to the microfrontends world:
Interesting, isn’t it? Take a deeper look at this schema. As you can see, each microservice has a dedicated frontend part in a different technology. What is frontend wrapper? There needs to be a part which stitches all frontends together e.g. page skeleton, layout, navigation, etc. We can think of this wrapper as an entry point for customers. From a technical point of view this part is the orchestrator for our microfrontends applications. In the best approach it only knows what should be rendered, when and where, it can be a communication layer between other elements and suppliers of core scripts and styles for all applications.
How are the elements delivered to the orchestrator?
There are a few approaches but the most popular ones are custom elements and iframes. If we think about custom elements, we think about script which provides the code of microfrontend application rendered on page by custom html tag. There are a lot of advantages of this approach, e.g. styling or using shared elements are easier. However, scripts could be really large which may cause some performance problems.
The second approach are iframes with each application having its own hosting. Orchestrator renders multiple iframes on the page. In such situation each iframe is a separate thread in the browser so it could have performance advantages. However, styling is more difficult. In an iframe we can not create e.g. sticky header because it’s completely isolated, especially when it’s hosted on a different domain. Page navigation and routing could be a problematic part, too. Iframe approach is perfect when our application needs something like widgets which should be an isolated part of our page. Perfect example – situation when you need an advertisement placeholder on your page.
Microfrontends are not only superlatives
The entry threshold is high and it’s difficult to create a perfect orchestrator. The hardest part is routing, especially when multiple microfrontends apps on the same landing page need to call an url. It’s easier when one app is on one subpage, because the whole responsibility of navigation can be passed to the server. Routing is the hardest part of microfrontends application.
Another difficult area is page styling. We have to create a common way of working for all micro parts. Each part needs to be integrated and styled on the wrapper’s skeleton and be compatible with different browsers, devices and resolutions. Keep responsive web design on microfrontends world needs to be done in the whole application, wrapper and individual applications which can be quite a challenge.
Is microfrontend a solution?
No, it’s an approach not suitable for each application. Decision whether to use it or not should be done before starting a project. You can find projects based on microservices in which microfrontends are useless because frontend is simple or you can isolate context in monolith using libraries and approaches named modular monolith. But if you create complex applications in big teams, microfrontends approach is perfect. Take a look at this comparison of monolith and micrfrontends approaches:
|Teams working in one big project||Autonomous teams|
|Mostly one technology||Technology dedicated per team or project|
|Incremental updates could be difficult||Incremental updates are easily|
|Hosted in one place||Each part could be hosted in different place|
|Testing as one project||Testing separates elements|
|One big code piece||Smaller code pieces dedicated per functionality|
|Perfect for small applications||Perfect for big applications|
|Easy routing||In some situation routing is difficult, need to be done on server side|
|Easier styling||Styling could be more difficult|
|Big project is difficult to maintain and develop||Big project is easier to maintain and develop|
Have you ever seen microfrontend application?
For sure, e.g. Facebook or Allegro. Simple parts of microfrontend could be done in any application actually. Imagine that you are going to create an info service but you need your own advertisement area – weather forecast or custom image taken from your gallery. It’s a perfect world for microfrontend because each part can be a separate app integrated into one. Are you going to create a big shop? Why not use microfrontends – let’s split it to smaller, integrated parts like home page, category view, basket, order etc.
You’re interested in the topic or have some questions? Don’t hesitate to contact our team!