Module Federation SSR: Faster Micro-Frontend Rendering

Краткое содержание

Pixel shares a guide to combining Module Federation with server-side rendering for micro-frontends. It explains how hosts fetch and render remote server bundles, then hydrate with client bundles, while highlighting deployment benefits and challenges around caching, runtime consistency, network reliability, and security.

DEV Community

The Future of Micro-Frontends: How to Combine Module Federation with Server-Side Rendering (SSR)

Client-side Module Federation has a well-known weakness, the browser downloads a shell, then fetches remotes, then renders. Users stare at a blank page, and crawlers may see very little. For content-heavy or SEO-sensitive products, that's a deal-breaker, and it's why teams keep asking the same question, can we get independently deployed micro-frontends and server-rendered HTML? The answer is yes, with caveats. Module Federation supports server-side rendering, and the same model that works in the browser transfers to Node, a host fetches code from remotes at runtime and renders it on the server. But SSR raises the stakes. You now have two runtimes to keep consistent, a network dependency inside your render path, and a security boundary you didn't have before. This guide explains how the pieces fit, shows a working configuration pattern, and is candid about where the ecosystem is solid and where it isn't. How Module Federation Works with SSR In client-only federation, a remote publishes a remoteEntry.js, and the browser loads it on demand. With SSR, each application builds twice, a browser bundle and a server bundle, each with its own federation entry. Request ──► Host server (Node)
│ 1. fetch remote's SERVER entry (cached)
│ 2. execute remote code, render to HTML stream
▼
HTML response ──► Browser
│ 3. load remote's CLIENT entry
▼
Hydration
Conceptually, the remotes act as static asset servers, the host's server pulls their server-side code and executes it in the same Node process that renders the page. That's what makes independent deployment possible, ship a new remote and the host picks it up without a rebuild, and it's also the source of most SSR-specific risk. Step 1 - Build the Remote Twice The remote exposes a component and produces a server entry alongside the client one. A server federation config typically targets a Node environment and uses the @module-federation/node runtime plugin: npm install @module-federation/enhanced @module-federation/node
// remote/webpack.server.js
const { ModuleFederationPlugin } = require('@module-federation/enhanced/webpack');

module.exports = {
target: 'async-node',
output: { path: __dirname + '/dist/server', libraryTarget: 'commonjs-module' },
plugins: [
new ModuleFederationPlugin({
name: 'productApp',
filename: 'remoteEntry.js',
library: { type: 'commonjs-module' },
exposes: { './ProductList': './src/ProductList' },
runtimePlugins: [require.resolve('@module-federation/node/runtimePlugin')],
shared: { react: { singleton: true }, 'react-dom': { singleton: true } },
}),
],
};
The client config mirrors our earlier micro-frontend setup (target: 'web', output to dist/client). Serve both directories from the remote's static host so each has its own entry URL, for example /server/remoteEntry.js and /client/remoteEntry.js. Config details vary by version, so treat this as a pattern and confirm option names in the current docs. Step 2 - Load Remotes at Runtime on the Host On the host's server, use the federation runtime to register and load remotes dynamically rather than hardcoding them at build time: // host/server/federation.ts
import { init, loadRemote } from '@module-federation/enhanced/runtime';

init({
name: 'host',
remotes: [
{ name: 'productApp', entry: ${process.env.PRODUCT_APP_URL}/server/remoteEntry.js },
],
});

export async function loadProductList() {
const mod = await loadRemote<{ default: React.ComponentType }>('productApp/ProductList');
return mod?.default ?? null;
}
Then render with a timeout and a fallback so a slow remote can't hang your page: // host/server/render.tsx
import { renderToPipeableStream } from 'react-dom/server';

export async function renderPage(res, props) {
const ProductList = await withTimeout(loadProductList(), 800).catch(() => null...
Module Federation SSR: Faster Micro-Frontend Rendering
Powered by Readybot.io

Последнее от Pixel

Incident Response: Look Beyond the Red Alert

Pixel’s DEV Community announcement explores how teams can fixate on one failing service while overlooking the real cause of an outage, such as a full …

Sach: AI Fact-Checks WhatsApp Forwards in India

Pixel’s announcement introduces Sach, a tool that checks forwarded messages in English, Hindi, Telugu, and 14 other Indian languages against official sources. It gives cited …

Java 25 Removes SecurityManager: What to Fix

Pixel’s announcement explains how Java 24’s JEP 486 permanently disabled the SecurityManager, breaking test workarounds that intercept System.exit. It recommends returning exit codes or throwing …

Why a Java HPA Got Stuck at 20 Kubernetes Pods

Pixel shares how a Spring Boot pricing service autoscaled to 20 Kubernetes pods and stayed there because the JVM retained committed heap memory after traffic …