TExcelTAD
Home › Vite › Vite Preview

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

bash · try it yourself
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 preview is for checking a build locally. It is not designed to be a production web server. Deploy dist/ 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:

js
// 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:

bash · try it yourself
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:

js · try it yourself
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:

bash · try it yourself
du -sh dist
ls -la dist/assets
16K	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?

  1. 5173
  2. 4173
  3. 3000
  4. 8080

Is vite preview meant to serve your production site?

  1. Yes, it is a hardened production server
  2. No, it is only for checking the build locally
  3. Only behind nginx
  4. Only for SSR

Why doesn't opening dist/index.html via file:// usually work?

  1. The file is encrypted
  2. Module scripts and root-absolute paths need an HTTP server
  3. Browsers can't open HTML from disk
  4. Vite deletes it