Skip to main content
The preview() function starts a local server to preview your production build. This helps you test your site locally before deploying it.
The JavaScript API is experimental and may change in future releases.
The preview server is not designed for production use. Always use a proper hosting platform for production deployments.

Import

Signature

Parameters

AstroInlineConfig
required
Configuration object for your Astro project. Supports all options from astro.config.mjs.
string
The root directory of your Astro project. Defaults to the current working directory.
'debug' | 'info' | 'warn' | 'error' | 'silent'
default:"'info'"
Controls the verbosity of logging output.
string
default:"'./dist'"
The directory containing your build output to preview.
object
Server configuration options.
number
default:"4321"
The port the preview server listens on.
string | boolean
default:"false"
Set which network IP addresses the server listens on. Set to true to listen on all addresses.

Return Value

Returns a Promise<PreviewServer> that resolves to a server object with the following properties:
string | undefined
The host the server is listening on.
number
The port the server is listening on.
() => Promise<void>
Stops the preview server and cleans up resources.
() => Promise<void>
Promise that resolves when the server is closed.

Examples

Basic Preview

Custom Port

Network Access

Preview with Custom Output Directory

Automated Testing

Preview and Test Script

Temporary Preview

SSR Preview

Preview with Logging

Error Handling

  • build() - Build for production
  • dev() - Start development server
  • sync() - Generate TypeScript types