Semua artikel

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...

Tariq Khan (Open-Source)4 min read0 kali dilihat
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 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

One Vite remote serves both Next.js routers. The App Router fetches the descriptor in a Server Component and the Pages Router fetches it in page props.

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 getServerSideProps or getStaticProps using @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


Sumber artikel open-source oleh Tariq Khan di DEV Community — Baca artikel asli. Topik: #nextjs #react #webdev #opensource

Ditulis & Dikurasi Oleh

Tariq Khan (Open-Source)

Software Engineer yang berfokus pada Next.js, TypeScript, arsitektur backend skalabel, Cloud, dan Applied AI.

Semua artikel

Komentar