Skip to content

Commit

Permalink
perf(assets-retry): reduce nullish coalescing operator
Browse files Browse the repository at this point in the history
  • Loading branch information
chenjiahan committed Jan 5, 2025
1 parent 3fd33a4 commit 80841f0
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 30 deletions.
35 changes: 15 additions & 20 deletions packages/plugin-assets-retry/src/runtime/asyncChunkRetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,22 +60,22 @@ const retryCollector: RetryCollector = {};
const retryCssCollector: RetryCollector = {};

function findCurrentDomain(url: string) {
const domainList = config.domain ?? [];
const domains = config.domain || [];
let domain = '';
for (let i = 0; i < domainList.length; i++) {
if (url.indexOf(domainList[i]) !== -1) {
domain = domainList[i];
for (let i = 0; i < domains.length; i++) {
if (url.indexOf(domains[i]) !== -1) {
domain = domains[i];
break;
}
}
return domain || window.origin;
}

function findNextDomain(url: string) {
const domainList = config.domain ?? [];
const domains = config.domain || [];
const currentDomain = findCurrentDomain(url);
const index = domainList.indexOf(currentDomain);
return domainList[(index + 1) % domainList.length] || url;
const index = domains.indexOf(currentDomain);
return domains[(index + 1) % domains.length] || url;
}

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

const existRetryTimes = 0;
const nextDomain = config.domain?.[0] ?? window.origin;
const nextDomain = config.domain?.[0] || window.origin;

return {
nextDomain,
Expand Down Expand Up @@ -215,8 +215,8 @@ const originalEnsureChunk = __RUNTIME_GLOBALS_ENSURE_CHUNK__;
const originalGetChunkScriptFilename =
__RUNTIME_GLOBALS_GET_CHUNK_SCRIPT_FILENAME__;
const originalGetCssFilename =
__RUNTIME_GLOBALS_GET_MINI_CSS_EXTRACT_FILENAME__ ??
__RUNTIME_GLOBALS_GET_CSS_FILENAME__ ??
__RUNTIME_GLOBALS_GET_MINI_CSS_EXTRACT_FILENAME__ ||
__RUNTIME_GLOBALS_GET_CSS_FILENAME__ ||
(() => null);
const originalLoadScript = __RUNTIME_GLOBALS_LOAD_SCRIPT__;

Expand Down Expand Up @@ -335,11 +335,7 @@ function ensureChunk(chunkId: string): Promise<unknown> {
}
}

if (
config.domain &&
config.domain.length > 0 &&
config.domain.indexOf(nextDomain) === -1
) {
if (config.domain && config.domain.indexOf(nextDomain) === -1) {
throw error;
}

Expand Down Expand Up @@ -376,11 +372,10 @@ function loadScript() {

function loadStyleSheet(href: string, chunkId: ChunkId): string {
const retry = globalCurrRetryingCss[chunkId];
if (retry?.nextRetryUrl) {
return retry.nextRetryUrl;
}

return __RUNTIME_GLOBALS_PUBLIC_PATH__ + href;
return (
// biome-ignore lint/complexity/useOptionalChain: for less code
(retry && retry.nextRetryUrl) || __RUNTIME_GLOBALS_PUBLIC_PATH__ + href
);
}

function registerAsyncChunkRetry() {
Expand Down
20 changes: 10 additions & 10 deletions packages/plugin-assets-retry/src/runtime/initialChunkRetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,22 +20,22 @@ declare global {
}

// this function is the same as async chunk retry
function findCurrentDomain(url: string, domainList: string[]) {
function findCurrentDomain(url: string, domains: string[]) {
let domain = '';
for (let i = 0; i < domainList.length; i++) {
if (url.indexOf(domainList[i]) !== -1) {
domain = domainList[i];
for (let i = 0; i < domains.length; i++) {
if (url.indexOf(domains[i]) !== -1) {
domain = domains[i];
break;
}
}
return domain || window.origin;
}

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

function getRequestUrl(element: HTMLElement) {
Expand All @@ -56,7 +56,7 @@ function validateTargetInfo(
e: Event,
): { target: HTMLElement; tagName: string; url: string } | false {
const target: HTMLElement = e.target as HTMLElement;
const tagName = target.tagName?.toLocaleLowerCase();
const tagName = target.tagName.toLocaleLowerCase();
const allowTags = config.type!;
const url = getRequestUrl(target);
if (
Expand Down Expand Up @@ -237,7 +237,7 @@ function retry(config: RuntimeRetryOptions, e: Event) {

// if the initial request is "/static/js/async/src_Hello_tsx.js?q=1", retry url would be "/static/js/async/src_Hello_tsx.js?q=1&retry=1"
const originalQuery =
target.dataset.rsbuildOriginalQuery ?? getQueryFromUrl(url);
target.dataset.rsbuildOriginalQuery || getQueryFromUrl(url);

// this function is the same as async chunk retry
function getUrlRetryQuery(existRetryTimes: number): string {
Expand Down

0 comments on commit 80841f0

Please sign in to comment.