Astro preview --background: Run a Built Site Without Holding the Terminal
If astro preview keeps a terminal occupied while you inspect a production build, Astro 7.2.0 adds a lifecycle for running that preview in the background. Use --background to start the server, then use status, logs, or stop instead of managing a process by hand.
The important boundary is that preview still serves the last generated build. Run astro build first; --background changes process management, not the build or deployment model.
Start and inspect the background preview
From a project that has Astro 7.2.0 or a later release, build the site and start its preview server:
pnpm buildpnpm exec astro preview --backgroundThe command returns after the preview is ready, so another command can run in the same terminal. Check whether the process is running and inspect its output with the companion commands:
pnpm exec astro preview statuspnpm exec astro preview logspnpm exec astro preview logs --followWhen the check is finished, stop the background process through Astro’s own lifecycle:
pnpm exec astro preview stopUsing the CLI for the lifecycle makes the state visible to a developer or coding agent. It also avoids leaving an old preview process running after the generated dist/ directory has changed.
Understand what preview is serving
astro preview is a local check of the output produced by astro build. It does not watch source files or rebuild pages when you edit them. After changing content or configuration, run the build again before inspecting the new result:
pnpm buildpnpm exec astro preview --backgroundIf the command is unknown, check the installed version rather than debugging the port first:
pnpm exec astro --versionThe background preview lifecycle was added in Astro 7.2.0. A project pinned to an older Astro release should continue using its existing astro preview flow or upgrade in a deliberate dependency change. Do not copy a new CLI flag into a lockfile that still resolves an older binary.
Use the opt-out when automation should own the process
Astro can automatically enable background mode when it detects an AI coding agent. If an automation runner expects astro preview to remain attached to its process group, disable that automatic behavior with the documented environment variable:
ASTRO_PREVIEW_BACKGROUND=0 pnpm exec astro previewUse this opt-out when the surrounding tool already provides process supervision, timeout handling, or log collection. When you intentionally want a detached local preview, prefer the explicit --background command and keep the later status, logs, and stop calls in the same workflow.
Do not treat it as a production server
Background mode is a convenience for checking a built site locally or in a short-lived verification job. It does not add an Astro adapter, a process supervisor, TLS termination, or deployment health checks. A static deployment still needs a web server or hosting platform to serve the generated files; the Astro static site with Caddy and Docker guide covers that separate runtime boundary.
For a CI check, keep the sequence explicit:
- Install the lockfile’s dependencies.
- Run the production build.
- Start the preview only if the test needs HTTP responses.
- Collect logs if a route check fails.
- Stop the preview process in cleanup.
This ordering distinguishes a broken build from a preview-process problem. It also prevents a successful server start from hiding stale output in dist/.
FAQ
Does astro preview --background rebuild the site when files change?
No. It serves the existing build output. Run astro build again, then restart or inspect the preview process so it serves the new dist/ contents.
How do I stop an Astro background preview?
Run pnpm exec astro preview stop from the same project context. Use pnpm exec astro preview status first if you need to confirm which preview process is active.
Why does Astro preview still block my automation?
The installed Astro version may not include the background lifecycle, or the workflow may be using the normal foreground command. Check pnpm exec astro --version, then choose the explicit background mode or set ASTRO_PREVIEW_BACKGROUND=0 when the runner must own the foreground process.
References:
Report a typo or broken link, or suggest a related topic.