Back to blog
Deploy9 min read1 read

Deploying Next.js to production: what SSG, ISR and SSR each need from your infrastructure

The project that runs perfectly under next dev needs things in production that the getting-started docs never mention.

The first time a Next.js project hurts in production, the code is rarely the culprit. The product page still shows the old price after you changed it. A form built on a Server Action errors for a few minutes after every deploy. An API URL you changed in an environment variable still has the old value in the browser.

All of these share a root cause: Next.js expects different things from your infrastructure depending on how each page is rendered. This post spells those expectations out.

Where and when is the HTML built?

The key question for any page is the moment its final HTML is produced. The answer decides how much work the server does, where the cache has to live, and what your response time depends on.

How HTML is produced in SSG, ISR, SSR and PPR modes in Next.js

SSG (static generation): the page is built once, during next build. After that the server just hands out a ready-made file. It's the fastest and cheapest mode, but the data is frozen until the next build.

ISR (incremental regeneration): the page is served from cache, and when the revalidate time passes or revalidatePath / revalidateTag is called, a fresh version is built in the background. This needs a live Node server and a cache that survives between requests.

SSR (server rendering): on every request, server code runs, fetches data and builds HTML. The data is always fresh, but time to first byte (TTFB) is tied directly to your database and API speed.

PPR (partial prerendering): a static shell of the page is sent immediately and dynamic parts stream in. In recent Next.js versions this model has grown into Cache Components and is still changing, so read the docs for the exact version you run before relying on it in production.

What each mode needs from your infrastructure

ModeNeeds a Node server?Persistent cacheDatabase access at request timeGood for
SSGNo (with output: "export")Not neededOnly at build timeLanding pages, docs, a rarely changing blog
ISRYesYes, shared if you run several instancesWhen regeneratingProduct catalogues, CMS content
SSRYesOptionalEvery requestDashboards, personalised pages
PPRYesYes, for the static partFor dynamic partsPages that are mostly static with a little dynamic

Keep in mind that a real project is usually a mix. As soon as a single page uses ISR or SSR, you need a Node server, and a purely static deployment is off the table.

Standalone output: the leanest way to run

With output: "standalone", Next.js collects only the files and dependencies actually needed at runtime into .next/standalone, along with a server.js. The result runs without the full node_modules and is ideal for containers and platforms.

next.config.ts
import type { NextConfig } from "next";

const config: NextConfig = {
  output: "standalone",
  // One id per deployment, so browser and server versions can't silently drift apart
  deploymentId: process.env.DEPLOYMENT_ID,
};

export default config;

Two things almost everyone runs into once:

  1. The public and .next/static folders are not copied into the standalone output by default. If you build your own image, put them next to server.js yourself, or the site comes up with no CSS or images.
  2. server.js listens on whatever address is in HOSTNAME. Inside a container, set it to 0.0.0.0 so it's reachable from outside.

If you do need a Dockerfile, a minimal multi-stage version looks like this:

Dockerfile
FROM node:22-alpine AS build
WORKDIR /app
COPY package.json package-lock.json ./
RUN npm ci
COPY . .
RUN npm run build

FROM node:22-alpine AS run
WORKDIR /app
ENV NODE_ENV=production HOSTNAME=0.0.0.0 PORT=3000
COPY --from=build /app/.next/standalone ./
COPY --from=build /app/.next/static ./.next/static
COPY --from=build /app/public ./public
USER node
EXPOSE 3000
CMD ["node", "server.js"]

On Parslinks most projects don't need this file at all: the Next.js preset defines the install, build and start commands. Add a Dockerfile when you need a specific system package.

When the app runs on more than one instance

With one instance, most things just work. Problems start when two or more instances serve traffic at once, or when the old and new versions are alive side by side for a few seconds.

SymptomCauseFix
Two users see two different versions of the same ISR pageEach instance has its own filesystem cacheA shared cacheHandler, for example backed by Redis
Server Actions fail for a while after every deployThe action encryption key changes with each buildA fixed value for NEXT_SERVER_ACTIONS_ENCRYPTION_KEY
A user with the page already open gets errors when navigatingBrowser has old code, server has new codeSet deploymentId so Next.js detects the mismatch and does a full reload
An uploaded file is sometimes there, sometimes notIt was saved to one instance's diskStore uploads in object storage

The NEXT_PUBLIC_ trap

Any variable starting with NEXT_PUBLIC_ is written straight into the browser JavaScript at build time. After the build, changing it on the server has no effect on code that already shipped to browsers.

This matters even more when your platform, like Parslinks, promotes the same preview artifact to production. That's deliberate: it guarantees that what you approved is what goes live. But it also means any NEXT_PUBLIC_ value baked into the preview build reaches production too.

Variables without the prefix, like DATABASE_URL, are read only on the server and can change at runtime. That's why one build can serve a preview and production connected to two different databases.

Image optimisation and health checks

On a Node server, next/image resizes images on request and caches the result. Check two things: that external image hosts are listed in images.remotePatterns, and that if you see build or runtime errors about sharp, you add it explicitly to your dependencies.

So the platform knows the app is actually ready for traffic, add a simple health route that doesn't depend on any external service:

app/api/health/route.ts
export const dynamic = "force-dynamic";

export function GET() {
  return Response.json({ ok: true, version: process.env.DEPLOYMENT_ID ?? "dev" });
}

Connect the repo and pick the Next.js preset. Every branch you push gets its own build and preview URL, with build logs and status right there. When a version is approved, that same artifact is promoted to production, and if you spot a problem after release, you roll back to the previous version without rebuilding.

Before the first deploy, pin down four things so builds are reproducible everywhere: the Node version (for example in the engines field of package.json), your package manager's lockfile, the environment variables for each environment, and which pages need runtime data.

The one-paragraph summary

If every page is static, you don't need a server at all. The moment ISR or SSR shows up, you need a Node server, ideally running lean with output: "standalone". And before you scale to multiple instances, share three things: the cache, the Server Actions key, and where files are stored.

Frequently asked questions

Join the discussion

No comments yet. Be the first to share your thoughts.