What is a SPA?
A strict SPA:- Serves one HTML file (
index.html) - Handles all routing and data fetching in the browser with JavaScript
- Does not reload the full page on navigation
Why use Next.js for SPAs?
- Automatic code splitting — Multiple HTML entry points per route, reducing bundle size
- Fast navigation —
next/linkprefetches routes, providing instant transitions - URL-persisted state — Route state is preserved in the URL for sharing and linking
- Progressive enhancement — Add React Server Components, Server Actions, and more as needed
Patterns
Data fetching with context and use()
Fetch data in a parent Server Component (or root layout) and pass the Promise to a Client Component via context. This starts data fetching on the server early, avoiding client waterfalls:
use():
Data fetching with SWR
With SWR 2.3.0+ and React 19+, you can provide server-fetched data as SWR fallback without changing existing client code:fallback data is included in the initial HTML and immediately available to useSWR. SWR polling and revalidation still run client-side.
Rendering only in the browser
Disable prerendering for Client Components that depend on browser APIs:Shallow routing
Update the URL without triggering a full navigation using the native History API. These calls integrate with Next.js hooks likeusePathname and useSearchParams:
Using Server Actions in Client Components
Progressively replace API route boilerplate with Server Actions:Static export
Generate a fully static site with automatic code splitting per route:next build, the out/ folder contains separate HTML files per route.
Next.js server features are not supported with static exports. See static exports for the full list.
