Skip to content

feat(cli): support --host and --open on preview command (closes #367)#835

Open
50rayn wants to merge 1 commit intohistoire-dev:mainfrom
50rayn:fix/cli-host-preview
Open

feat(cli): support --host and --open on preview command (closes #367)#835
50rayn wants to merge 1 commit intohistoire-dev:mainfrom
50rayn:fix/cli-host-preview

Conversation

@50rayn
Copy link
Copy Markdown
Contributor

@50rayn 50rayn commented May 1, 2026

Closes #367.

histoire preview now accepts --host and --open, matching histoire dev. Implementation delegates to Vite's built-in preview() so host binding, network URL printing, browser opening, and port retry are handled by Vite directly.

Verified end-to-end on examples/vue3:

preview --host                → Local: localhost  + Network URLs
preview --host 0.0.0.0        → Local: localhost  + Network URLs
preview --host 127.0.0.1      → Local: 127.0.0.1
preview                       → Local: localhost  + "use --host to expose"

Also tightened dev's --host <host> to --host [host] so the value is explicitly optional in the help text (behavior unchanged — sade already handled the no-value case).

Test plan

  • pnpm build and pnpm test pass
  • Manually verified all four histoire preview invocations above against the vue3 example
  • Manually verified histoire dev --host, --host 127.0.0.1 still work
  • build.ts's internal startPreview({}, ctx) (used for screenshot rendering) still works

@bolt-new-by-stackblitz
Copy link
Copy Markdown

Review PR in StackBlitz Codeflow Run & review this pull request in StackBlitz Codeflow.

@netlify
Copy link
Copy Markdown

netlify Bot commented May 1, 2026

Deploy Preview for histoire-examples-svelte3 ready!

Name Link
🔨 Latest commit 7f7a29c
🔍 Latest deploy log https://app.netlify.com/projects/histoire-examples-svelte3/deploys/69f50513b81967000827cf59
😎 Deploy Preview https://deploy-preview-835--histoire-examples-svelte3.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify
Copy link
Copy Markdown

netlify Bot commented May 1, 2026

Deploy Preview for histoire-examples-vue3 ready!

Name Link
🔨 Latest commit 7f7a29c
🔍 Latest deploy log https://app.netlify.com/projects/histoire-examples-vue3/deploys/69f505130a6be200086101bb
😎 Deploy Preview https://deploy-preview-835--histoire-examples-vue3.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify
Copy link
Copy Markdown

netlify Bot commented May 1, 2026

Deploy Preview for histoire-site ready!

Name Link
🔨 Latest commit 7f7a29c
🔍 Latest deploy log https://app.netlify.com/projects/histoire-site/deploys/69f505133bc9d10008cebd7d
😎 Deploy Preview https://deploy-preview-835--histoire-site.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

@netlify
Copy link
Copy Markdown

netlify Bot commented May 1, 2026

Deploy Preview for histoire-controls ready!

Name Link
🔨 Latest commit 7f7a29c
🔍 Latest deploy log https://app.netlify.com/projects/histoire-controls/deploys/69f50513ccb97b0008464072
😎 Deploy Preview https://deploy-preview-835--histoire-controls.netlify.app
📱 Preview on mobile
Toggle QR Code...

QR Code

Use your smartphone camera to open QR code link.
🤖 Make changes Run an agent on this branch

To edit notification comments on pull requests, go to your Netlify project configuration.

Copy link
Copy Markdown
Contributor

@gemini-code-assist gemini-code-assist Bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

This pull request refactors the preview server implementation to utilize Vite's built-in preview functionality, replacing the previous custom server setup using connect and sirv. It also updates the CLI to support --open and --host options for the preview command. Feedback was provided to ensure that the new implementation respects existing Vite configuration settings for the preview server and to improve the robustness of the base URL resolution when specific network hosts are configured.

Comment on lines +17 to +31
const port = options.port ?? 6006
const server = await preview({
root: ctx.root,
base: ctx.resolvedViteConfig.base,
configFile: false,
build: {
outDir: ctx.config.outDir,
},
preview: {
port,
host: options.host,
open: options.open,
strictPort: false,
},
})
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The current implementation hardcodes several preview options and ignores potential user configuration from the Vite config (such as https, proxy, or headers). It is better to respect the resolved Vite configuration while allowing CLI options to override it. Additionally, the default port should ideally consider the one defined in the configuration to ensure consistency with user settings.

Suggested change
const port = options.port ?? 6006
const server = await preview({
root: ctx.root,
base: ctx.resolvedViteConfig.base,
configFile: false,
build: {
outDir: ctx.config.outDir,
},
preview: {
port,
host: options.host,
open: options.open,
strictPort: false,
},
})
const port = options.port ?? ctx.resolvedViteConfig.preview.port ?? 6006
const server = await preview({
root: ctx.root,
base: ctx.resolvedViteConfig.base,
configFile: false,
build: {
outDir: ctx.config.outDir,
},
preview: {
...ctx.resolvedViteConfig.preview,
port,
host: options.host,
open: options.open,
strictPort: false,
},
})

})

httpServer.on('error', onError)
const baseUrl = server.resolvedUrls?.local[0] ?? `http://localhost:${port}${ctx.resolvedViteConfig.base}`
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The baseUrl calculation can be improved for better robustness. If host is set to a specific IP, local[0] might be empty. Checking network[0] as well ensures a valid URL is returned in more network configurations (e.g., when exposing to the network).

Suggested change
const baseUrl = server.resolvedUrls?.local[0] ?? `http://localhost:${port}${ctx.resolvedViteConfig.base}`
const baseUrl = server.resolvedUrls?.local[0] ?? server.resolvedUrls?.network[0] ?? `http://localhost:${port}${ctx.resolvedViteConfig.base}`

@pkg-pr-new
Copy link
Copy Markdown

pkg-pr-new Bot commented May 1, 2026

Open in StackBlitz

histoire

npm i https://pkg.pr.new/histoire@835

@histoire/app

npm i https://pkg.pr.new/@histoire/app@835

@histoire/controls

npm i https://pkg.pr.new/@histoire/controls@835

@histoire/plugin-nuxt

npm i https://pkg.pr.new/@histoire/plugin-nuxt@835

@histoire/plugin-percy

npm i https://pkg.pr.new/@histoire/plugin-percy@835

@histoire/plugin-screenshot

npm i https://pkg.pr.new/@histoire/plugin-screenshot@835

@histoire/plugin-svelte

npm i https://pkg.pr.new/@histoire/plugin-svelte@835

@histoire/plugin-vue

npm i https://pkg.pr.new/@histoire/plugin-vue@835

@histoire/shared

npm i https://pkg.pr.new/@histoire/shared@835

@histoire/vendors

npm i https://pkg.pr.new/@histoire/vendors@835

commit: 7f7a29c

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Allow passing --host flag to CLI

1 participant