Vite Preview
After vite build you have a dist/ folder. Opening dist/index.html by double-clicking usually doesn't work — module scripts are blocked on file:// URLs and absolute paths like /assets/... point to your disk root. You need a real HTTP server. vite preview is exactly that: a small static server for your build.
Build, then preview
npm run build
npm run preview> my-app@0.0.0 build
> vite build
vite v8.3.4 building client environment for production...
✓ 5 modules transformed.
dist/index.html 0.46 kB │ gzip: 0.30 kB
dist/assets/index-DiwrgTda.css 1.24 kB │ gzip: 0.63 kB
dist/assets/index-C3k9pQ2x.js 3.10 kB │ gzip: 1.48 kB
✓ built in 140ms
> my-app@0.0.0 preview
> vite preview
➜ Local: http://localhost:4173/
➜ Network: use --host to expose
➜ press h + enter to show help
Open http://localhost:4173/. You're now running the real production bundle: minified, hashed, no HMR, import.meta.env.PROD === true.
Warning:
vite previewis for checking a build locally. It is not designed to be a production web server. Deploydist/to a static host, a CDN, or nginx instead.
Why preview matters
Some bugs only appear in production builds:
| Symptom | Typical cause |
|---|---|
Blank page, 404 for /assets/index-*.js |
Wrong base for the deployment path |
Code behind if (import.meta.env.DEV) missing |
Intended — but maybe you relied on it |
| A library breaks only after build | CommonJS interop or a side-effectful import removed by tree-shaking |
| Image works in dev but 404s in build | Path built at runtime as a string instead of imported / new URL(...) |
Env variable undefined |
Missing VITE_ prefix or not set for the production mode |
Preview options
The preview config section mirrors server:
// vite.config.js
import { defineConfig } from 'vite';
export default defineConfig({
preview: {
port: 8080,
strictPort: true,
host: true, // test the build from your phone
open: true,
proxy: {
'/api': 'http://localhost:3000', // same API routing as in dev
},
},
});Or with flags:
npx vite preview --port 8080 --host ➜ Local: http://localhost:8080/
➜ Network: http://192.168.1.24:8080/
➜ press h + enter to show help
vite preview --outDir build previews a different folder if you changed build.outDir.
SPA fallback
For a single-page app with client-side routing, a URL like /courses/vite has no matching file in dist/. The preview server falls back to index.html for HTML requests, so your router can handle the path — just like you will configure nginx to do later. The playground models that decision:
const files = new Set(['/index.html', '/assets/index-C3k9pQ2x.js', '/favicon.svg', '/about/index.html']);
function serve(url, accept = 'text/html') {
if (files.has(url)) return `200 ${url}`;
if (url.endsWith('/') && files.has(url + 'index.html')) return `200 ${url}index.html`;
if (accept.includes('text/html')) return `200 /index.html (SPA fallback)`;
return '404';
}
console.log('/ ', serve('/'));
console.log('/about/ ', serve('/about/'));
console.log('/courses/vite ', serve('/courses/vite'));
console.log('/assets/missing.js', serve('/assets/missing.js', '*/*'));/ 200 /index.html
/about/ 200 /about/index.html
/courses/vite 200 /index.html (SPA fallback)
/assets/missing.js 404
Missing assets still return 404 — only page navigations fall back. That's important: you don't want a missing script to silently receive HTML.
Inspecting the build output
Since the build is just files, you can explore it with ordinary tools:
du -sh dist
ls -la dist/assets16K dist
total 16
drwxr-xr-x 2 me me 4096 Oct 16 10:12 .
drwxr-xr-x 3 me me 4096 Oct 16 10:12 ..
-rw-r--r-- 1 me me 3104 Oct 16 10:12 index-C3k9pQ2x.js
-rw-r--r-- 1 me me 1240 Oct 16 10:12 index-DiwrgTda.css
Tip: Add a combined script so you never forget to rebuild:
"serve:prod": "vite build && vite preview".
Checking what the browser sees
The hashed JS is minified, but you can still verify behaviour in DevTools: the Network tab shows the files and their sizes, and enabling build.sourcemap: true lets you debug original sources in a preview session.
Test yourself
What is the default port of vite preview?
- 5173
- 4173
- 3000
- 8080
Is vite preview meant to serve your production site?
- Yes, it is a hardened production server
- No, it is only for checking the build locally
- Only behind nginx
- Only for SSR
Why doesn't opening dist/index.html via file:// usually work?
- The file is encrypted
- Module scripts and root-absolute paths need an HTTP server
- Browsers can't open HTML from disk
- Vite deletes it