Static exports

Intermediate · 2 min

Set output: 'export' to turn a Next.js app into plain HTML, CSS, and JavaScript for any static host, and learn what the build rejects, allows, and costs you.

Before thisGenerating static paramsBuild output and next build

Static Website Hosting · 2 min

Static exports

The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.

Taught by Static Website HostingReport a problem with this video
Not started
Set this lesson's status

What you'll learn

  • Setting output: 'export' makes next build write a folder of plain HTML, CSS, and JS to out/, which any server that can serve static files can host.
  • Server Components still work; they run once at build time. Anything that needs a request or a server, such as cookies, Server Actions, ISR, redirects, headers, or the proxy, is unsupported.
  • Dynamic routes need generateStaticParams and cannot allow unknown params, since every page must exist as a file at build time.
  • The default next/image loader does not work: the build succeeds but emits /_next/image URLs a static host cannot serve, so use images.unoptimized or a custom loader.
  • Data that changes after the build must come from the client, for example with SWR from a Client Component, or from a rebuild.

Dropping the server

A Next.js app does not have to need a server. If every page can be computed at build time, the output is a folder of files, and any host can serve them: S3, nginx, Apache, GitHub Pages, shared hosting that has no Node.js. The cost is that anything which needs a request has nowhere to run.

The docs’ deployment table marks static export as “Limited” feature support, where Node.js and Docker support everything.

Turning it on

// next.config.ts
import type { NextConfig } from 'next'

const config: NextConfig = { output: 'export' }
export default config

next build now writes an out folder. The optional settings from the docs are trailingSlash: true, which changes /about to /about/; skipTrailingSlashRedirect; and distDir to rename the output folder.

I built a small app on 16.4.6 to see the file shape. With the default settings:

out/index.html
out/about.html
out/blog/a.html
out/blog/b.html
out/404.html

With trailingSlash: true, about.html becomes about/index.html. Alongside the HTML sit .txt payload files and _next/static, which client-side navigation uses, so keep the whole folder together.

What works

  • Server Components. They run at build time. The docs show a fetch in an async page that happens once during next build. See fetching data in Server Components.
  • Client Components. For data that changes after the build, fetch in the browser; the docs use SWR. Route changes happen on the client, so it behaves like a single-page app, and browser-only APIs such as window belong in useEffect. Client-side data fetching covers the pattern.
  • Dynamic routes with generateStaticParams. Each param becomes its own file. The build output above shows /blog/a and /blog/b.
  • Route handlers, with a catch. Only GET works and it must be explicitly static.

Route handlers show the build being strict. My app/api/ping/route.ts failed the export:

Error: export const dynamic = "force-static"/export const revalidate not configured on route "/api/ping" with "output: export".

Adding export const dynamic = 'force-static' makes it render to a file at build time. The docs’ example turns app/data.json/route.ts into a data.json file. A handler that reads the request cannot be exported.

What the build rejects

A page that reads cookies() fails with an error that it “couldn’t be rendered statically because it used cookies()”. Docs list the full set of unsupported features:

  • Dynamic routes without generateStaticParams(), or with dynamicParams: true
  • Route handlers that rely on the request
  • Cookies
  • Rewrites, redirects, and headers in next.config
  • Proxy
  • Incremental Static Regeneration
  • Image Optimization with the default loader
  • Draft Mode
  • Server Actions
  • Intercepting routes

Running these in next dev errors in a similar way to setting dynamic = 'error' on the root layout. Not every one is caught by the build, though.

What the build quietly allows

Two items did not fail in my tests.

The default image loader. A page with a plain next/image built without a complaint and produced <img src="/_next/image?url=%2Fa.png&w=32&q=75">. That path belongs to the image optimizer, which runs at request time, so a static host returns 404. The fix is either images: { unoptimized: true }, after which I saw a plain src="/a.png", or a custom loader pointing at an image service, which is what the docs show with Cloudinary.

A proxy.ts. With one present, next build still succeeded and listed ƒ Proxy (Middleware), along with a warning that a static export “disables API routes and middleware”. The proxy will not run on a static host. Redirects and rewrites therefore have to move to the host’s own configuration.

The lesson: a green build is not proof that a static export works. Open the output and look.

Serving it

On nginx, the docs give try_files $uri $uri.html $uri/ =404, plus a rewrite for /blog/ paths when trailingSlash is false, and error_page 404 /404.html. Other hosts differ in whether /about finds about.html, so test a deep link and a mistyped URL before trusting it. The docs also link a GitHub Pages template.

When to choose it

Pick a static export for a marketing site, docs, or a blog that rebuilds on content changes and has no per-user pages. Pick a server the moment you need authentication cookies, Server Actions, ISR, or a proxy. The docs describe it as a start, not a dead end: you can begin static and later add a server for features that need one.

What the video covers

The video is a vendor walkthrough for Static.app. Finding its hosting page, uploading the ZIP, and its site settings are specific to that host. From its description and chapters: set output: 'export' in the config, build, zip the contents of out, upload (0:12 to 0:57), then look at the exported pages, a dynamic route with its own address (1:37), client-side links (1:52), and the export’s own 404 page (2:04). It says a static export has no server, so features that need one, like API routes, will not work. The framework part of this is correct against the docs.

Treat it as a quick demonstration, not a reference. It promotes Static.app, the channel is small, and transcripts were unavailable, so I rely on the description. The runner-up, CodeInMinute, shows the same export deployed to Hostinger shared hosting; its description names Hostinger hPanel and includes an affiliate link, and I could not confirm its spoken language.

Where this fits

Next is upgrading and the codemods, because every one of these deployment options eventually has to survive a version bump.

Check yourself

  1. 01What do Server Components do in a static export?

    They run during next build, like traditional static-site generation, and their output becomes HTML plus a payload for client-side navigation. They need no changes unless they use request-time APIs.

  2. 02Why does a [slug] route need generateStaticParams in a static export?

    There is no server to render an unknown slug on request, so every page has to be produced at build time as a file. Dynamic routes without generateStaticParams, or with dynamicParams: true, are listed as unsupported.

  3. 03You build with next/image and the default loader, and the build passes. Why is the deployed site still broken?

    The build emits image URLs of the form /_next/image?url=..., which are served by the Next.js image optimizer. A static host has no such endpoint, so the images fail. Set images: { unoptimized: true } or provide a custom loader.

  4. 04How do you show data that changes after the build?

    Fetch it in the browser from a Client Component, for example with SWR, or rebuild and redeploy. There is no server to regenerate a page for you.

Go deeper

Notes

Private to you

Esc
↑↓ navigate↵ open? shortcuts

Keyboard shortcuts

Press Esc to close

Anywhere

⌘K
Search and commands
/
Search
?
This sheet
Esc
Close

Reading a lesson

J
Next lesson
K
Previous lesson
B
Bookmark