next-federation
API Reference

@next-federation/next

Next.js adapter: embeds remote islands and remote components in an App Router or Pages Router host and serves the host's identity routes. Supports Next...

Next.js adapter: embeds remote islands and remote components in an App Router or Pages Router host and serves the host's identity routes. Supports Next >=15.5.28 <17; cacheComponents needs Next 16.

Install

npm install @next-federation/next @next-federation/core @next-federation/react

Usage

next.config.ts
import { withNextFederation } from '@next-federation/next/config'

export default withNextFederation({})
lib/federation.ts
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)
app/page.tsx
import { RemoteIsland } from '../lib/federation'

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

Entry points

No root entry. Import from one of these.

EntryExports
@next-federation/next/configwithNextFederation
@next-federation/next/servercreateIslands, getComponentDescriptor (App Router, server only)
@next-federation/next/routescreateFederationRoutes
@next-federation/next/componentscreateRemoteComponent, loadRemoteModule, preloadRemoteComponent, registerSharedModules, COMPONENT_EVENT
@next-federation/next/pagesRemoteIsland
@next-federation/next/pages/servercreatePagesIslands, createFederationApiHandler, getComponentDescriptor, preloadRemoteComponent
@next-federation/next/pages/componentsSame as /components, for the Pages Router
nf-types (bin)Downloads remote component prop types

API

withNextFederation(config)

Required in every Next host. Wraps a Next config object or (phase, ctx) => config function. Adapter entry points fail with NF-017 without it.

export default withNextFederation({ basePath: '/shop' })

