Next.js - How to Test a Build Locally to Detect Differences
The Next.js equivalent to gatsby build && gatsby serve is:
npm run build && npm run startor if you're using pnpm/yarn:
pnpm build && pnpm start
# or
yarn build && yarn startThis works the same way as your Gatsby workflow:
npm run build(runsnext build) creates an optimized production buildnpm run start(runsnext start) serves that production build locally on http://localhost:3000 by default
This lets you test the production build locally to catch any differences between development and production mode before deploying. Common differences you might catch include:
- Client-side vs server-side rendering issues
- Environment variable problems (make sure you have the right
.env.productionor.env.localfiles) - Image optimization differences
- API route behavior
- Static generation vs server-side rendering issues
- Missing
generateStaticParamsor data fetching problems
If you're also running Sanity Studio alongside your Next.js app (like in a monorepo), you can test a production build of the Studio locally with npm run build in the Studio directory, as mentioned in the Sanity Studio development docs.
The production build is important to test because Next.js behaves quite differently in development mode (with features like Fast Refresh, more verbose error messages, and unoptimized bundles) compared to the optimized production build that gets deployed.
Sanity – Build the way you think, not the way your CMS thinks
Sanity is the developer-first content operating system that gives you complete control. Schema-as-code, GROQ queries, and real-time APIs mean no more workarounds or waiting for deployments. Free to start, scale as you grow.