Skip to content

Commit 5f5105b

Browse files
Copilothuntie
andcommitted
refactor: cache sanitizeUrl to improve performance
Co-authored-by: huntie <2547783+huntie@users.noreply.github.com>
1 parent 353763c commit 5f5105b

1 file changed

Lines changed: 12 additions & 1 deletion

File tree

packages/cli-server-api/src/openURLMiddleware.ts

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,17 @@ import {json} from 'body-parser';
1111
import connect from 'connect';
1212
import open from 'open';
1313

14+
// Cache the imported sanitizeUrl function to avoid repeated dynamic imports
15+
let sanitizeUrlFn: ((url: string) => string) | null = null;
16+
17+
async function getSanitizeUrl(): Promise<(url: string) => string> {
18+
if (sanitizeUrlFn === null) {
19+
const module = await import('strict-url-sanitise');
20+
sanitizeUrlFn = module.sanitizeUrl;
21+
}
22+
return sanitizeUrlFn;
23+
}
24+
1425
/**
1526
* Open a URL in the system browser.
1627
*/
@@ -39,7 +50,7 @@ export async function openURLMiddleware(
3950

4051
let sanitizedUrl: string;
4152
try {
42-
const {sanitizeUrl} = await import('strict-url-sanitise');
53+
const sanitizeUrl = await getSanitizeUrl();
4354
sanitizedUrl = sanitizeUrl(url);
4455
} catch (error) {
4556
res.writeHead(400);

0 commit comments

Comments
 (0)