-
-
Notifications
You must be signed in to change notification settings - Fork 3.3k
astro preview ignores vite.preview.allowedHosts configuration #16088
Description
Astro Info
Astro v6.0.8
Package Manager pnpm
Platform linux (Docker, node:22)
Describe the Bug
astro preview completely ignores the allowedHosts configuration, whether it's set in astro.config.mjs via the vite field or in a standalone vite.config.js.
When accessing the preview server through a reverse proxy (Traefik) with a custom domain, Vite 6's host security check blocks the request:
Blocked request. This host ("example.com") is not allowed.
To allow this host, add "example.com" to `preview.allowedHosts` in vite.config.js.
None of the following configurations work:
astro.config.mjswithvite.preview.allowedHosts: trueastro.config.mjswithvite.server.allowedHosts: true- Standalone
vite.config.jswithpreview.allowedHosts: true
Only the CLI flag works:
astro preview --host 0.0.0.0 --port 3000 --allowed-hosts
This means the vite config merging in astro preview is broken for allowedHosts. The config is simply not being passed to the underlying Vite preview server.
Steps to reproduce
- Create an Astro project, build it
- Set
allowedHosts: truein either astro.config.mjs vite field or vite.config.js - Run
astro preview --host 0.0.0.0 - Access it via a reverse proxy with a different hostname
- Get 403 Forbidden
Expected Behavior
allowedHosts in config files should work the same as --allowed-hosts CLI flag. Users shouldn't have to dig through CLI flags to discover a workaround for a config option that is documented but silently ignored.
Honestly, this kind of issue makes me wonder if I should just write a plain Node.js static server myself — at least http.createServer doesn't pretend to read my config and then ignore it.
What's the workaround
Use the CLI flag --allowed-hosts instead of any config file approach:
{
"scripts": {
"start": "astro preview --host 0.0.0.0 --port 3000 --allowed-hosts"
}
}