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

feat: add new allowedHosts option #13278

Merged
merged 8 commits into from
Feb 26, 2025
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions .changeset/grumpy-sloths-fail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
---
'astro': minor
---

Adds a new option in the config called `server.allowedHosts` and CLI option called `--allowed-hosts`.

This new option allows to specify the hostnames that the dev server and preview server are allowed to respond to.

```shell
astro dev --allowed-hosts=foo.bar.example.com,bar.example.com
```

```shell
astro preview --allowed-hosts=foo.bar.example.com,bar.example.com
```

```js
// astro.config.mjs
import {defineConfig} from "astro/config";

export default defineConfig({
server: {
allowedHosts: ['foo.bar.example.com', 'bar.example.com']
}
})
```

More information are available in the [Vite documentation](https://vite.dev/config/server-options.html#server-allowedhosts).
1 change: 1 addition & 0 deletions packages/astro/src/cli/dev/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ export async function dev({ flags }: DevOptions) {
['--host <custom-address>', `Expose on a network IP address at <custom-address>`],
['--open', 'Automatically open the app in the browser on server start'],
['--force', 'Clear the content layer cache, forcing a full rebuild.'],
['--allowed-hosts', 'Specify a comma-separated list of allowed hosts or allow any hostname.'],
['--help (-h)', 'See all available flags.'],
],
},
Expand Down
6 changes: 6 additions & 0 deletions packages/astro/src/cli/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ export function flagsToAstroInlineConfig(flags: Flags): AstroInlineConfig {
typeof flags.host === 'string' || typeof flags.host === 'boolean' ? flags.host : undefined,
open:
typeof flags.open === 'string' || typeof flags.open === 'boolean' ? flags.open : undefined,
allowedHosts:
typeof flags.allowedHosts === 'string'
? flags.allowedHosts.split(',')
: typeof flags.allowedHosts === 'boolean' && flags.allowedHosts === true
? flags.allowedHosts
: [],
},
};
}
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/cli/preview/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export async function preview({ flags }: PreviewOptions) {
['--host', `Listen on all addresses, including LAN and public addresses.`],
['--host <custom-address>', `Expose on a network IP address at <custom-address>`],
['--open', 'Automatically open the app in the browser on server start'],
['--allowed-hosts', 'Specify a comma-separated list of allowed hosts or allow any hostname.'],
['--help (-h)', 'See all available flags.'],
],
},
Expand Down
9 changes: 9 additions & 0 deletions packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ export const ASTRO_CONFIG_DEFAULTS = {
host: false,
port: 4321,
open: false,
allowedHosts: [],
},
integrations: [],
markdown: markdownConfigDefaults,
Expand Down Expand Up @@ -214,6 +215,10 @@ export const AstroConfigSchema = z.object({
.default(ASTRO_CONFIG_DEFAULTS.server.host),
port: z.number().optional().default(ASTRO_CONFIG_DEFAULTS.server.port),
headers: z.custom<OutgoingHttpHeaders>().optional(),
allowedHosts: z
.union([z.array(z.string()), z.literal(true)])
.optional()
.default(ASTRO_CONFIG_DEFAULTS.server.allowedHosts),
})
.default({}),
),
Expand Down Expand Up @@ -718,6 +723,10 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: string) {
port: z.number().optional().default(ASTRO_CONFIG_DEFAULTS.server.port),
headers: z.custom<OutgoingHttpHeaders>().optional(),
streaming: z.boolean().optional().default(true),
allowedHosts: z
.union([z.array(z.string()), z.literal(true)])
.optional()
.default(ASTRO_CONFIG_DEFAULTS.server.allowedHosts),
})
.optional()
.default({}),
Expand Down
4 changes: 2 additions & 2 deletions packages/astro/src/core/dev/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ export async function createContainer({

const {
base,
server: { host, headers, open: serverOpen },
server: { host, headers, open: serverOpen, allowedHosts },
} = settings.config;

// serverOpen = true, isRestart = false
Expand Down Expand Up @@ -92,7 +92,7 @@ export async function createContainer({
const mode = inlineConfig?.mode ?? 'development';
const viteConfig = await createVite(
{
server: { host, headers, open },
server: { host, headers, open, allowedHosts },
optimizeDeps: {
include: rendererClientEntries,
},
Expand Down
1 change: 1 addition & 0 deletions packages/astro/src/core/preview/static-preview-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ export default async function createStaticPreviewServer(
port: settings.config.server.port,
headers: settings.config.server.headers,
open: settings.config.server.open,
allowedHosts: settings.config.server.allowedHosts
},
plugins: [vitePluginAstroPreview(settings)],
});
Expand Down
20 changes: 20 additions & 0 deletions packages/astro/src/types/public/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,26 @@ export type ServerConfig = {
* ```
*/
open?: string | boolean;

/**
* @name server.allowedHosts
* @type {string[] | true}
* @default `[]`
* @version 5.4.0
* @description
*
* A list of hostnames that Astro is allowed to respond to. When the value is set to `true`, any
* hostname is allowed.
*
* ```js
* {
* server: {
* allowedHosts: ['staging.example.com', 'qa.example.com']
* }
* }
* ```
*/
allowedHosts?: string[] | true;
};

export type SessionDriverName = BuiltinDriverName | 'custom' | 'test';
Expand Down
Loading