diff --git a/.changeset/cyan-seals-bathe.md b/.changeset/cyan-seals-bathe.md new file mode 100644 index 000000000000..0e2f69f597b1 --- /dev/null +++ b/.changeset/cyan-seals-bathe.md @@ -0,0 +1,5 @@ +--- +"astro": minor +--- + +Allows passing a string to `--open` and `server.open` to open a specific URL on startup in development diff --git a/packages/astro/src/@types/astro.ts b/packages/astro/src/@types/astro.ts index 6a255c5734b7..dc47d3a63592 100644 --- a/packages/astro/src/@types/astro.ts +++ b/packages/astro/src/@types/astro.ts @@ -153,7 +153,7 @@ export interface CLIFlags { host?: string | boolean; port?: number; config?: string; - open?: boolean; + open?: string | boolean; } /** @@ -371,19 +371,21 @@ type ServerConfig = { /** * @name server.open - * @type {boolean} + * @type {string | boolean} * @default `false` * @version 2.1.8 * @description - * Control whether the dev server should open in your browser window on startup. + * Controls whether the dev server should open in your browser window on startup. + * + * Pass a full URL string (e.g. "http://example.com") or a pathname (e.g. "/about") to specify the URL to open. * * ```js * { - * server: { open: true } + * server: { open: "/about" } * } * ``` */ - open?: boolean; + open?: string | boolean; }; export interface ViteUserConfig extends vite.UserConfig { @@ -1020,16 +1022,19 @@ export interface AstroUserConfig { */ /** + * @docs * @name server.open - * @type {boolean} + * @type {string | boolean} * @default `false` * @version 2.1.8 * @description - * Control whether the dev server should open in your browser window on startup. + * Controls whether the dev server should open in your browser window on startup. + * + * Pass a full URL string (e.g. "http://example.com") or a pathname (e.g. "/about") to specify the URL to open. * * ```js * { - * server: { open: true } + * server: { open: "/about" } * } * ``` */ diff --git a/packages/astro/src/cli/flags.ts b/packages/astro/src/cli/flags.ts index c384d98671b8..d61f1e66d7df 100644 --- a/packages/astro/src/cli/flags.ts +++ b/packages/astro/src/cli/flags.ts @@ -19,7 +19,8 @@ export function flagsToAstroInlineConfig(flags: Flags): AstroInlineConfig { port: typeof flags.port === 'number' ? flags.port : undefined, host: typeof flags.host === 'string' || typeof flags.host === 'boolean' ? flags.host : undefined, - open: typeof flags.open === 'boolean' ? flags.open : undefined, + open: + typeof flags.open === 'string' || typeof flags.open === 'boolean' ? flags.open : undefined, }, }; } diff --git a/packages/astro/src/core/config/config.ts b/packages/astro/src/core/config/config.ts index 82bb872b1593..5284af1631a7 100644 --- a/packages/astro/src/core/config/config.ts +++ b/packages/astro/src/core/config/config.ts @@ -63,10 +63,11 @@ export function resolveFlags(flags: Partial): CLIFlags { site: typeof flags.site === 'string' ? flags.site : undefined, base: typeof flags.base === 'string' ? flags.base : undefined, port: typeof flags.port === 'number' ? flags.port : undefined, - open: typeof flags.open === 'boolean' ? flags.open : undefined, config: typeof flags.config === 'string' ? flags.config : undefined, host: typeof flags.host === 'string' || typeof flags.host === 'boolean' ? flags.host : undefined, + open: + typeof flags.open === 'string' || typeof flags.open === 'boolean' ? flags.open : undefined, }; } diff --git a/packages/astro/src/core/config/schema.ts b/packages/astro/src/core/config/schema.ts index 08910720a13e..0ea2d368a182 100644 --- a/packages/astro/src/core/config/schema.ts +++ b/packages/astro/src/core/config/schema.ts @@ -147,7 +147,10 @@ export const AstroConfigSchema = z.object({ // validate z .object({ - open: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.server.open), + open: z + .union([z.string(), z.boolean()]) + .optional() + .default(ASTRO_CONFIG_DEFAULTS.server.open), host: z .union([z.string(), z.boolean()]) .optional() @@ -464,12 +467,15 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: string) { // validate z .object({ + open: z + .union([z.string(), z.boolean()]) + .optional() + .default(ASTRO_CONFIG_DEFAULTS.server.open), host: z .union([z.string(), z.boolean()]) .optional() .default(ASTRO_CONFIG_DEFAULTS.server.host), port: z.number().optional().default(ASTRO_CONFIG_DEFAULTS.server.port), - open: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.server.open), headers: z.custom().optional(), streaming: z.boolean().optional().default(true), }) diff --git a/packages/astro/src/core/dev/container.ts b/packages/astro/src/core/dev/container.ts index 4f2b5d4f2fd5..d252e2eea534 100644 --- a/packages/astro/src/core/dev/container.ts +++ b/packages/astro/src/core/dev/container.ts @@ -54,10 +54,11 @@ export async function createContainer({ const { base, - server: { host, headers, open: shouldOpen }, + server: { host, headers, open: serverOpen }, } = settings.config; - // Open server to the correct path - const open = shouldOpen ? base : false; + // Open server to the correct path. We pass the `base` here as we didn't pass the + // base to the initial Vite config + const open = typeof serverOpen == 'string' ? serverOpen : serverOpen ? base : false; // The client entrypoint for renderers. Since these are imported dynamically // we need to tell Vite to preoptimize them.