Next.js static export turns your whole Next.js site into plain HTML, CSS and JavaScript files at build time. You set output: "export" in the config, run next build, and get an out folder you can upload to any web server: shared cPanel hosting, an Nginx or Apache server, an object storage bucket, or a CDN. No Node.js process has to run in production.
The trade-off is that anything needing a server at request time stops working: middleware (now called proxy in Next.js 16), Server Actions, incremental regeneration, rewrites and redirects in the config, and image optimisation through the built-in loader. For most marketing sites, portfolios, documentation and blogs, that is a fair price for fast, cheap, low-maintenance hosting.
This site is built exactly this way. Here is how to set it up, what to watch for, and how to configure the common servers.
How to turn on static export
The core setup is a few lines in next.config.ts:
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
output: "export",
trailingSlash: true,
images: { unoptimized: true },
};
export default nextConfig;What each line does:
output: "export"tellsnext buildto write static files toout/.trailingSlash: truemakes/aboutbecome/about/index.html. Almost every web server servesindex.htmlfrom a folder automatically, so URLs work without extra rules.images.unoptimized: trueis needed because the default image optimiser runs on a server. Alternatively, you can set up a custom image loader that points at an image CDN.
Then run:
npm run buildand upload the contents of out/.
The official static exports guide on nextjs.org lists every supported and unsupported feature for the current version.
What works in a static export
More than people expect. With the App Router:
- Server Components run at build time. You can read files, fetch from an API or a CMS, and the result is baked into the HTML.
- Dynamic routes like
/blog/[slug]work if you tell Next.js every value at build time withgenerateStaticParams. - Metadata, Open Graph tags,
sitemap.tsandrobots.tsare generated as static files. - Client Components work as normal in the browser: forms, toggles, interactive widgets.
- Route handlers with
GETcan produce static files such as an RSS feed or a JSON file. - `next/font` downloads fonts at build time and serves them from your own domain.
A dynamic route for a blog looks like this:
// app/blog/[slug]/page.tsx
import { getAllPosts, getPost } from "@/lib/posts";
export const dynamicParams = false;
export async function generateStaticParams() {
const posts = await getAllPosts();
return posts.map((post) => ({ slug: post.slug }));
}
export default async function Post({
params,
}: {
params: Promise<{ slug: string }>;
}) {
const { slug } = await params;
const post = await getPost(slug);
return <article dangerouslySetInnerHTML={{ __html: post.html }} />;
}dynamicParams = false makes any slug not in the list a 404, which is what you want on a static site.
What does not work
| Feature | Static export | Alternative |
|---|---|---|
| Middleware / proxy | Not supported | Server or CDN rules |
Redirects and rewrites in next.config | Not supported | .htaccess, Nginx config or host's redirect settings |
Custom headers in next.config | Not supported | Set headers on the server or CDN |
| Server Actions | Not supported | Form service, serverless function, or separate API |
| ISR and on-demand revalidation | Not supported | Rebuild and redeploy when content changes |
| Reading cookies or headers per request | Not supported | Do it in client code |
| Default image optimisation | Not supported | unoptimized, custom loader, or pre-optimised images |
Dynamic routes without generateStaticParams | Build error | List all params at build time |
The two that catch people most are redirects and forms. Redirects move to the server config, shown below. Forms need somewhere to post to: a form handling service, a small serverless function, or an API you already run. Anything that sends email or saves data must live outside the static site.
Hosting on cPanel and Apache
Most shared hosting runs Apache or LiteSpeed, which reads .htaccess files. Upload the contents of out/ into public_html (or the domain's folder), then add an .htaccess file. You can keep it in your project's public folder so it is copied into out/ on every build:
ErrorDocument 404 /404.html
RewriteEngine On
# Force HTTPS
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Old URL redirects
Redirect 301 /old-services/ /services/
# Cache static assets for a long time
<IfModule mod_headers.c>
<FilesMatch "\.(js|css|woff2|webp|avif|svg)$">
Header set Cache-Control "public, max-age=31536000, immutable"
</FilesMatch>
</IfModule>Files in /_next/static/ have content hashes in their names, so they are safe to cache for a year. HTML files should not be cached that long, so visitors get updates when you redeploy.
Hosting on Nginx
On a VPS with Nginx, point the server root at the uploaded out folder:
server {
listen 443 ssl;
server_name example.com;
root /var/www/example.com/out;
location / {
try_files $uri $uri/ $uri.html =404;
}
location /_next/static/ {
add_header Cache-Control "public, max-age=31536000, immutable";
}
error_page 404 /404.html;
}try_files checks for the exact file, then a folder with index.html, then a .html file, then returns your custom 404 page. SSL settings are left out here for brevity.
Hosting on a CDN or object storage
Static hosts and CDNs are a natural fit. Upload out/, set the index document to index.html and the error document to 404.html, and add redirects and headers through the provider's own settings or config file. Each provider names these differently, so check its docs.
The benefit is that pages are served from locations close to each visitor, and there is almost nothing to maintain or patch.
Deploying updates
Without a server, content changes mean a rebuild. A simple workflow:
- Edit content (Markdown files, a CMS, or code).
- Run
next build. - Upload
out/to the server, replacing the old files.
You can automate step 3 with an rsync or FTP deploy in a CI pipeline, triggered when you push to your main branch. Upload new files before removing old ones, so visitors mid-session do not request a JavaScript file that has just been deleted.
For sites with many pages, builds take longer, but for a typical business site or blog they finish in well under a few minutes.
If your content lives in a headless CMS, most CMS platforms can call a webhook when an editor publishes. Point that webhook at your CI pipeline and the site rebuilds and redeploys on its own, so editors never need to touch the build. For a small site where content changes once a week, a manual build is perfectly fine. Either way, keep the previous out folder for a day or two so you can roll back by re-uploading it if a deploy goes wrong.
When static export is the wrong choice
Choose a server deployment instead if you need:
- Personalised pages per user, such as dashboards and account areas.
- Content that changes every minute and must appear without a rebuild.
- Server-side logic tied to each request, like geolocation or A/B testing at the edge.
- Thousands of pages that change often, where full rebuilds become slow.
For those, a Node server or a platform that supports the full feature set makes more sense. Many projects mix both: a static marketing site and a separate app on a subdomain. I covered the business side of this choice in why your next website should be a static Next.js site.
The short version
Set output: "export", trailingSlash: true and images.unoptimized: true, generate every dynamic route with generateStaticParams, and upload the out folder anywhere. Move redirects, headers and the 404 page into your server config, and handle forms outside the site. You get fast pages, cheap hosting and very little to maintain.
If you want a site built or moved this way, my Next.js development service covers the build and the hosting setup. You can also read how I tuned this site in how I built a website that scores 100 on Lighthouse.