Writing an island remote
Build a Vite remote that renders React Server Components and serves them to hosts as islands.
Build a Vite 8 remote that runs React Server Components and answers island requests from hosts. To embed an island in a host, see Getting Started.
Good to know: Requires Node ^20.19.0 || >=22.12.0. One remote can also ship remote components.
Start from create-vite
npm create vite@latest shop -- --template react-ts scaffolds a browser app. Edit it into a remote:
- Remove
@vitejs/plugin-reactand@vitejs/plugin-react-swcfrompackage.jsonandvite.config.ts. Either one fails the build withNF-002. - Delete
index.html,src/main.tsx,src/App.tsx, the CSS files andsrc/assets/. A remote has no page. - Replace
vite.config.tswith the config in Configure Vite. Put islands insrc/islands/. - Change the build script to
vite build. Add"start": "nf-serve --dir dist --port 4100".vite previewdoes not serve a remote. - Add
react-server-dom-webpackat the same exact version asreact, and remove carets. - For
node:*imports in islands, add"node"totypesintsconfig.app.jsonand keep@types/node. Never importnode:*from a'use client'file.
Build an island remote
Install the packages
Pin React, React DOM and react-server-dom-webpack to one exact version. The last is an optional peer dependency, so npm does not install it. Without it, vite fails at startup with NF-002.
npm install --save-exact react@19.3.1 react-dom@19.3.1 react-server-dom-webpack@19.3.1
npm install @next-federation/island
npm install --save-dev @next-federation/vite @next-federation/server viteRemove any caret that create-vite wrote in package.json.
Good to know: Use React 19.0.9, 19.1.10, 19.2.9, 19.3.1 or a later patch
(GHSA-xqc2-xf4c-x3h3).
vite build refuses older versions with
NF-041. After an upgrade,
remove affected
builds.
Configure Vite
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'],
issuers: {
'https://www.example.com':
'https://www.example.com/api/next-federation/jwks',
},
islands: {
ProductPanel: './src/islands/ProductPanel.tsx',
MyOrders: { entry: './src/islands/MyOrders.tsx', private: true },
},
}),
})| Option | Meaning |
|---|---|
name | Remote id. It is the token audience, so it must equal the name the host configures. |
base | Absolute URL the remote is served from. A relative base fails with NF-020. |
hosts | Exact browser origins allowed to embed (CORS and Origin check). No *. |
issuers | Trusted token issuers: exact iss value to JWKS URL. Needed for private islands. |
islands | Island name to entry file, or { entry, private }. |
See the @next-federation/vite README for every option.
Write an island
An island entry is a Server Component with a default export. It may not start with 'use client' (NF-002). Put interactive code in client components.
import './ProductPanel.css'
import { getProduct } from '../data'
import { Counter } from './Counter'
export default async function ProductPanel({ sku }: { sku: string }) {
const product = await getProduct(sku)
return (
<section className="product-panel">
<h2>{product.name}</h2>
<p>${product.price}</p>
<Counter />
</section>
)
}const products: Record<string, { name: string; price: number }> = {
A1: { name: 'Desk lamp', price: 39 },
}
export async function getProduct(sku: string) {
return products[sku] ?? { name: `Unknown product ${sku}`, price: 0 }
}.product-panel {
border: 1px solid #ddd;
padding: 1rem;
}'use client'
import { useState } from 'react'
export function Counter() {
const [n, setN] = useState(0)
return <button onClick={() => setN((v) => v + 1)}>count {n}</button>
}Imported CSS goes into the envelope. Do not call import.meta.viteRsc.loadCss().
Build and serve
vite build # writes dist/<buildId>/{client,ssr,rsc} and nf-build.json
nf-serve --dir dist --port 4100 # from @next-federation/servervite serves the remote in development. See Deployment for production.
Good to know: vite build warns [MODULE_LEVEL_DIRECTIVE] "use client"
once per client component. The warnings are harmless.
Match host origins and issuers exactly
Both lists are compared as strings.
hostsare origins: scheme, host and port, no path.issuersequal the host'sidentity.issuerexactly, includingbasePath. The JWKS URL includes it too.
hosts: ['https://www.example.com'],
issuers: {
'https://www.example.com/app': 'https://www.example.com/app/api/next-federation/jwks',
},identity: { issuer: 'https://www.example.com/app', signingKeys: process.env.NF_SIGNING_KEY! },A wrong issuer answers 401 (NF-016). An unlisted origin answers 403 (NF-022). A new host origin needs a rebuild.
Show per-user content
import { getIdentity } from '@next-federation/island'
export default async function MyOrders() {
const user = getIdentity() // { sub, ... } from a verified token
if (!user) throw new Error('MyOrders requires identity')
return <h2>Orders for {user.sub}</h2>
}getIdentity() works in Server Components and Server Actions. In client code it throws NF-002: pass what the client needs as props. See Identity.
Link to host pages
import { Link } from '@next-federation/island'
;<Link href="/orders/1">Order</Link>Link renders a real <a href>. Write host-relative hrefs. The host adds basePath and locale.
Develop against a host
Run vite and point the host at it (origin: 'http://localhost:4100'). There is no Fast Refresh: edit, then reload the host page.
The host caches public envelopes for 60 seconds, also in next dev. Add dynamic while you work (Caching):
<RemoteIsland
remote="shop"
island="ProductPanel"
dynamic
fallback={<p>loading…</p>}
/>Good to know: Remove dynamic before you ship. It makes the route
dynamic.
Know the rules
- Props are plain JSON. React context does not cross into an island.
- Do not nest islands. Embed both from the host (Limits).
<title>,<meta>and<link>never reach the host<head>(NF-018in development).- Pass Server Actions to client components as props (
NF-034). Use<Form>from@next-federation/island(NF-035). - Keep islands small. The host warns above 100 KB per envelope (
NF-024). @vitejs/plugin-rscmust be exactly 0.5.39 (NF-025).
Next steps
Identity
Private islands, signing keys and key rotation.
Deployment
Serve a remote in production and keep old builds.
Server Actions
Run actions from an island.
Limits
What islands cannot do and what they cost, including JS cost.
Runnable example: examples/vite-remote.