Skip to content

Commit 2a8b9c5

Browse files
bluwysarah11918
andauthoredJan 3, 2024
Support --open with url string (#9573)
* Support --open with url string * Update jsdoc * Fix typo * Document server.open * Apply suggestions from code review Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca> --------- Co-authored-by: Sarah Rainsberger <sarah@rainsberger.ca>
1 parent 607303b commit 2a8b9c5

File tree

6 files changed

+34
-15
lines changed

6 files changed

+34
-15
lines changed
 

‎.changeset/cyan-seals-bathe.md

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"astro": minor
3+
---
4+
5+
Allows passing a string to `--open` and `server.open` to open a specific URL on startup in development

‎packages/astro/src/@types/astro.ts

+13-8
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,7 @@ export interface CLIFlags {
153153
host?: string | boolean;
154154
port?: number;
155155
config?: string;
156-
open?: boolean;
156+
open?: string | boolean;
157157
}
158158

159159
/**
@@ -371,19 +371,21 @@ type ServerConfig = {
371371

372372
/**
373373
* @name server.open
374-
* @type {boolean}
374+
* @type {string | boolean}
375375
* @default `false`
376376
* @version 2.1.8
377377
* @description
378-
* Control whether the dev server should open in your browser window on startup.
378+
* Controls whether the dev server should open in your browser window on startup.
379+
*
380+
* Pass a full URL string (e.g. "http://example.com") or a pathname (e.g. "/about") to specify the URL to open.
379381
*
380382
* ```js
381383
* {
382-
* server: { open: true }
384+
* server: { open: "/about" }
383385
* }
384386
* ```
385387
*/
386-
open?: boolean;
388+
open?: string | boolean;
387389
};
388390

389391
export interface ViteUserConfig extends vite.UserConfig {
@@ -1020,16 +1022,19 @@ export interface AstroUserConfig {
10201022
*/
10211023

10221024
/**
1025+
* @docs
10231026
* @name server.open
1024-
* @type {boolean}
1027+
* @type {string | boolean}
10251028
* @default `false`
10261029
* @version 2.1.8
10271030
* @description
1028-
* Control whether the dev server should open in your browser window on startup.
1031+
* Controls whether the dev server should open in your browser window on startup.
1032+
*
1033+
* Pass a full URL string (e.g. "http://example.com") or a pathname (e.g. "/about") to specify the URL to open.
10291034
*
10301035
* ```js
10311036
* {
1032-
* server: { open: true }
1037+
* server: { open: "/about" }
10331038
* }
10341039
* ```
10351040
*/

‎packages/astro/src/cli/flags.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,8 @@ export function flagsToAstroInlineConfig(flags: Flags): AstroInlineConfig {
1919
port: typeof flags.port === 'number' ? flags.port : undefined,
2020
host:
2121
typeof flags.host === 'string' || typeof flags.host === 'boolean' ? flags.host : undefined,
22-
open: typeof flags.open === 'boolean' ? flags.open : undefined,
22+
open:
23+
typeof flags.open === 'string' || typeof flags.open === 'boolean' ? flags.open : undefined,
2324
},
2425
};
2526
}

‎packages/astro/src/core/config/config.ts

+2-1
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,11 @@ export function resolveFlags(flags: Partial<Flags>): CLIFlags {
6363
site: typeof flags.site === 'string' ? flags.site : undefined,
6464
base: typeof flags.base === 'string' ? flags.base : undefined,
6565
port: typeof flags.port === 'number' ? flags.port : undefined,
66-
open: typeof flags.open === 'boolean' ? flags.open : undefined,
6766
config: typeof flags.config === 'string' ? flags.config : undefined,
6867
host:
6968
typeof flags.host === 'string' || typeof flags.host === 'boolean' ? flags.host : undefined,
69+
open:
70+
typeof flags.open === 'string' || typeof flags.open === 'boolean' ? flags.open : undefined,
7071
};
7172
}
7273

‎packages/astro/src/core/config/schema.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,10 @@ export const AstroConfigSchema = z.object({
147147
// validate
148148
z
149149
.object({
150-
open: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.server.open),
150+
open: z
151+
.union([z.string(), z.boolean()])
152+
.optional()
153+
.default(ASTRO_CONFIG_DEFAULTS.server.open),
151154
host: z
152155
.union([z.string(), z.boolean()])
153156
.optional()
@@ -464,12 +467,15 @@ export function createRelativeSchema(cmd: string, fileProtocolRoot: string) {
464467
// validate
465468
z
466469
.object({
470+
open: z
471+
.union([z.string(), z.boolean()])
472+
.optional()
473+
.default(ASTRO_CONFIG_DEFAULTS.server.open),
467474
host: z
468475
.union([z.string(), z.boolean()])
469476
.optional()
470477
.default(ASTRO_CONFIG_DEFAULTS.server.host),
471478
port: z.number().optional().default(ASTRO_CONFIG_DEFAULTS.server.port),
472-
open: z.boolean().optional().default(ASTRO_CONFIG_DEFAULTS.server.open),
473479
headers: z.custom<OutgoingHttpHeaders>().optional(),
474480
streaming: z.boolean().optional().default(true),
475481
})

‎packages/astro/src/core/dev/container.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -54,10 +54,11 @@ export async function createContainer({
5454

5555
const {
5656
base,
57-
server: { host, headers, open: shouldOpen },
57+
server: { host, headers, open: serverOpen },
5858
} = settings.config;
59-
// Open server to the correct path
60-
const open = shouldOpen ? base : false;
59+
// Open server to the correct path. We pass the `base` here as we didn't pass the
60+
// base to the initial Vite config
61+
const open = typeof serverOpen == 'string' ? serverOpen : serverOpen ? base : false;
6162

6263
// The client entrypoint for renderers. Since these are imported dynamically
6364
// we need to tell Vite to preoptimize them.

0 commit comments

Comments
 (0)
Please sign in to comment.