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

Support --open with url string #9573

Merged
merged 6 commits into from
Jan 3, 2024
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
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
17 changes: 11 additions & 6 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.
* A full URL string (e.g. "http://example.com") or a pathname (e.g. "/about") can also be passed
* to specify the URL to open.
bluwy marked this conversation as resolved.
Show resolved Hide resolved
*
* ```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.
* A full URL string (e.g. "http://example.com") or a pathname (e.g. "/about") can also be passed
* to specify the URL to open.
bluwy marked this conversation as resolved.
Show resolved Hide resolved
*
* ```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
Loading