Skip to content

Commit 502b900

Browse files
authored
perf(core): Optimize docusaurus start/serve, fix openBrowser() perf issue on macOS (#11007)
Optimize openBrowser() util
1 parent 3782244 commit 502b900

File tree

1 file changed

+64
-28
lines changed

1 file changed

+64
-28
lines changed

packages/docusaurus/src/commands/utils/openBrowser/openBrowser.ts

+64-28
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,12 @@
1212

1313
/* eslint-disable */
1414

15-
import {execSync} from 'child_process';
15+
import {exec} from 'child_process';
16+
import {promisify} from 'util';
1617
import open from 'open';
18+
import {PerfLogger} from '@docusaurus/logger';
19+
20+
const execPromise = promisify(exec);
1721

1822
type BrowserName = string | undefined;
1923
type BrowserArgs = string[];
@@ -49,38 +53,66 @@ async function tryOpenWithAppleScript({
4953
}
5054

5155
if (shouldTryOpenChromiumWithAppleScript) {
52-
// Will use the first open browser found from list
53-
const supportedChromiumBrowsers = [
54-
'Google Chrome Canary',
55-
'Google Chrome Dev',
56-
'Google Chrome Beta',
57-
'Google Chrome',
58-
'Microsoft Edge',
59-
'Brave Browser',
60-
'Vivaldi',
61-
'Chromium',
62-
];
63-
64-
for (let chromiumBrowser of supportedChromiumBrowsers) {
56+
async function getBrowsersToTry(): Promise<string[]> {
57+
// Will use the first open browser found from list
58+
const supportedChromiumBrowsers = [
59+
'Google Chrome Canary',
60+
'Google Chrome Dev',
61+
'Google Chrome Beta',
62+
'Google Chrome',
63+
'Microsoft Edge',
64+
'Brave Browser',
65+
'Vivaldi',
66+
'Chromium',
67+
];
68+
69+
// Among all the supported browsers, retrieves to stdout the active ones
70+
const command = `ps cax -o command | grep -E "^(${supportedChromiumBrowsers.join(
71+
'|',
72+
)})$"`;
73+
const result = await execPromise(command);
74+
75+
const activeBrowsers = result.stdout.toString().trim().split('\n');
76+
77+
// This preserves the initial browser order
78+
// We open Google Chrome Canary in priority over Google Chrome
79+
return supportedChromiumBrowsers.filter((b) =>
80+
activeBrowsers.includes(b),
81+
);
82+
}
83+
84+
async function tryBrowser(browserName: string): Promise<boolean> {
6585
try {
66-
// Try our best to reuse existing tab
67-
// on OSX Chromium-based browser with AppleScript
68-
execSync(`ps cax | grep "${chromiumBrowser}"`);
69-
execSync(
70-
`osascript openChrome.applescript "${encodeURI(
71-
url,
72-
)}" "${chromiumBrowser}"`,
73-
{
74-
cwd: __dirname,
75-
stdio: 'ignore',
76-
},
77-
);
86+
// This command runs the openChrome.applescript (copied from CRA)
87+
const command = `osascript openChrome.applescript "${encodeURI(
88+
url,
89+
)}" "${browserName}"`;
90+
await execPromise(command, {
91+
cwd: __dirname,
92+
});
7893
return true;
7994
} catch (err) {
80-
// Ignore errors.
95+
console.error(
96+
`Failed to open browser ${browserName} with AppleScript`,
97+
err,
98+
);
99+
return false;
100+
}
101+
}
102+
103+
const browsers = await PerfLogger.async('getBrowsersToTry', () =>
104+
getBrowsersToTry(),
105+
);
106+
for (let browser of browsers) {
107+
const success = await PerfLogger.async(browser, () =>
108+
tryBrowser(browser),
109+
);
110+
if (success) {
111+
return true;
81112
}
82113
}
83114
}
115+
84116
return false;
85117
}
86118

@@ -103,7 +135,11 @@ function toOpenApp(params: Params): open.App | undefined {
103135
}
104136

105137
async function startBrowserProcess(params: Params): Promise<boolean> {
106-
if (await tryOpenWithAppleScript(params)) {
138+
if (
139+
await PerfLogger.async('tryOpenWithAppleScript', () =>
140+
tryOpenWithAppleScript(params),
141+
)
142+
) {
107143
return true;
108144
}
109145
try {

0 commit comments

Comments
 (0)