Next.js supports starting as a static site or SPA, then optionally upgrading to server features later. When you run next build with output: 'export', Next.js generates an HTML file per route, enabling deployment to any static host.
Configuration
Set output: 'export' in next.config.js:
After running next build, Next.js creates an out/ folder with your HTML/CSS/JS assets.
Supported features
Server Components
Server Components run during next build, similar to static-site generation. The rendered HTML is output for the initial page load and a static payload for client navigation.
Client Components
Client Components are prerendered to HTML at build time. For client-side data fetching, use SWR:
Route Handlers
Route Handlers render a static response during next build. Only GET requests are supported:
The above produces out/data.json containing { "name": "Lee" }.
Image optimization
Use a custom image loader to optimize images via a third-party service:
Browser APIs
Client Components are prerendered on the server during next build, so browser APIs like window and localStorage are unavailable at that point. Access them only after mounting:
Unsupported features
Features requiring a Node.js server or dynamic logic at request time are not supported with static exports:
Using any of these with output: 'export' will result in a build error:
- Dynamic Routes without
generateStaticParams()
- Route Handlers that read from the request
- Cookies and Headers APIs
- Rewrites, Redirects, Headers config
- Middleware
- Incremental Static Regeneration
- Default image optimization loader
- Draft Mode
- Server Actions
- Intercepting Routes
Deploying
After next build, the out/ folder contains your static assets. Given routes / and /blog/[id], Next.js generates:
Deploy to any static host. For Nginx:
You can also deploy to Vercel, GitHub Pages, Netlify, AWS S3, or any CDN that serves static files.
Version history