Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions packages/cli/src/commands/preview.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ interface BrowserLaunchOptions {
browserPath?: string;
userDataDir?: string;
remoteDebuggingPort?: number;
browserNoGpu?: boolean;
}

interface StudioLaunchOptions extends BrowserLaunchOptions {
Expand Down Expand Up @@ -142,6 +143,12 @@ export default defineCommand({
type: "string",
description: "Chromium remote debugging port (requires --browser-path and --user-data-dir)",
},
"browser-no-gpu": {
type: "boolean",
default: false,
description:
"Launch the opened browser with --disable-gpu (requires --browser-path). For hosts where hardware acceleration crashes the graphics driver (e.g. NVIDIA Xid resets); with the system default browser use --no-open instead.",
},
},
async run({ args }) {
const startPort = parseInt(args.port ?? "3002", 10);
Expand Down Expand Up @@ -239,6 +246,14 @@ export default defineCommand({

const noOpen = !args.open;
const browserPath = args["browser-path"] as string | undefined;
const browserNoGpu = !!args["browser-no-gpu"];
if (browserNoGpu && !browserPath) {
clack.log.error(
"--browser-no-gpu requires --browser-path (the system default browser cannot receive Chromium flags — use --no-open on GPU-unstable hosts)",
);
process.exitCode = 1;
return;
}
const userDataDir = args["user-data-dir"] as string | undefined;
let remoteDebuggingPort: number | undefined;
try {
Expand All @@ -258,6 +273,7 @@ export default defineCommand({
browserPath,
userDataDir,
remoteDebuggingPort,
browserNoGpu,
});
}

Expand All @@ -269,6 +285,7 @@ export default defineCommand({
browserPath,
userDataDir,
remoteDebuggingPort,
browserNoGpu,
});
}

Expand All @@ -280,6 +297,7 @@ export default defineCommand({
browserPath,
userDataDir,
remoteDebuggingPort,
browserNoGpu,
});
},
});
Expand Down Expand Up @@ -641,6 +659,7 @@ function openStudioBrowser(url: string, projectName: string, options?: BrowserLa
browserPath: options?.browserPath,
userDataDir: options?.userDataDir,
remoteDebuggingPort: options?.remoteDebuggingPort,
disableGpu: options?.browserNoGpu,
});
}

Expand Down
13 changes: 13 additions & 0 deletions packages/cli/src/utils/openBrowser.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,16 @@ describe("validateRemoteDebuggingPortDeps", () => {
).toBe("--remote-debugging-port requires --browser-path");
});
});

describe("buildBrowserArgs --disable-gpu", () => {
it("appends --disable-gpu before the url when disableGpu is set", () => {
expect(buildBrowserArgs("http://localhost:3002", { disableGpu: true })).toEqual([
"--disable-gpu",
"http://localhost:3002",
]);
});

it("omits --disable-gpu by default", () => {
expect(buildBrowserArgs("http://localhost:3002", {})).toEqual(["http://localhost:3002"]);
});
});
11 changes: 11 additions & 0 deletions packages/cli/src/utils/openBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ export interface OpenBrowserOptions {
browserPath?: string;
userDataDir?: string;
remoteDebuggingPort?: number;
/**
* Launch the browser with --disable-gpu. For hosts where hardware
* acceleration crashes the graphics driver (wild report: auto-opened
* preview triggered NVIDIA Xid 32 resets on NixOS). Only effective with
* `browserPath` — the `open`-package fallback launches the system default
* browser with no way to pass Chromium flags.
*/
disableGpu?: boolean;
}

export function parseRemoteDebuggingPort(value: string | undefined): number | undefined {
Expand Down Expand Up @@ -52,6 +60,9 @@ export function buildBrowserArgs(url: string, options: OpenBrowserOptions): stri
if (options.remoteDebuggingPort !== undefined && options.userDataDir) {
args.push(`--remote-debugging-port=${options.remoteDebuggingPort}`);
}
if (options.disableGpu) {
args.push("--disable-gpu");
}
args.push(url);
return args;
}
Expand Down
Loading