Previewing Production Builds
What you'll learn: How to test your optimized production build locally before deploying it to the web.
Why Preview Production Builds?
When you run vite build, Vite creates an optimized production bundle with minified code, compressed assets, and performance enhancements. However, these optimizations can sometimes introduce subtle differences from your development environment. You need a way to test the actual production build locally before pushing it live.
Think of it like rehearsing a play in full costume and makeup before opening night—you want to catch any issues in realistic conditions, not just during casual practice.
Using vite preview
After building your project with vite build, you can use Vite's built-in preview server:
npm run build
npx vite preview
This command:
- Serves your production files from the
distfolder (or your configured output directory) - Runs a local HTTP server similar to what you'd experience on a real web host
- Lets you test minified code, code splitting, and lazy loading behavior
- Helps catch issues like broken asset paths or missing environment variables
What to Test
While previewing, verify:
- All pages load correctly with production paths
- Assets (images, fonts, CSS) appear properly
- Code splitting and lazy loading work as expected
- Environment variables resolve correctly
- No console errors from the minified code
The preview server is not meant for actual production hosting—it's a local testing tool. Once verified, deploy your dist folder to your real hosting provider.
Key Takeaway: vite preview lets you test your optimized production build locally, catching deployment issues before they reach real users.