Strapi
Published in

Strapi

A Quick Introduction to Micro Frontend using Module Federation

Learn what Micro Frontend is, its advantages, and how it works by sharing its state within two applications using Module Federation.

What Is Micro Frontend?

Benefits of Micro Frontends

How Do We Split Apps?

Module Federation

What Is Strapi?

Prerequisites

Project Scope

mkdir microfrontends
npx create-strapi-app@latest my-project --quickstart
#or
yarn create strapi-app my-project --quickstart
Strapi Admin Signup
Creating Our Collection Type
Populate the collection

Setting up our frontend Applications

npx create-mf-app
npx create
Terminal
npm install
npm install axios
npm start
CSS
npx create-mf-app
Terminal npx create
npm install
npm start
CSS Pop up
import React from "react";
import "./style.css";
function Header() {
return (
<div>
<nav>
<ul>
<li>
<a href="/">Home</a>
</li>
<li>
<a href="/">About</a>
</li>
<li>
<a href="/">Info</a>
</li>
<li>
<a href="/">Contact</a>
</li>
</ul>
</nav>
</div>
);
}
export default Header;
/* Fix navigation to create a sticky nav bar */
nav {
position: fixed;
margin-top: -20px;
width: 100%;
background-color: #1a1a1a;
padding: 0 25px;
}
nav ul {
margin: 0;
padding: 0;
}
nav li {
display: inline-block;
width: 70px;
color: #fff;
text-align: center;
padding: 10px 0;
}
nav li:active {
background-color: #333333;
}
nav li:hover {
background-color: #8f8f8f;
}
nav a {
text-decoration: none;
color: #fff;
}
nav button {
display: none;
}
import React from "react";
import ReactDOM from "react-dom";
import Header from "./Header";
import "./index.css";
const App = () => (
<div className="container">
<div><Header/></div>
</div>
);
ReactDOM.render(<App />, document.getElementById("app"));
Header sample
"./Header": "./src/Header.jsx",
webpack
remote: "  remote@http://localhost:8081/remoteEntry.js "
import React from "react";
import ReactDOM from "react-dom";
import Header from "remote/Header";
const App = () => (
<div className="container">
<div><Header/></div>
</div>
);
ReactDOM.render(<App />, document.getElementById("app"));
import axios from "axios";
const url = "http://localhost:1337/api/displays";
export const readDisplay = () => axios.get(url);
import React from "react";
import ReactDOM from "react-dom";
import { useState, useEffect } from "react";
import * as component from "./components/index";
function View() {
const [display, setDisplay] = useState([]);
useEffect(() => {
const fetchData = async () => {
const result = await component.readDisplay();
setDisplay(result.data.data);
};
fetchData();
}, []);
return (
<div>
<section id="intro">
<p className="name">
<span>MICRO FRONTEND</span>
</p>
<p>
sit amet consectetur adipisicing elit. Sed, ad inventore deserunt
omnis numquam cumque quisquam quos, molestiae quaerat assumenda earum
quis nihil ea explicabo illo tempora labore? Distinctio, officia.{" "}
<a href="/" target="_blank">
click here
</a>
.
</p>
</section>
<div className="cards_wrapper">
<div classNmae="pricing_card">
{display.map((display) => (
<div className="pricing">
<h2 class="title">{display.attributes.title}</h2>
<p class="plan_description">{display.attributes.description}</p>
<p class="price">${display.attributes.price}</p>
</div>
))}
</div>
</div>
</div>
);
}
export default View;
body {
font-family: Arial, Helvetica, sans-serif;
}

a {
color: #e310cb;
text-decoration: none;
}
a:hover {
color: #ff17e4;
text-decoration: none;
}
/* intro styles*/
#intro {
padding-bottom: 10rem;
background-color: #171321
}
#intro p {
font-size: 1rem;
line-height: 1.5;
color: #fff;
}

.name span {
margin-top: 18px;
font-family: var(--sans);
font-size: 4rem;
color: #86fbfb;
display: block;
}
intro h2 {
font-size: 10rem;
color: #fff;
}

.pricing {
display: flex;
float: left;
width: 25%;
flex-direction: column;
margin-left: 50px;
margin-top: 20px;
background-color: white;
color: #333;
min-height: 320px;
max-width: 260px;
border-radius: 8px;
box-shadow: 1px 10px 20px rgba(0, 0, 0, .2);
}
.title {
font-size: 32px;
font-weight: 300;
margin-bottom: 16px;
}
.plan_description {
margin-bottom: 48px;
color: lightslategray;
line-height: 140%;
letter-spacing: .25px;
}
.price {
font-size: 52px;
font-weight: bold;
margin-bottom: 4px;
}
.price_description {
font-size: 12px;
color: lightslategray;
margin-bottom: 16px;
}
import React from "react";
import ReactDOM from "react-dom";
import Header from "remote/Header";
import View from "./View";
import "./index.css";
const App = () => (
<div className="container">
<div>
<Header />
</div>
<div>
<View />
</div>
</div>
);
ReactDOM.render(<App />, document.getElementById("app"));
Micro Frontend sample

Conclusion

--

--

Strapi is the leading open-source headless CMS. It’s 100% Javascript, fully customizable and developer-first. Unleash your content with Strapi.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store