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

perf(core): Optimize docusaurus start/serve, fix openBrowser() perf issue on macOS #11007

Merged
merged 1 commit into from
Mar 19, 2025
Merged
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
92 changes: 64 additions & 28 deletions packages/docusaurus/src/commands/utils/openBrowser/openBrowser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,12 @@

/* eslint-disable */

import {execSync} from 'child_process';
import {exec} from 'child_process';
import {promisify} from 'util';
import open from 'open';
import {PerfLogger} from '@docusaurus/logger';

const execPromise = promisify(exec);

type BrowserName = string | undefined;
type BrowserArgs = string[];
Expand Down Expand Up @@ -49,38 +53,66 @@
}

if (shouldTryOpenChromiumWithAppleScript) {
// Will use the first open browser found from list
const supportedChromiumBrowsers = [
'Google Chrome Canary',
'Google Chrome Dev',
'Google Chrome Beta',
'Google Chrome',
'Microsoft Edge',
'Brave Browser',
'Vivaldi',
'Chromium',
];

for (let chromiumBrowser of supportedChromiumBrowsers) {
async function getBrowsersToTry(): Promise<string[]> {
// Will use the first open browser found from list
const supportedChromiumBrowsers = [
'Google Chrome Canary',
'Google Chrome Dev',
'Google Chrome Beta',
'Google Chrome',
'Microsoft Edge',
'Brave Browser',
'Vivaldi',
'Chromium',
];

// Among all the supported browsers, retrieves to stdout the active ones
const command = `ps cax -o command | grep -E "^(${supportedChromiumBrowsers.join(
'|',
)})$"`;
const result = await execPromise(command);

const activeBrowsers = result.stdout.toString().trim().split('\n');

// This preserves the initial browser order
// We open Google Chrome Canary in priority over Google Chrome
return supportedChromiumBrowsers.filter((b) =>
activeBrowsers.includes(b),
);
}

async function tryBrowser(browserName: string): Promise<boolean> {
try {
// Try our best to reuse existing tab
// on OSX Chromium-based browser with AppleScript
execSync(`ps cax | grep "${chromiumBrowser}"`);
execSync(
`osascript openChrome.applescript "${encodeURI(
url,
)}" "${chromiumBrowser}"`,
{
cwd: __dirname,
stdio: 'ignore',
},
);
// This command runs the openChrome.applescript (copied from CRA)
const command = `osascript openChrome.applescript "${encodeURI(
url,
)}" "${browserName}"`;
await execPromise(command, {
cwd: __dirname,
});
return true;
} catch (err) {
// Ignore errors.
console.error(
`Failed to open browser ${browserName} with AppleScript`,
err,
);
return false;
}
}

const browsers = await PerfLogger.async('getBrowsersToTry', () =>
getBrowsersToTry(),
);
for (let browser of browsers) {
const success = await PerfLogger.async(browser, () =>
tryBrowser(browser),
);
if (success) {
return true;
}
}
}

return false;
}

Expand All @@ -103,7 +135,11 @@
}

async function startBrowserProcess(params: Params): Promise<boolean> {
if (await tryOpenWithAppleScript(params)) {
if (
await PerfLogger.async('tryOpenWithAppleScript', () =>
tryOpenWithAppleScript(params),
)
) {
return true;
}
try {
Expand Down
Loading