It sets env.NF_CACHE_MODEL, env.NF_BASE_PATH and env.NF_NEXT_VERSION, aliases the internal island-cache entry for your cache model, and appends the package to transpilePackages. It throws NF-002 when a @next-federation/* package is in serverExternalPackages.

createIslands(federation, options?)

App Router only. Returns { RemoteIsland, preloadIsland, revalidateIsland }.

OptionTypeDefaultDescription
getIdentityAppIdentitynone({ cookies, headers }) => identity | null. Needed for private islands (NF-013).
lib/federation.ts
export const { RemoteIsland, preloadIsland, revalidateIsland } = createIslands(federation, {
  getIdentity,
})

RemoteIsland

A Server Component that fetches an island and renders it inside its own <Suspense>.

PropTypeDefaultDescription
remotestringrequiredRemote name from defineFederation.
islandstringrequiredThe island name.
propsRecord<string, unknown>nonePlain JSON props (NF-009 otherwise).
privatebooleanfalsePer-user island. Anonymous visitors see errorFallback.
dynamicbooleanfalseFetch on every request. The route becomes dynamic.
revalidatenumberislands.revalidateSeconds a public envelope is cached.
localestringnoneURL prefix segment passed to the remote. '' for none.
fallbackReactNodenoneThe loading state.
errorFallbackReactNodefallbackShown when the remote fails or an anonymous visitor hits a private island.
inertOnLoadFailurebooleantrue without errorFallback, else falseOn NF-011, keep the server HTML, not interactive.
blockingbooleanfalsePrivate only (NF-002). Drops the <Suspense>: in first HTML.
<RemoteIsland remote="shop" island="Cart" props={{ sku }} fallback={<Spinner />} errorFallback={<p>Cart unavailable</p>} />

With cacheComponents, a page that uses blocking must export instant = false. See the caching reference.

preloadIsland(spec)

Starts the load early, for example in a layout. An equal RemoteIsland in the same request reuses it. It never throws.

revalidateIsland(remote, island?)

Expires nf:<remote> or nf:<remote>:<island> immediately, such as from a deploy webhook. Throws NF-001 for an unknown remote.

createPagesIslands(federation, options?)

Pages Router, server only. Returns { getIslandProps }.

OptionTypeDefaultDescription
getIdentityPagesIdentitynone(req) => identity | null. Needed for private islands.
lib/federation.ts
export const { getIslandProps } = createPagesIslands(federation, { getIdentity })

getIslandProps(ctx, spec)

Returns IslandProps ({ remote, envelope, host, token }, plus stale: true for a stale-if-error envelope) to spread into RemoteIsland. spec takes remote, island, props and private. A failed island or an anonymous visitor on a private island gives envelope: null. Mistakes throw NF-001, NF-009 or NF-017. private: true works in getServerSideProps only (NF-002 otherwise).

pages/product.tsx
export const getStaticProps = async (ctx) => {
  const banner = await getIslandProps(ctx, { remote: 'shop', island: 'Banner' })
  return { props: { banner }, revalidate: banner.envelope && !banner.stale ? 60 : 5 }
}

RemoteIsland (Pages Router)

From @next-federation/next/pages. Spread getIslandProps into it. It renders errorFallback ?? fallback when envelope is null.

PropTypeDefaultDescription
fallbackReactNodenoneShown when envelope is null.
errorFallbackReactNodefallbackShown when the remote failed or after an island error.
inertOnLoadFailurebooleantrue without errorFallback, else falseAs for the App Router RemoteIsland.
pages/product.tsx
import { RemoteIsland } from '@next-federation/next/pages'
import type { IslandProps } from '@next-federation/next/pages'

export default function Product({ cart }: { cart: IslandProps }) {
  return <RemoteIsland {...cart} fallback={<p>Cart unavailable</p>} />
}

createFederationRoutes(federation, options)

Creates the JWKS and token route handlers for the App Router. Returns { GET, POST }.

OptionTypeDefaultDescription
getIdentityAppIdentityrequiredReads your same-origin session. The library sets no cookie.
app/api/next-federation/[...nf]/route.ts
import { createFederationRoutes } from '@next-federation/next/routes'
import { federation } from '@/federation'

export const { GET, POST } = createFederationRoutes(federation, {
  getIdentity: async ({ cookies }) => {
    const session = cookies.get('session')?.value
    return session ? { sub: await userIdFor(session) } : null
  },
})
RouteAnswer
GET jwksPublic keys, public, max-age=300.
POST token?aud=<remote>{ token, expiresAt, identityKey }. Anonymous: 200 {"token":null}.
anything else404.

A request with Sec-Fetch-Site not same-origin gets 403. Creating the routes calls validateIdentity(federation), so a malformed key logs NF-002 at load. Key rotation: Identity.

createFederationApiHandler(federation, options)

The Pages Router version of the routes, from @next-federation/next/pages/server. Returns a NextApiHandler.

OptionTypeDefaultDescription
getIdentityPagesIdentityrequired(req) => identity | null.
pages/api/next-federation/[...nf].ts
import { createFederationApiHandler } from '@next-federation/next/pages/server'
import { federation } from '@/federation'

export default createFederationApiHandler(federation, {
  getIdentity: (req) => (req.cookies.session ? { sub: userIdFor(req.cookies.session) } : null),
})

Island scripts run in the host origin, so any island on the page can request a token for any configured remote.

getComponentDescriptor(federation, remote, options?)

From /server (App Router) or /pages/server. Resolves and verifies the build of a components remote this render uses. Only a signed or pinned build keeps ssr; others render in the browser only (NF-028, or NF-027 for a bad signature). When the remote is down it returns the last known descriptor, or null.

OptionTypeDefaultDescription
dynamicbooleanfalseFetch on every request. With cacheComponents, use <Suspense>.
app/cart.tsx
import { getComponentDescriptor } from '@next-federation/next/server'

const descriptor = await getComponentDescriptor(federation, 'shop')
return <Cart descriptor={descriptor} items={3} /> // Cart: createRemoteComponent in a 'use client' file

createRemoteComponent and the other components exports

/components (App Router) and /pages/components (Pages Router) are 'use client' entries. They export createRemoteComponent, loadRemoteModule, preloadRemoteComponent, registerSharedModules and COMPONENT_EVENT, like @next-federation/react/components (without fetchDescriptorInBrowser). They also share the host's next/link and next/navigation (next/router in Pages) with remotes that list them in shared.

lib/NavMenu.tsx
'use client'
import { createRemoteComponent } from '@next-federation/next/components'

export const NavMenu = createRemoteComponent({ expose: './NavMenu', fallback: null })

A remote component that imports a module the host does not provide renders its fallback (NF-036). See Remote components.

nf-types

Downloads each components remote's prop types into next-federation-types/<remote>/ and writes index.d.ts, which fills RemoteProps.

nf-types --config lib/federation.config.ts [--remote shop]... [--out next-federation-types]
FlagDescription
--configFederation file with no Next.js imports. Exports federation or a default.
--remoteA remote to sync. Repeatable. Default: every remote with components.
--outOutput folder. Default next-federation-types.
createRemoteComponent<RemoteProps<'shop', './Cart'>>({ expose: './Cart' })

A .ts config needs Node 22.18 or 23.6 or newer; otherwise use .mjs. It does not read .env.local, runs nothing it downloads, checks every file's sha384 and exits 1 when a remote fails. See Remote components.

Types

IslandSpec, RemoteIslandProps, AppIdentity, PagesIdentity, PagesIslandSpec, IslandProps, ComponentDescriptorOptions, RemoteComponentDescriptor.

Learn more

License: MIT

On this page