Skip to main content

serve()

Serve a built VitePress site locally for preview. This is useful for testing the production build before deployment.

Usage

Function Signature

Parameters

ServeOptions
Configuration options for the preview server.
string
The root directory of your VitePress project. Defaults to process.cwd().This should be the directory containing the .vitepress folder.
string
Base public path to serve the site at. Overrides the base path from config.Example: /my-docs/
number
default:"4173"
Port number for the preview server.

Return Value

Polka
Returns a Polka server instance that can be used to stop the server or access server details.

Examples

Basic Preview Server

Custom Port

Custom Base Path

Custom Root Directory

Build and Serve

Programmatic Server Control

Server Features

The preview server includes:

Compression

Automatic Brotli and gzip compression for all responses.

Caching

Smart caching strategy:
  • Asset files (/assets/): Cached for 1 year with immutable flag
  • HTML files: No cache, always revalidated

SPA Fallback

Serves 404.html for non-asset routes that don’t match files, supporting client-side routing.

Static File Serving

Serves all static files from the build output directory (.vitepress/dist by default).

CLI Alternative

You can also use the VitePress CLI to preview your site:
With options:

Prerequisites

Before running the preview server, you must build your site:
Or programmatically:

Server Configuration

The preview server uses:

Error Handling

Production Deployment

While serve() is useful for local preview, for production deployment you should:
  1. Build your site with build()
  2. Deploy the .vitepress/dist folder to a static hosting service
Popular options:
  • Vercel
  • Netlify
  • GitHub Pages
  • CloudFlare Pages
  • AWS S3 + CloudFront