vite preview: Serve the Production Build
vite preview boots a local static server that serves exactly what vite build produced in dist/.
preview is not a dev server: it serves the already-built assets, which makes it the right target for end-to-end smoke tests in CI after a build.
What it does
vite preview starts a lightweight static file server pointing at the build outDir. It mimics production asset serving (base path, hashed files) so Playwright or Cypress can hit a realistic build.
Common usage
vite build
vite preview --port 4173 --host
# in CI, run the server in the background then test against it
vite preview --port 4173 &
npx wait-on http://localhost:4173Options
| Flag | What it does |
|---|---|
| --port <n> | Port to listen on (default 4173) |
| --host | Expose on the network (needed inside containers) |
| --outDir <dir> | Directory to serve (default dist) |
| --strictPort | Fail instead of picking another port if taken |
| --base <path> | Base path to serve under |
In CI
Add --host so the server binds beyond localhost inside a container, and --strictPort so a port clash fails loudly instead of shifting to a port your tests do not know about. Run it backgrounded and gate the test step on the port being up.
Common errors in CI
"http://localhost:4173 refused to connect" from a test runner usually means preview bound to localhost only inside a container; add --host. A blank page with 404s for hashed assets means the --base at build time differs from where preview serves; keep them consistent.