Micro-frontends extend the microservices philosophy to the frontend. Instead of a monolithic SPA, you compose the UI from independently developed, tested, and deployed frontend modules.
🏗️ Why Micro-Frontends?
- Team Autonomy — Each team owns a vertical slice (feature) end-to-end
- Independent Deployments — Ship features without coordinating releases
- Tech Diversity — Mix React, Vue, Svelte in one app
- Scalability — Add teams without slowing down
📊 Integration Approaches
| Approach | Best For | Complexity |
|---|---|---|
| Module Federation 2.0 | Webpack/Rspack apps | Medium |
| Single-SPA | Multi-framework orchestration | High |
| Web Components | Framework-agnostic widgets | Low |
| iFrames | Complete isolation | Low |
💻 Module Federation 2.0 Example
// webpack.config.js — Host Application
const { ModuleFederationPlugin } = require("webpack").container;
module.exports = {
plugins: [
new ModuleFederationPlugin({
name: "host",
remotes: {
cart: "cart@https://cart.example.com/remoteEntry.js",
checkout: "checkout@https://checkout.example.com/remoteEntry.js",
},
shared: {
react: { singleton: true, requiredVersion: "^18.3.0" },
"react-dom": { singleton: true, requiredVersion: "^18.3.0" },
},
}),
],
};
// App.tsx — Lazy load remote modules
const Cart = React.lazy(() => import("cart/CartWidget"));
const Checkout = React.lazy(() => import("checkout/CheckoutPage"));
function App() {
return (
<Suspense fallback={<Loading />}>
<Cart />
<Checkout />
</Suspense>
);
}
🌐 Web Components Approach
// Define a micro-frontend as a Web Component
class UserProfile extends HTMLElement {
connectedCallback() {
const shadow = this.attachShadow({ mode: "open" });
shadow.innerHTML = `
<style>
.profile { padding: 16px; border-radius: 8px; }
</style>
<div class="profile">
<h3>${this.getAttribute("name")}</h3>
<p>${this.getAttribute("role")}</p>
</div>
`;
}
}
customElements.define("user-profile", UserProfile);
// Use anywhere: <user-profile name="John" role="Admin" />
⚠️ Challenges to Consider
- Shared state — Use event buses or shared stores carefully
- CSS conflicts — Shadow DOM or CSS Modules for isolation
- Performance — Avoid duplicating large dependencies
- Testing — Integration tests across module boundaries
💡 When to Use: Micro-frontends are ideal for large organizations with 3+ frontend teams. For smaller teams, a well-structured monolith is simpler and faster to develop.