Skip to content

Commit aed200f

Browse files
authored
perf(assets-retry): reduce nullish coalescing operator (#4325)
1 parent 3fd33a4 commit aed200f

File tree

3 files changed

+58
-45
lines changed

3 files changed

+58
-45
lines changed

e2e/cases/assets/assets-retry/index.test.ts

Lines changed: 34 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ async function createRsbuildWithMiddleware(
7777
options: PluginAssetsRetryOptions,
7878
entry?: string,
7979
port?: number,
80-
assetPrefix?: string
80+
assetPrefix?: string,
8181
) {
8282
const rsbuild = await dev({
8383
cwd: __dirname,
@@ -94,9 +94,11 @@ async function createRsbuildWithMiddleware(
9494
middlewares.unshift(...addMiddleWares);
9595
},
9696
],
97-
...(assetPrefix ? {
98-
assetPrefix
99-
}: {})
97+
...(assetPrefix
98+
? {
99+
assetPrefix,
100+
}
101+
: {}),
100102
},
101103
...(port
102104
? {
@@ -111,10 +113,7 @@ async function createRsbuildWithMiddleware(
111113
}
112114
: {}),
113115
output: {
114-
sourceMap: {
115-
css: false,
116-
js: false,
117-
},
116+
sourceMap: false,
118117
},
119118
},
120119
});
@@ -465,7 +464,11 @@ test('domain, onRetry and onFail options should work when retrying initial chunk
465464
blockedMiddleware,
466465
{
467466
minify: true,
468-
domain: [`http://localhost:${port}`, 'http://a.com/foo-path', 'http://b.com'],
467+
domain: [
468+
`http://localhost:${port}`,
469+
'http://a.com/foo-path',
470+
'http://b.com',
471+
],
469472
onRetry(context) {
470473
console.info('onRetry', context);
471474
},
@@ -541,7 +544,11 @@ test('domain, onRetry and onFail options should work when retrying async chunk f
541544
blockedMiddleware,
542545
{
543546
minify: true,
544-
domain: [`http://localhost:${port}`, 'http://a.com/foo-path', 'http://b.com'],
547+
domain: [
548+
`http://localhost:${port}`,
549+
'http://a.com/foo-path',
550+
'http://b.com',
551+
],
545552
onRetry(context) {
546553
console.info('onRetry', context);
547554
},
@@ -659,7 +666,9 @@ test('should work with addQuery boolean option', async ({ page }) => {
659666
logger.level = 'log';
660667
});
661668

662-
test('should work with addQuery boolean option when retrying async css chunk', async ({ page }) => {
669+
test('should work with addQuery boolean option when retrying async css chunk', async ({
670+
page,
671+
}) => {
663672
logger.level = 'verbose';
664673
const { logs, restore } = proxyConsole();
665674

@@ -678,7 +687,10 @@ test('should work with addQuery boolean option when retrying async css chunk', a
678687
await gotoPage(page, rsbuild);
679688
const asyncCompTestElement = page.locator('#async-comp-test');
680689
await expect(asyncCompTestElement).toHaveText('Hello AsyncCompTest');
681-
await expect(asyncCompTestElement).toHaveCSS('background-color', 'rgb(0, 0, 139)');
690+
await expect(asyncCompTestElement).toHaveCSS(
691+
'background-color',
692+
'rgb(0, 0, 139)',
693+
);
682694

683695
const blockedAsyncChunkResponseCount = count404ResponseByUrl(
684696
logs,
@@ -901,14 +913,20 @@ test('onRetry and onFail options should work when multiple parallel retrying asy
901913
await rsbuild.close();
902914
});
903915

904-
test('should work when the first, second cdn are all failed and the third is success', async ({ page }) => {
916+
test('should work when the first, second cdn are all failed and the third is success', async ({
917+
page,
918+
}) => {
905919
// this is a real world case for assets-retry
906920
const port = await getRandomPort();
907921
const rsbuild = await createRsbuildWithMiddleware(
908922
[],
909923
{
910924
minify: true,
911-
domain: ['http://a.com/foo-path', 'http://b.com', `http://localhost:${port}`],
925+
domain: [
926+
'http://a.com/foo-path',
927+
'http://b.com',
928+
`http://localhost:${port}`,
929+
],
912930
addQuery: true,
913931
onRetry(context) {
914932
console.info('onRetry', context);
@@ -922,11 +940,11 @@ test('should work when the first, second cdn are all failed and the third is suc
922940
},
923941
undefined,
924942
port,
925-
'http://a.com/foo-path'
943+
'http://a.com/foo-path',
926944
);
927945

928946
await gotoPage(page, rsbuild);
929947
const compTestElement = page.locator('#async-comp-test');
930948
await expect(compTestElement).toHaveText('Hello AsyncCompTest');
931949
await expect(compTestElement).toHaveCSS('background-color', 'rgb(0, 0, 139)');
932-
})
950+
});

packages/plugin-assets-retry/src/runtime/asyncChunkRetry.ts

Lines changed: 15 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -60,22 +60,22 @@ const retryCollector: RetryCollector = {};
6060
const retryCssCollector: RetryCollector = {};
6161

6262
function findCurrentDomain(url: string) {
63-
const domainList = config.domain ?? [];
63+
const domains = config.domain || [];
6464
let domain = '';
65-
for (let i = 0; i < domainList.length; i++) {
66-
if (url.indexOf(domainList[i]) !== -1) {
67-
domain = domainList[i];
65+
for (let i = 0; i < domains.length; i++) {
66+
if (url.indexOf(domains[i]) !== -1) {
67+
domain = domains[i];
6868
break;
6969
}
7070
}
7171
return domain || window.origin;
7272
}
7373

7474
function findNextDomain(url: string) {
75-
const domainList = config.domain ?? [];
75+
const domains = config.domain || [];
7676
const currentDomain = findCurrentDomain(url);
77-
const index = domainList.indexOf(currentDomain);
78-
return domainList[(index + 1) % domainList.length] || url;
77+
const index = domains.indexOf(currentDomain);
78+
return domains[(index + 1) % domains.length] || url;
7979
}
8080

8181
const postfixRE = /[?#].*$/;
@@ -146,7 +146,7 @@ function initRetry(chunkId: string, isCssAsyncChunk: boolean): Retry {
146146
const originalQuery = getQueryFromUrl(originalSrcUrl);
147147

148148
const existRetryTimes = 0;
149-
const nextDomain = config.domain?.[0] ?? window.origin;
149+
const nextDomain = config.domain?.[0] || window.origin;
150150

151151
return {
152152
nextDomain,
@@ -215,8 +215,8 @@ const originalEnsureChunk = __RUNTIME_GLOBALS_ENSURE_CHUNK__;
215215
const originalGetChunkScriptFilename =
216216
__RUNTIME_GLOBALS_GET_CHUNK_SCRIPT_FILENAME__;
217217
const originalGetCssFilename =
218-
__RUNTIME_GLOBALS_GET_MINI_CSS_EXTRACT_FILENAME__ ??
219-
__RUNTIME_GLOBALS_GET_CSS_FILENAME__ ??
218+
__RUNTIME_GLOBALS_GET_MINI_CSS_EXTRACT_FILENAME__ ||
219+
__RUNTIME_GLOBALS_GET_CSS_FILENAME__ ||
220220
(() => null);
221221
const originalLoadScript = __RUNTIME_GLOBALS_LOAD_SCRIPT__;
222222

@@ -335,11 +335,7 @@ function ensureChunk(chunkId: string): Promise<unknown> {
335335
}
336336
}
337337

338-
if (
339-
config.domain &&
340-
config.domain.length > 0 &&
341-
config.domain.indexOf(nextDomain) === -1
342-
) {
338+
if (config.domain && config.domain.indexOf(nextDomain) === -1) {
343339
throw error;
344340
}
345341

@@ -376,11 +372,10 @@ function loadScript() {
376372

377373
function loadStyleSheet(href: string, chunkId: ChunkId): string {
378374
const retry = globalCurrRetryingCss[chunkId];
379-
if (retry?.nextRetryUrl) {
380-
return retry.nextRetryUrl;
381-
}
382-
383-
return __RUNTIME_GLOBALS_PUBLIC_PATH__ + href;
375+
return (
376+
// biome-ignore lint/complexity/useOptionalChain: for less code
377+
(retry && retry.nextRetryUrl) || __RUNTIME_GLOBALS_PUBLIC_PATH__ + href
378+
);
384379
}
385380

386381
function registerAsyncChunkRetry() {

packages/plugin-assets-retry/src/runtime/initialChunkRetry.ts

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,22 @@ declare global {
2020
}
2121

2222
// this function is the same as async chunk retry
23-
function findCurrentDomain(url: string, domainList: string[]) {
23+
function findCurrentDomain(url: string, domains: string[]) {
2424
let domain = '';
25-
for (let i = 0; i < domainList.length; i++) {
26-
if (url.indexOf(domainList[i]) !== -1) {
27-
domain = domainList[i];
25+
for (let i = 0; i < domains.length; i++) {
26+
if (url.indexOf(domains[i]) !== -1) {
27+
domain = domains[i];
2828
break;
2929
}
3030
}
3131
return domain || window.origin;
3232
}
3333

3434
// this function is the same as async chunk retry
35-
function findNextDomain(url: string, domainList: string[]) {
36-
const currentDomain = findCurrentDomain(url, domainList);
37-
const index = domainList.indexOf(currentDomain);
38-
return domainList[(index + 1) % domainList.length] || url;
35+
function findNextDomain(url: string, domains: string[]) {
36+
const currentDomain = findCurrentDomain(url, domains);
37+
const index = domains.indexOf(currentDomain);
38+
return domains[(index + 1) % domains.length] || url;
3939
}
4040

4141
function getRequestUrl(element: HTMLElement) {
@@ -56,7 +56,7 @@ function validateTargetInfo(
5656
e: Event,
5757
): { target: HTMLElement; tagName: string; url: string } | false {
5858
const target: HTMLElement = e.target as HTMLElement;
59-
const tagName = target.tagName?.toLocaleLowerCase();
59+
const tagName = target.tagName.toLocaleLowerCase();
6060
const allowTags = config.type!;
6161
const url = getRequestUrl(target);
6262
if (

0 commit comments

Comments
 (0)