Module Federation in Next.js 16 after nextjs-mf
Say your storefront has a product page in app/ and an older page in pages/. The cart team wants to update the Add to cart button on both pages this afternoon. Can they do that without putting a storefront redeploy on som
Say your storefront has a product page in app/ and an older page in pages/. The cart team wants to update the Add to cart button on both pages this afternoon. Can they do that without putting a storefront redeploy on someone elseβs calendar?
Thatβs the problem I built next-federation to solve. Next.js loads the button from a separately deployed app at runtime. The button uses the storefrontβs cart context, so when you click it, the count in the header goes up. The cart team can ship a compatible update without rebuilding the storefront. Source on GitHub
If youβre coming from @module-federation/nextjs-mf, the old integration supported the webpack-based Pages Router and is now marked deprecated. I built this as an alternative for teams using the App Router or Pages Router on Next.js 16. The host can use Turbopack or webpack. You can move a remote first and migrate a route later. Upstream deprecation notice Β· Migration guide
Start with one button
The cart team builds a Vite remote and exposes CartButton as a Module Federation 2.0 module. The storefront owns the route and the cart provider. Hereβs the remote config, adapted from the demo to show a shared dependency range.
import { defineConfig } from 'vite'
import { nextFederationComponents } from '@next-federation/vite'
export default defineConfig({
plugins: [
nextFederationComponents({
name: 'cart',
base: 'https://cart.example.com/',
exposes: { './CartButton': './src/CartButton.tsx' },
shared: {
'example-cart-context': { requiredVersion: '^1.0.0' },
},
}),
],
})
The host registers its installed version of example-cart-context and provides the module. ^1.0.0 accepts compatible 1.x releases. If the button really needs one release, use requiredVersion: '1.0.0'. With an incompatible version, the button can show its configured error fallback. Iβd keep the cart context host-provided so the button and the header read the same state. Shared modules guide
Use the same remote from app/ and pages/
Both routers fetch a descriptor that identifies the remote build. The host binds the exposed button with createRemoteComponent() and passes that descriptor to the wrapper.
-
App Router β Fetch the descriptor in an async Server Component using
getComponentDescriptor()from@next-federation/next/server. -
Pages Router β Fetch it in
getServerSidePropsorgetStaticPropsusing@next-federation/next/pages/server, then pass it through page props.
The same remote build can serve both routers. To server-render the button in the host, configure a verified remote build. The host guide walks through registration and signing. Host setup for both routers
Where Iβd draw the line
The cart button needs to update state the page already owns, so Iβd use a remote component. For an order-history panel that needs to fetch data with its own service credentials, Iβd use an island. The remote renders its React Server Components on its own server, and the storefront embeds the result. For a migration from nextjs-mf, keep Next.js as the host and move the exposed code into a separate remote build. Architecture
If I were trying this on a real storefront, Iβd start with something the page can live without for a bit, like product recommendations. Ship a remote update without redeploying the host. Then take the remote offline in staging and see what the customer gets. Both tests matter when youβre deciding whether to give a team its own release cycle.
Try it
Open either demo and click Add A1 to cart. Watch the count in the header. The example source includes both hosts, the remote, and the shared cart context. App Router demo Β· Pages Router demo Β· Run the examples locally
If youβre moving off nextjs-mf, which component would you move first? If something in the migration guide gets in your way, Iβd like to hear about it. GitHub issues
Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes β full credit and traffic to the original publisher.