diff --git a/code/core/src/bin/core.ts b/code/core/src/bin/core.ts index 9ce0413d9bcb..2a61d3753c2a 100644 --- a/code/core/src/bin/core.ts +++ b/code/core/src/bin/core.ts @@ -97,6 +97,7 @@ command('dev') .option('--smoke-test', 'Exit after successful start') .option('--ci', "CI mode (skip interactive prompts, don't open browser)") .option('--no-open', 'Do not open Storybook automatically in the browser') + .option('--open-url ', 'Open a custom URL instead of the default local address') .option('--quiet', 'Suppress verbose build output') .option('--no-version-updates', 'Suppress update check', true) .option('--debug-webpack', 'Display final webpack configurations for debugging purposes') @@ -134,6 +135,10 @@ command('dev') ci: 'CI', }); + if (!options.openUrl && process.env.STORYBOOK_OPEN_URL) { + options.openUrl = process.env.STORYBOOK_OPEN_URL; + } + if (parseInt(`${options.port}`, 10)) { options.port = parseInt(`${options.port}`, 10); } diff --git a/code/core/src/core-server/dev-server.ts b/code/core/src/core-server/dev-server.ts index 1841a15c4b02..2051b7db853e 100644 --- a/code/core/src/core-server/dev-server.ts +++ b/code/core/src/core-server/dev-server.ts @@ -186,9 +186,15 @@ export async function storybookDevServer( const [indexGenerator] = await Promise.all([storyIndexGeneratorPromise, listening]); if (indexGenerator && !options.ci && !options.smokeTest && options.open) { - const url = options.host ? options.networkAddress : options.localAddress; - openInBrowser(options.previewOnly ? `${url}iframe.html?navigator=true` : url!).catch(() => { - // the browser window could not be opened, this is non-critical, we just ignore the error + const defaultUrl = options.host ? options.networkAddress : options.localAddress; + const url = + options.openUrl || + (options.previewOnly ? `${defaultUrl}iframe.html?navigator=true` : defaultUrl!); + // the browser window could not be opened, this is non-critical, we just ignore the error + openInBrowser(url).catch((err: Error) => { + if (options.openUrl) { + logger.warn(`Failed to open custom URL: ${url}\n ${err.message}`); + } }); } } catch (e) { diff --git a/code/core/src/types/modules/core-common.ts b/code/core/src/types/modules/core-common.ts index f1ed67e754f9..47b412d54aa8 100644 --- a/code/core/src/types/modules/core-common.ts +++ b/code/core/src/types/modules/core-common.ts @@ -196,6 +196,7 @@ export interface CLIOptions extends CLIBaseOptions { smokeTest?: boolean; managerCache?: boolean; open?: boolean; + openUrl?: string; ci?: boolean; versionUpdates?: boolean; docs?: boolean; diff --git a/docs/api/cli-options.mdx b/docs/api/cli-options.mdx index 64d1830b5421..9bc9cd44b83e 100644 --- a/docs/api/cli-options.mdx +++ b/docs/api/cli-options.mdx @@ -53,6 +53,7 @@ Options include: | `--smoke-test` | Exit after successful start.
`storybook dev --smoke-test` | | `--ci` | CI mode (skip interactive prompts, don't open browser).
`storybook dev --ci` | | `--no-open` | Do not open Storybook automatically in the browser.
`storybook dev --no-open` | +| `--open-url ` | Open a custom URL instead of the default local address. Useful when running a local HTTPS reverse proxy. Can also be set via the `STORYBOOK_OPEN_URL` environment variable (CLI flag takes precedence). Note: `--no-open` disables opening entirely and overrides both `--open-url` and the environment variable.
`storybook dev --open-url https://myapp.localhost` | | `--quiet` | Suppress verbose build output.
`storybook dev --quiet` | | `--debug` | Outputs more logs in the CLI to assist debugging.
`storybook dev --debug` | | `--debug-webpack` | Display final webpack configurations for debugging purposes.
`storybook dev --debug-webpack` |