Skip to content

Commit abeef37

Browse files
committed
fix(examples): auto-assign the hub side-car port instead of failing hard
getPort({ random: false }) throws when the preferred side-car port is taken (e.g. by a lingering previous hub instance), so `vite dev` refused to start while the old instance kept serving — which made it look like new code changes (like ctx.terminals spawning) had no effect. Walk a port range and fall back to a random free port instead; clients discover the chosen port via __connection.json either way.
1 parent 6d122b5 commit abeef37

2 files changed

Lines changed: 8 additions & 2 deletions

File tree

examples/minimal-vite-devframe-hub/src/minimal-vite-devframe-hub.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,10 @@ export function minimalViteDevframeHub(options: MinimalViteDevframeHubOptions =
7777
started = undefined
7878

7979
const cwd = viteConfig!.root
80-
const port = options.port ?? await getPort({ port: 9777, random: false })
80+
// Prefer 9777 but keep booting when it's taken (e.g. a lingering
81+
// previous instance) — walk the range, then fall back to a random free
82+
// port. Clients discover whatever was chosen via `__connection.json`.
83+
const port = options.port ?? await getPort({ port: 9777, portRange: [9777, 9877] })
8184

8285
// Serve the side-car's connection meta (`__connection.json`) at a URL
8386
// base so a browser loaded there can discover the WS endpoint via

examples/storybook-hub/src/storybook-hub.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,10 @@ export function storybookHub(options: StorybookHubOptions = {}): Plugin {
153153
killDevServers()
154154

155155
const cwd = viteConfig?.root ?? process.cwd()
156-
const port = options.port ?? await getPort({ port: 9787, random: false })
156+
// Prefer 9787 but keep booting when it's taken (e.g. a lingering previous
157+
// instance) — walk the range, then fall back to a random free port. The
158+
// client discovers whatever was chosen via `__connection.json`.
159+
const port = options.port ?? await getPort({ port: 9787, portRange: [9787, 9887] })
157160

158161
const serveConnectionMeta = (metaBase: string): void => {
159162
server.middlewares.use(`${metaBase}${DEVFRAME_CONNECTION_META_FILENAME}`, (_req, res) => {

0 commit comments

Comments
 (0)