Why Your Next.js App Works Locally but Breaks in Production

Production is not a slower laptop. It is a different runtime, build, cache, and set of environment values.

By · · 8 min read · Next.js · Web Development

Start by classifying the failure: build-time, server runtime, browser runtime, or deployment routing. That one distinction prevents hours of checking the wrong layer.

Compare the environment deliberately

// Server-only secret: never prefix with NEXT_PUBLIC_
const apiKey = process.env.INTERNAL_API_KEY;

Check build-time versus request-time data

A value read during a static build will not change at request time unless the route is configured to revalidate or render dynamically. Log the build result and the server result separately.

Evidence first

Read deployment logs, response headers, and the browser console together. “It works locally” is a comparison, not a diagnosis.

Audit server/client boundaries

Browser APIs belong in client components. Secrets and private fetches belong on the server. A boundary that is tolerated by local development can fail during an optimized build.

Still stuck with a React or Next.js bug? See React & Next.js debugging help.

Related notes

How to Fix Hydration Errors in Next.js
React Component Not Re-rendering? Common Causes