Creating a Static Export of Your Next.js Application – HTML Output, Serverless Hosting, and Image Optimization

Next.js allows you to generate a static export of your application, turning each route into an independent HTML file. This article explains how to configure static output, use Server and Client Components, optimize images with Cloudinary, and host your app on static servers like Nginx.

Server ComponentCloudinaryNginx

~3 min read · Updated Oct 28, 2025

Introduction


Next.js lets you start as a SPA or static site and progressively add server features. When you run next build, each route is rendered into a separate HTML file, reducing JavaScript load and improving performance.


Static Export Configuration


In next.config.js, enable static export:

const nextConfig = {
  output: 'export',
  trailingSlash: true, // optional
  distDir: 'dist', // optional
}
module.exports = nextConfig

After running next build, the out folder will contain your HTML/CSS/JS files.


Server Components Support


Server Components run during build and produce static HTML:

export default async function Page() {
  const res = await fetch('https://api.example.com/...')
  const data = await res.json()
  return <main>...</main>
}

Client Components and SWR


To fetch data on the client:

'use client'
import useSWR from 'swr'

const fetcher = (url) => fetch(url).then((r) => r.json())

export default function Page() {
  const { data, error } = useSWR('/api/posts/1', fetcher)
  if (error) return 'Failed to load'
  if (!data) return 'Loading...'
  return data.title
}

Client-Side Navigation Between Pages


Example index page:

import Link from 'next/link'

export default function Page() {
  return (
    <>
      <h1>Index Page</h1>
      <ul>
        <li><Link href="/post/1">Post 1</Link></li>
        <li><Link href="/post/2">Post 2</Link></li>
      </ul>
    </>
  )
}

Image Optimization with Cloudinary


In next.config.js:

images: {
  loader: 'custom',
  loaderFile: './my-loader.ts',
}

In my-loader.ts:

export default function cloudinaryLoader({ src, width, quality }) {
  const params = ['f_auto', 'c_limit', `w_${width}`, `q_${quality || 'auto'}`]
  return `https://res.cloudinary.com/demo/image/upload/${params.join(',')}${src}`
}

Using next/image


import Image from 'next/image'

export default function Page() {
  return <Image alt="turtles" src="/turtles.jpg" width={300} height={300} />
}

Using Route Handlers for JSON Output


// app/data.json/route.ts
export async function GET() {
  return Response.json({ name: 'Lee' })
}

This will generate a static data.json file during build.


Accessing Browser APIs


In a Client Component:

'use client'
import { useEffect } from 'react'

useEffect(() => {
  console.log(window.innerHeight)
}, [])

Unsupported Features in Static Export


  • Dynamic routes without generateStaticParams()
  • Cookies, redirects, rewrites, Proxy, ISR, Server Actions
  • Image optimization with default loader

Hosting on Static Servers


After build, Next.js generates files like:

/out/index.html
/out/404.html
/out/blog/post-1.html
/out/blog/post-2.html

Nginx configuration:

server {
  listen 80;
  server_name acme.com;
  root /var/www/out;

  location / {
    try_files $uri $uri.html $uri/ =404;
  }

  location /blog/ {
    rewrite ^/blog/(.*)$ /blog/$1.html break;
  }

  error_page 404 /404.html;
  location = /404.html {
    internal;
  }
}

Conclusion


With static export in Next.js, you can deploy your app without a Node.js server to any HTML/CSS/JS host. It’s ideal for SPAs, documentation sites, and lightweight projects.


Written & researched by Dr. Shahin Siami

Related Articles

Advanced Client-Side Routing and Performance Hooks in Next.js

Next.js provides a rich set of client-side hooks and caching utilities that empower developers to build dynamic, responsive, and secure applications. From reading route parameters to tracking navigation state and reporting performance metrics, this guide walks you through the most important tools available in the App Router.

Continue

Handling Authorization and Caching in Next.js: A Developer’s Guide

Next.js introduces powerful experimental features for access control and smart caching. This guide covers the unauthorized() function for custom 401 handling, unstable_cache for persistent memoization, updateTag for instant cache invalidation, and useLinkStatus for inline navigation feedback. Learn how to use these tools to build secure, performant, and responsive applications.

Continue

redirect and refresh in Next.js — Smart Redirects and Client Refreshing via Server Actions

The redirect function in Next.js allows you to navigate users to a new route, returning either a 307 or 303 HTTP response depending on context. It works in Server Components, Client Components, Route Handlers, and Server Actions. The refresh function is used exclusively within Server Actions to refresh the client router. This article explains how both functions work, with practical examples and key considerations.

Continue

NextRequest and NextResponse in Next.js — Managing Cookies, Headers, Redirects, and Rewrites

Next.js extends the native Web Request and Response APIs with NextRequest and NextResponse, offering powerful tools for managing cookies, headers, redirects, rewrites, and JSON responses. These utilities simplify server-side logic and improve control over routing, personalization, and security. This guide walks through their capabilities with practical examples and best practices.

Continue

headers, ImageResponse, notFound, and permanentRedirect in Next.js — Request Handling, Dynamic Images, Errors, and Redirects

Next.js offers powerful tools for handling HTTP requests and responses in Server Components. The headers function lets you read incoming request headers. ImageResponse allows you to generate dynamic images using JSX and CSS. The notFound function renders a custom 404 page, and permanentRedirect enables permanent redirection to another route. This article explains how to use each feature with practical examples.

Continue

A Complete Guide to Using metadata and generateMetadata in Next.js

In modern versions of Next.js, managing page metadata is more powerful and intuitive than ever. Metadata is automatically injected into the <head> of your pages and plays a vital role in SEO, social sharing, and user experience. This guide explains the two main ways to define metadata: using the static metadata object and the dynamic generateMetadata function.

Continue