Dev.to WebDev 🛠 Dev 👁 0 📖 5 min read

nextjs-mf doesn't support the App Router, so I built my own micro-frontends

Raise your hand if you've ever waited for a full redeploy because another team changed one button in the header. 🙋 That's the whole pitch for micro-frontends. Every team ships its own piece of the page whenever it wants

Raise your hand if you've ever waited for a full redeploy because another team changed one button in the header. 🙋

That's the whole pitch for micro-frontends. Every team ships its own piece of the page whenever it wants, and the page just... picks it up. No coordinated releases. No "can you merge my PR before Friday?".

In Next.js, the go-to tool for this was @module-federation/nextjs-mf. Then the App Router arrived, and nextjs-mf never followed. So I did what developers do when a tool doesn't exist: I built one. 😅

It's called next-federation, it's open source, and it just hit 0.2. Here's how it works.

🧩 Two ways to borrow someone else's UI

Quick vocabulary first. The app that provides the UI is the remote. Your Next.js app that renders it is the host.

The remote can hand you UI in two ways.

Islands. The remote renders React Server Components on its own server and sends back HTML. Your page embeds it, and it hydrates as its own little React root. The nice part: the remote's code never runs on your server. It keeps its own secrets, its own database and its own problems. 😄

Remote components. The remote ships a client component (Module Federation 2.0 under the hood), and it renders right inside your React tree. It can read your context and use your router, so it behaves like it was always there. Great for things like a cart or a nav menu.

Islands Remote components
Remote's code runs On the remote's server In your React tree
Props Plain JSON Anything
Your React context Doesn't cross in Readable
Good for Content the remote owns Interactive bits that must feel native

And yes, you can mix both on the same page.

⚡ Your first island in about five minutes

The remote

The remote is just a small Vite project. You tell it which islands it serves and who's allowed to embed them:

// vite.config.ts (remote)
import { nextFederationIslands } from '@next-federation/vite'
import { defineConfig } from 'vite'

export default defineConfig({
  plugins: nextFederationIslands({
    name: 'shop',
    base: 'https://shop.example.com/',
    hosts: ['https://www.example.com'],
    islands: { ProductPanel: './src/islands/ProductPanel.tsx' },
  }),
})

An island is an async Server Component. Fetch whatever you need, drop in client components, the usual:

// src/islands/ProductPanel.tsx (remote)
import { getProduct } from '../data'
import { Counter } from './Counter'

export default async function ProductPanel({ sku }: { sku: string }) {
  const product = await getProduct(sku)
  return (
    <section>
      <h2>{product.name}</h2>
      <p>${product.price}</p>
      <Counter />
    </section>
  )
}

The host

On the Next.js side there are three small pieces. Wrap your config:

// next.config.ts
import { withNextFederation } from '@next-federation/next/config'
export default withNextFederation({})

List the remote and create the island component:

// lib/federation.ts
import 'server-only'
import { defineFederation } from '@next-federation/core'
import { createIslands } from '@next-federation/next/server'

export const federation = defineFederation({
  remotes: { shop: { origin: 'https://shop.example.com' } },
})

export const { RemoteIsland } = createIslands(federation)

And from there it's just a component:

// app/page.tsx
import { RemoteIsland } from '../lib/federation'

export default function Page() {
  return (
    <RemoteIsland
      remote="shop"
      island="ProductPanel"
      props={{ sku: 'A1' }}
      fallback={<p>Loading product…</p>}
      errorFallback={<p>Product unavailable</p>}
    />
  )
}

Now when the shop team deploys a new ProductPanel, your page shows it. You don't rebuild anything. And if their server is having a bad day, your page still returns 200 and shows the errorFallback instead of falling over.

🗄️ Caching is where I was most stubborn

Islands go through Next.js's own fetch cache. So a page with a public island can stay completely static, or use ISR, like any other page. Need one island refreshed right now? revalidateIsland() calls revalidateTag under the hood. On Next 16 it works with cacheComponents too.

I set myself one rule early and kept it: the library must never quietly turn a static page into a dynamic one. If you've ever lost an afternoon to "why is this page suddenly not cached?!", you know why.

🔐 Per-user islands, without leaking tokens

Islands can also be private, meaning they render per signed-in user. After a short identity setup, the host mints a short-lived token for the remote, and the remote reads the user with getIdentity().

That token never shows up in your HTML, in Next.js's RSC payload or in browser storage. And I'm not just hoping that's true: the e2e suite checks for it, in next dev and in production builds.

<RemoteIsland remote="shop" island="MyOrders" private fallback={<p>Loading orders…</p>} />

🛡️ When does the host trust a remote's code?

This is the part I thought about the most. If your server renders a remote component, it's running someone else's code on your machine. So the host only does that when the build is signed (or pinned by digest) and every file matches its sha384 hash. Anything else renders in the browser only.

A small confession: while I was preparing 0.2, one test kept failing about half the time. It turned out that, with the right timing, the runtime could load the wrong build of a remote when two builds shared a name. Not fun to track down. But I'd much rather CI finds that than you. 😬

Remote components can also use next/link, next/navigation or React Router. The build rewires those imports, so the same component navigates through whatever router the host has. App Router, Pages Router, a React Router SPA, it doesn't care.

🤷 Why only public Next.js APIs?

Because I really don't want to rewrite this every time Next.js ships a release.

Everything goes through APIs that Next.js documents: fetch with tags, 'use cache', Route Handlers and unstable_rethrow. There are no next/dist imports, and CI fails if one sneaks in. It's a bet that a Next.js upgrade won't break your micro-frontends. Ask me again in a year. 😄

👀 Try it

The live demo is probably the fastest way to get the idea: three hosts (App Router, Pages Router and a React SPA) pulling UI from two separately deployed remotes.

It's 0.2, so there are rough edges. If you've tried micro-frontends with Next.js before, I'd genuinely like to know what got in your way. Drop it in the comments or open an issue!

next-federation is an independent open-source project, not affiliated with or endorsed by Vercel.

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.