Overview
The preview command allows you to test your production build locally before deploying it to a live environment. This is a critical step to catch any issues that might only appear in production builds.Preview Command
After building your application withnpm run build, start the preview server:
package.json:
package.json
You must run
npm run build before using npm run preview. The preview server serves the built files from the dist/ directory.How Preview Works
Build the Application
First, create a production build:This generates optimized files in the
dist/ directory.Start Preview Server
Start the local preview server:The server typically starts on
http://localhost:4173.Preview Server Details
Thevite preview command starts a local static file server that serves your production build:
- Default port: 4173
- Serves from:
dist/directory - MIME types: Proper content-type headers for all assets
- HTTP only: No HTTPS by default (can be configured)
Terminal Output
When to Use Preview vs Dev
Development Server
Use for active development
- Fast Hot Module Replacement
- Source maps for debugging
- Development-only warnings
- Not optimized
- Runs on port 5173
Preview Server
Use for testing production builds
- Serves optimized build
- Production environment
- Tests actual deployment code
- Minified and bundled
- Runs on port 4173
What to Test in Preview
When testing your production build, verify:Functionality
- All routes and navigation work correctly
- Forms submit and validate properly
- API calls function as expected
- State management behaves correctly
- Authentication/authorization flows work
Performance
- Page load times are acceptable
- Images and assets load quickly
- No console errors or warnings
- Network requests are optimized
- Code splitting works as expected
Visual Appearance
- Tailwind CSS styles are applied correctly
- Responsive design works on different screen sizes
- Images and fonts display properly
- No CSS conflicts or missing styles
Common Issues in Preview
Issue: Routes Return 404
If your app uses client-side routing (React Router), you may need to configure the server to serveindex.html for all routes.
Solution for deployment:
- Most hosting platforms (Vercel, Netlify) handle this automatically
- For custom servers, configure fallback to
index.html
Issue: Environment Variables Not Working
Only environment variables prefixed withVITE_ are exposed to the client:
.env.production
Issue: Assets Not Loading
If assets fail to load, check:- Base path configuration in
vite.config.ts - Asset imports are using Vite’s import syntax
- Public assets are in the
public/directory
vite.config.ts
Issue: Different Behavior Than Development
Some features behave differently in production:- React strict mode: May have different effects
- Source maps: Not available by default
- Console warnings: Development-only warnings are removed
- Performance: Much faster due to optimization
Configuring Preview Server
You can customize the preview server invite.config.ts:
vite.config.ts
port: Change the preview server portstrictPort: Exit if port is already in usehost: Listen on all addresses (useful for network testing)open: Automatically open browser
Testing Checklist
Before deploying to production, verify:The preview server is not designed for production use. It’s only for local testing. Use a proper hosting platform for production deployment.
Next Steps
Once you’ve verified your build works correctly in preview:Build Documentation
Learn more about the build process
Deploy to Production
Deploy your application to a hosting platform
