Skip to content

astro preview ignores vite.preview.allowedHosts configuration #16088

@zeroxer

Description

@zeroxer

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:

  1. astro.config.mjs with vite.preview.allowedHosts: true
  2. astro.config.mjs with vite.server.allowedHosts: true
  3. Standalone vite.config.js with preview.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

  1. Create an Astro project, build it
  2. Set allowedHosts: true in either astro.config.mjs vite field or vite.config.js
  3. Run astro preview --host 0.0.0.0
  4. Access it via a reverse proxy with a different hostname
  5. 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"
  }
}

Metadata

Metadata

Assignees

Labels

- P3: minor bugAn edge case that only affects very specific usage (priority)pkg: astroRelated to the core `astro` package (scope)

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions