iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
😸

Creating a Static Site with Next.js App Router

に公開

Next.js 13.3 now supports static exports (next export) with the App Router feature.

Static Export Support

This is a summary of the following scrap:

https://zenn.dev/hidaka/scraps/32ba6e601fe460

TL;DR

  • Create an application template using create-next-app
  • Enable static output options
  • Delete the pages directory and API Routes
  • Build

Create a Next.js Application

Create an Application Template

Create an application template using create-next-app.

terminal
$ npx create-next-app@latest        

Enable the app/ directory option.

terminal
Need to install the following packages:
  create-next-app@13.3.0
Ok to proceed? (y) y
✔ What is your project named? … nextjs-demo
✔ Would you like to use TypeScript with this project? … No / Yes
✔ Would you like to use ESLint with this project? … No / Yes
✔ Would you like to use Tailwind CSS with this project? … No / Yes
✔ Would you like to use `src/` directory with this project? … No / Yes
✔ Would you like to use experimental `app/` directory with this project? … No / Yes
✔ What import alias would you like configured? … @/*
Creating a new Next.js app in /Users/ryohidaka/nextjs-demo.

Enable Static Output Options

Modify next.config.js following the documentation.

next.config.js
/** @type {import('next').NextConfig} */
const nextConfig = {
  experimental: {
    appDir: true,
  },
  // @see https://beta.nextjs.org/docs/configuring/static-export#configuration
  output: "export",
};

module.exports = nextConfig

Delete the pages Directory and API Routes

Delete pages/api/hello.ts as it is included in the initial generated files.

error - API Routes cannot be used with "output: export". See more info here: https://nextjs.org/docs/advanced-features/static-html-export

Also, if the pages directory exists, it will conflict with the app directory and cause a 404 error, so delete the entire pages directory.

Verification

Check if it runs with hot reloading

Run next dev and confirm that the default Next.js page can be displayed.

Next.js default page

Build

Perform a static export.

Deploying
With a static export, Next.js can be deployed and hosted on any web server that can serve HTML/CSS/JS static assets.

When running next build, Next.js generates the static export into the out folder. Using next export is no longer needed. For example, let's say you have the following routes:

Previously, it was necessary to perform a static export after building using something like next build && next export, but starting from Next.js 13.3, it seems that running next build alone will output the assets to the out directory.

https://beta.nextjs.org/docs/configuring/static-export#deploying

I confirmed that the output process completed successfully and the contents were extracted under the out directory.

Console output upon successful build

The out directory upon successful build

GitHubで編集を提案

Discussion