Previewing the Production Build
What you'll learn: How to test your production-ready React app locally before deploying it to catch build-specific issues.
Why Preview Matters
You've just run npm run build and created an optimized production bundle. But here's the problem: your development environment (npm run dev) behaves differently than production. Features like HMR, source maps, and development warnings won't exist in production. Some bugs only appear after the build process optimizes your code.
Think of it like test-driving a car after assembly. You wouldn't ship a vehicle straight from the factory floor without checking if everything works together properly. Similarly, you shouldn't deploy your built app without testing it first.
The Preview Command
Vite provides a simple command to serve your production build locally:
npm run preview
This command:
- Serves the contents of your
distfolder (created bynpm run build) - Runs a local server similar to production hosting
- Shows you exactly what users will experience
What to Check
When previewing, look for:
- Broken imports: Sometimes relative paths work in dev but fail in production
- Missing assets: Images or files that didn't copy correctly to
dist - API endpoint issues: Environment variables that differ between dev and production
- Route problems: Client-side routing that works in dev but breaks on static hosts
The Workflow
- Make changes to your code
- Run
npm run build - Run
npm run preview - Test thoroughly in your browser
- Fix any issues, then repeat
Key Takeaway: Always preview your production build locally with npm run preview before deploying—it catches build-specific bugs that development mode hides.