How To Implement Micro-Frontend Architecture With Angular - by Bhargav Bachina - Bachina Labs - Medium
How To Implement Micro-Frontend Architecture With Angular - by Bhargav Bachina - Bachina Labs - Medium
Modern web applications are becoming big and more complex and
sometimes managed by different teams. Your application might have
features developed by different teams and you want to release only certain
features into production before delivering the entire application. How do
you manage different teams, different release timelines if you have one
repo?
Most of these complex apps live on the client-side which makes it harder to
maintain. There are some other issues as well with this monolithic big fat
application. In this post, I am going to discuss advantages, disadvantages,
implementation and a lot of other stuff.
Introduction
Advantages of Mirco-Frontends
Features of Mirco-Frontends
Micro-Frontend Frameworks
Summary
Conclusion
Introduction
Micro-frontends are small applications mostly divided by subdomain or
functionality working together to deliver a larger application. Before diving
into Mirco-frontends, we will understand what are micro frontends and
why we are talking about those.
But, this is not the case all the time. Sometimes your frontend app might be
a small part of another big application or your app consists a lot of sections
and features which are developed by different teams or your app is being
released into production feature by feature with developed by separate
teams. If you are in one of these situations then you need to think about
Micro Frontends. Let’s look at it in the below diagram.
Micro Frontends Architecture
As you see in the above diagram, we have 6 frontend apps working together
to deliver the large application. The communication between these apps
can be done with an event bus, window object, or publish/subscribe
methods. Each app can be implemented by a separate team and any
framework. Each app can talk to their backends or endpoints individually.
There is a bootstrap/launch app that loads all the apps and mounts and
unmounts in the DOM depending on the user interaction or routing.
. . .
coming soon!
. . .
Advantages of Mirco-Frontends
Here are the advantages of this architecture.
. . .
Features of Mirco-Frontends
Each frontend represents a specific feature or subdomain of the entire
application
. . .
By Feature
This is the most common method since we can easily divide the features of
the app. For example, if there are three features for the app Dashboard,
Profile and views we can make each feature as a separate app and mounts
and unmounts in the DOM with the help of Launch.js. This Launch.js can
be a separate app or just a simple javascript app.
By Feature
By Section
Some of the apps have so much functionality with each section, for
example, coinbase, Gmail, etc. We can implement each section as a new app
in that scenario.
By Section
By Page
Some app’s functionalities are divided by page. Each page has some
functionality that can be independent. We can divide these apps by page We
have four pages in the below diagram. we can make four apps out of this.
By Page
By Domain
Splitting app based on the domain is also one of the most common
approaches.
By Domain
. . .
Iframes
Through NGINX
Angular Libraries
Monorepos
Customized Orchestrator
. . .
Micro-Frontend Frameworks
Micro frontends have been implemented for at least two years and it is still a
green field. Have you ever wonder are there any frameworks or libraries to
implement these and makes our job easier. The answer is yes and there are
a couple of libraries or frameworks
single-spa
frint.js
single-spa
single-spa is a javascript framework for front-end microservices and can be
implemented with all three popular frameworks/libraries such as Angular,
React and Vue.js. It can lazy load the applications based on the need and
You can check out their website for more information.
frint.js
frint.js is a Modular JavaScript framework for building Scalable & Reactive
applications. It doesn’t support Angular yet but it supports React. If you are
building a reactive application from scratch and you are just starting, this is
the framework for you. You can check out their website for more
information.
. . .
We will split this app by section as shown in the below diagram. We are
going to implement 4 apps in total: HeaderApp, DashboardApp, FooterApp,
and root application.
Micro Frontends
Here are the four repositories for four apps. You can clone these run
separately on your machine.
Here is the micro-root app index HTML file. We are importing all the three
apps on line 10 and registering the apps with the appropriate name and
location. Since we are loading all the apps on page load, we are not defining
any specific context paths.
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <meta http-equiv="Content-Security-Policy" content="default-src * data: blob: 'unsafe-inline
5 <meta charset="utf-8">
6 <meta http-equiv="X-UA-Compatible" content="IE=edge">
7 <title>Your application</title>
8 <meta name="viewport" content="width=device-width, initial-scale=1">
9 <meta name="importmap-type" content="systemjs-importmap">
10 <script type="systemjs-importmap">
11 {
12 "imports": {
13 "footer": "http://localhost:4201/main.js",
14 "dashboard": "http://localhost:4202/main.js",
15 "header": "http://localhost:4300/main.js",
16 "single-spa": "https://cdnjs.cloudflare.com/ajax/libs/single-spa/4.3.5/system/single-s
17 }
18 }
19 </script>
20 <link rel="preload" href="https://cdnjs.cloudflare.com/ajax/libs/single-spa/4.3.5/system/sing
21 <script src='https://unpkg.com/[email protected]/minified.js'></script>
22 <script src="https://unpkg.com/zone.js"></script>
23 <script src="https://unpkg.com/[email protected]/dist/import-map-overrides.js"></sc
24 <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/4.0.0/system.min.js"></script>
25 <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/4.0.0/extras/amd.min.js"></scri
26 <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/4.0.0/extras/named-exports.js">
27 <script src="https://cdnjs.cloudflare.com/ajax/libs/systemjs/4.0.0/extras/named-register.min
28 <style>
28 <style>
29 </style>
30 </head>
31 <body>
32 <script>
33 System.import('single-spa').then(function (singleSpa) {
34 singleSpa.registerApplication(
35 'header',
36 function () {
37 return System.import('header');
38 },
39 function (location) {
40 return true;
41 }
42 )
43
44 singleSpa.registerApplication(
45 'dashboard',
46 function () {
47 return System.import('dashboard');
48 },
49 function (location) {
50 // return location.pathname.startsWith('/app2');
51 return true;
52 }
53 )
54
55 singleSpa.registerApplication(
56 'footer',
57 function () {
58 return System.import('footer');
59 },
60 function (location) {
61 // return location.pathname.startsWith('/app1');
62 return true;
63 }
64 );
65
66 singleSpa.start();
67 })
68 </script>
69 <import-map-overrides-full></import-map-overrides-full>
70 </body>
71 </html>
You can give /header location path so that the header is loaded when
browser URL navigates to /header. Let’s test it out.
1 <script>
2 System.import('single-spa').then(function (singleSpa) {
3 singleSpa.registerApplication(
4 'header',
5 function () {
6 return System.import('header');
7 },
8 function (location) {
9 return location.pathname.startsWith('/header');
10 // return true;
11 }
}
12 )
with /header
. . .
Summary
Micro frontends are the implementation of microservices for the front
end
Modern web apps are becoming more and more complex and most of
the code lives on the client-side.
There are so many advantages to using this approach. At the same time,
don't use it for every app especially small apps.
Each app can be developed independently, Each app can use a different
stack, Each can be owned by a single team or multiple teams and finally,
they don’t share logic.
It’s tempting to use a different framework for each app but not
recommended. Remember, this approach should make your life easier.
You don’t need frameworks to implement but, using one of these makes
your transition from monolith to micro frontends easier.
. . .
Conclusion
I know Microfrontends is such a trendy thing but you should not use it for
every app. Do not use it if your app is small and don’t complicate it. This
approach should make our whole process seamless not over complicated.
So use your best judgment before using this approach.
By signing up, you will create a Medium account if you don’t already have one. Review our Privacy Policy for more
information about our privacy practices.