Vite Live Preview Plugin
raw JSON →vite-live-preview is a Vite plugin designed to provide a live preview server specifically for Vite builds executed with the `--watch` flag. Unlike the standard `vite dev` server, which prioritizes Hot Module Replacement (HMR) for rapid development, this plugin focuses on serving a production-like build output, automatically reloading the browser page whenever a rebuild completes. The current stable version is 0.4.0, and the project appears actively maintained on GitHub, with recent updates. A key differentiator is its explicit non-support for HMR, making it suitable for scenarios where a full page reload is acceptable or preferred, or when simulating a built artifact in a watch mode. It allows for detailed configuration of the preview server, including port settings, reload delays, and handling of complex network setups like reverse proxies via a `clientPort` option. Developers should note that Vite plugins configured in the main `vite.config.ts` are not automatically inherited by the preview server and must be re-added via the plugin's `config` option if needed.
Common errors
error HMR not working / Changes not updating instantly ↓
npx vite dev). vite-live-preview is for watchable builds with full page reloads. error My Vite plugins (e.g., Vue plugin, React plugin) are not active in the preview server. ↓
config option of the preview() plugin, e.g., preview({ config: { plugins: [vue()] } }). error Vite build completes, but no preview server starts. ↓
npx vite build --watch to start both the build watcher and the preview server. error WebSocket connection failed during reload or 'vite:ws:disconnect' error in console. ↓
reload.clientPort option in your vite-live-preview plugin settings to specify the correct port for the WebSocket client to connect to. Warnings
breaking `vite-live-preview` explicitly does NOT support Hot Module Replacement (HMR). It is designed for full page reloads on rebuilds, not instantaneous HMR updates. ↓
gotcha Vite plugins defined in your main `vite.config.ts` are NOT automatically inherited by the `vite-live-preview` server. If your preview server requires specific plugins (e.g., for asset serving or transformations), you must re-add them within the `preview` plugin's `config` option. ↓
gotcha The `vite-live-preview` server only starts when `vite build --watch` is executed. Using `vite build` (without `--watch`) or `vite dev` will not activate the preview server. ↓
gotcha When running behind a reverse proxy or in complex network environments, the WebSocket connection for automatic reloading might fail due to incorrect port mapping. The `clientPort` option addresses this. ↓
Install
npm install vite-live-preview yarn add vite-live-preview pnpm add vite-live-preview Imports
- defineConfig wrong
const { defineConfig } = require('vite')correctimport { defineConfig } from 'vite' - preview wrong
import { preview } from 'vite-live-preview'correctimport preview from 'vite-live-preview' - UserConfig
import type { UserConfig } from 'vite'
Quickstart
import { defineConfig } from 'vite';
import preview from 'vite-live-preview';
export default defineConfig({
// Configure the base path for your application if deployed to a sub-directory
base: '/my-app/',
plugins: [
preview({
debug: true, // Enable debug logging for the preview server
reload: {
enabled: true,
delay: 1500, // Wait a bit longer after rebuilds before reloading
// clientPort: 80, // Uncomment if behind a reverse proxy that remaps websocket ports
},
// If specific Vite plugins are needed for the *preview server itself*
// (e.g., for certain asset serving or transformations), re-add them here.
// Most build-time plugins are not necessary for previewing.
config: {
// plugins: [somePreviewOnlyPlugin()],
},
}),
],
// Standard Vite preview options are inherited here
preview: {
port: 8080, // Configure the port for the live preview server
strictPort: true, // Ensure the specified port is used, or exit
open: true, // Automatically open the browser when the server starts
},
});
// To install dependencies:
// npm install --save-dev vite vite-live-preview
// To start the build with live preview:
// npx vite build --watch