已发布·持续改进
Next.js 指南 · 6/6
本章目前仅提供英文版。
You can run a Next.js application as a Node.js server, package it in a Docker container, export it as static files, or deploy it through a platform's own integration. The option you pick determines which features are available, so it pays to understand what a production build produces first.
next build optimizes your code, writes the production output to the .next folder and prerenders every page that can be rendered statically. When it finishes, it prints each route with a symbol showing whether it was prerendered or will be rendered on the server per request. Check that list for pages that turned dynamic unexpectedly.
npm run build # next build
npm run start # next start, port 3000 by default
# run on another port
npx next start -p 8080The Node.js server started by next start supports every feature: Server Components, Server Actions, Route Handlers, image optimization, revalidation and more. On a single machine, a common setup runs it under a process manager such as pm2 or systemd behind a reverse proxy like Nginx that handles HTTPS and compression.
To avoid shipping all of node_modules to the server, set output: "standalone". The build traces the files the app actually needs, copies them into .next/standalone, and generates a small server.js you can run instead of next start.
// next.config.ts
import type { NextConfig } from "next";
const nextConfig: NextConfig = {
output: "standalone",
};
export default nextConfig;This server does not copy the public and .next/static folders automatically. Unless a CDN serves them, copy them yourself before starting. The PORT and HOSTNAME environment variables control where it listens.
cp -r public .next/standalone/
cp -r .next/static .next/standalone/.next/
PORT=8080 HOSTNAME=0.0.0.0 node .next/standalone/server.jsStandalone output is especially handy for small Docker images. A multi-stage build separates installing dependencies, building and running, so the final image contains only what is needed at runtime.
FROM node:lts-alpine AS deps
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
FROM node:lts-alpine AS builder
WORKDIR /app
COPY --from=deps /app/node_modules ./node_modules
COPY . .
RUN npm run build
FROM node:lts-alpine AS runner
WORKDIR /app
ENV NODE_ENV=production
ENV PORT=3000
ENV HOSTNAME=0.0.0.0
COPY --from=builder /app/public ./public
COPY --from=builder /app/.next/standalone ./
COPY --from=builder /app/.next/static ./.next/static
EXPOSE 3000
CMD ["node", "server.js"]When you run several containers, you may need to share cached and revalidated data between instances. See the cache handler section of the official self-hosting guide.
To serve the site as plain HTML, CSS and JavaScript with no server, set output: "export". next build then writes static files to the out folder, ready for Nginx, Apache, object storage or GitHub Pages.
// next.config.ts
const nextConfig = {
output: "export",
trailingSlash: true,
};
export default nextConfig;Features that need a server at request time are not available: dynamic rendering that reads cookies, Server Actions, Route Handlers that compute a response per request, revalidation and the default image optimization are all limited. If you need them, choose a Node.js server or a platform deployment instead.
Vercel, the company behind Next.js, deploys a connected Git repository with no extra configuration. Other platforms support Next.js through deployment adapters or their own integrations. Feature coverage varies by provider, so read each provider's Next.js documentation.
NEXT_PUBLIC_ are inlined into browser code at build time. Changing them requires a rebuild.next build output, image optimization, metadata and error pages (error.tsx, not-found.tsx).next build followed by next start gives you every Next.js feature.output: "standalone" collects only the required files and keeps Docker images small.output: "export" enables static hosting but drops server features.NEXT_PUBLIC_ values are fixed at build time.
0 条评论
登录 · 登录后即可发表评论。
来发表第一条评论吧。