Skip to content

Commit

Permalink
Support --open with url string (#9573)
Browse files Browse the repository at this point in the history
* Support --open with url string

* Update jsdoc

* Fix typo

* Document server.open

* Apply suggestions from code review

Co-authored-by: Sarah Rainsberger <[email protected]>

---------

Co-authored-by: Sarah Rainsberger <[email protected]>
  • Loading branch information
bluwy and sarah11918 authored Jan 3, 2024
1 parent 607303b commit 2a8b9c5
Show file tree
Hide file tree
Showing 6 changed files with 34 additions and 15 deletions.
5 changes: 5 additions & 0 deletions .changeset/cyan-seals-bathe.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"astro": minor
---

Allows passing a string to `--open` and `server.open` to open a specific URL on startup in development
21 changes: 13 additions & 8 deletions packages/astro/src/@types/astro.ts
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ export interface CLIFlags {
host?: string | boolean;
port?: number;
config?: string;
open?: boolean;
open?: string | boolean;
}

/**
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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" }
* }
* ```
*/
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/cli/flags.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
};
}
Expand Down
3 changes: 2 additions & 1 deletion packages/astro/src/core/config/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,10 +63,11 @@ export function resolveFlags(flags: Partial<Flags>): 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,
};
}

Expand Down
10 changes: 8 additions & 2 deletions packages/astro/src/core/config/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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<OutgoingHttpHeaders>().optional(),
streaming: z.boolean().optional().default(true),
})
Expand Down
7 changes: 4 additions & 3 deletions packages/astro/src/core/dev/container.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 2a8b9c5

Please sign in to comment.