Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Aastro preview doesn't respect vite preview allowedHostnames in astro config #13060

Open
1 task
andrewflbarnes opened this issue Jan 24, 2025 · 4 comments
Open
1 task
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) good first issue Good for newcomers. If you need additional guidance, feel free to post in #dev on Discord

Comments

@andrewflbarnes
Copy link

andrewflbarnes commented Jan 24, 2025

Astro Info

Astro                    v5.1.9
Node                     v22.6.0
System                   Linux (x64)
Package Manager          pnpm
Output                   static
Adapter                  none
Integrations             @astrojs/starlight

If this issue only occurs in one browser, which browser is a problem?

No response

Describe the Bug

When running astro preview and visiting the site from another device via the hostname I am met with the below message

Blocked request. This host ("my-hostname") is not allowed.
To allow this host, add "my-hostname" to `preview.allowedHosts` in vite.config.js.

Updating the corresponding part of astro.config.mjs (vite.preview.allowedHosts) does not work, nor does setting it to true or adding a vite.config.js file with this set.

The corresponding property for astro dev (vite.server.allowedHosts) does work.

Note: not reproducible on stackblitz (probably because it forwards direct to the IP).
Can be done with pnpm astro create on the starlight template (unable to check basic template at the moment but suspect it's an issue there too).

astro config:

 export default defineConfig({
   site: "http://my-hostname:54321",
   vite: {
     server: {
       allowedHosts: true,
     },
     preview: {
       allowedHosts: true,
     },
   },
...

package.json scripts

    "dev": "astro dev --port 54321 --host",
    "preview": "astro preview --port 54321 --host",

What's the expected result?

astro preview respects astro config vite.preview.allowedHosts - when I add "my-hostname"/true to this setting I am able to visit the site in a browser at http://my-hostname

Link to Minimal Reproducible Example

https://github.com/andrewflbarnes/bug-astro-preview-allowedhosts

pnpm i
pnpm build
pnpm preview

curl yourhostname:54321
Blocked request. This host ("yourhostname") is not allowed.
To allow this host, add "yourhostname" to `preview.allowedHosts` in vite.config.js.

Participation

  • I am willing to submit a pull request for this issue.
@github-actions github-actions bot added the needs triage Issue needs to be triaged label Jan 24, 2025
@ematipico
Copy link
Member

ematipico commented Jan 24, 2025

astro preview doesn't use vite under the hoods, you would need to provide those hosts in a different way.

Also, please provide a reproduction. Even if it doesn't work on Stackblitz, we can download it and run it ourselves

@ematipico ematipico added the needs repro Issue needs a reproduction label Jan 24, 2025
Copy link
Contributor

Hello @andrewflbarnes. Please provide a minimal reproduction using a GitHub repository or StackBlitz. Issues marked with needs repro will be closed if they have no activity within 3 days.

@github-actions github-actions bot removed the needs triage Issue needs to be triaged label Jan 24, 2025
@andrewflbarnes
Copy link
Author

Thanks for the response @ematipico

astro preview doesn't use vite under the hoods

Should I rename this ticket to something like "incorrect astro preview errors and intellisense for vite preview" given the below?

  • the error astro preview reports when visiting the page is to add "yourhostname" to preview.allowedHosts in vite.config.js.
  • there is intellisense available on the astro config object for vite.preview... (which aligns with the above error message)

you would need to provide those hosts in a different way.

What is the astro configuration which does this?

I've had a look through the astro docs and done some googling but wasn't able to find any astro config which seemed to address this.

Very possible I missed it since was searching for "allowedHosts" which may have been a red herring given the error message is wrong.

@ematipico ematipico added needs triage Issue needs to be triaged and removed needs repro Issue needs a reproduction labels Jan 27, 2025
@ematipico
Copy link
Member

Yeah, I suppose the bug is that we don't use any preview options on purpose, so maybe we should remove vite.preview from the intellisense

@ematipico ematipico added - P3: minor bug An edge case that only affects very specific usage (priority) good first issue Good for newcomers. If you need additional guidance, feel free to post in #dev on Discord labels Jan 27, 2025
@github-actions github-actions bot removed the needs triage Issue needs to be triaged label Jan 27, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
- P3: minor bug An edge case that only affects very specific usage (priority) good first issue Good for newcomers. If you need additional guidance, feel free to post in #dev on Discord
Projects
None yet
Development

No branches or pull requests

2 participants