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(assets-retry): use shorter namespace prefix #4326

Merged
merged 1 commit into from
Jan 5, 2025
Merged
Show file tree
Hide file tree
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
4 changes: 2 additions & 2 deletions packages/plugin-assets-retry/src/AsyncChunkRetryPlugin.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class AsyncChunkRetryPlugin implements Rspack.RspackPluginInstance {
)
.replaceAll(
'__RUNTIME_GLOBALS_RSBUILD_LOAD_STYLESHEET__',
'__webpack_require__.rsbuildLoadStyleSheet',
'__webpack_require__.rbLoadStyleSheet',
)
.replaceAll('__RUNTIME_GLOBALS_PUBLIC_PATH__', RuntimeGlobals.publicPath)
.replaceAll('__RUNTIME_GLOBALS_LOAD_SCRIPT__', RuntimeGlobals.loadScript)
Expand All @@ -131,7 +131,7 @@ class AsyncChunkRetryPlugin implements Rspack.RspackPluginInstance {
(originSource) =>
originSource.replace(
'var fullhref = __webpack_require__.p + href;',
'var fullhref = __webpack_require__.rsbuildLoadStyleSheet ? __webpack_require__.rsbuildLoadStyleSheet(href, chunkId) : (__webpack_require__.p + href);',
'var fullhref = __webpack_require__.rbLoadStyleSheet ? __webpack_require__.rbLoadStyleSheet(href, chunkId) : (__webpack_require__.p + href);',
),
isRspack,
);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ declare global {
| undefined;
// RuntimeGlobals.loadScript
var __RUNTIME_GLOBALS_LOAD_SCRIPT__: LoadScript;
// __webpack_require__.rsbuildLoadStyleSheet
// __webpack_require__.rbLoadStyleSheet
var __RUNTIME_GLOBALS_RSBUILD_LOAD_STYLESHEET__: LoadStyleSheet;
// RuntimeGlobals.publicPath
var __RUNTIME_GLOBALS_PUBLIC_PATH__: string;
Expand Down
31 changes: 15 additions & 16 deletions packages/plugin-assets-retry/src/runtime/initialChunkRetry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,13 +89,13 @@ function createElement(
attributes.crossOrigin === true ? 'anonymous' : attributes.crossOrigin;
const crossOriginAttr = crossOrigin ? `crossorigin="${crossOrigin}"` : '';
const retryTimesAttr = attributes.times
? `data-rsbuild-retry-times="${attributes.times}"`
? `data-rb-retry-times="${attributes.times}"`
: '';

const originalQueryAttr = attributes.originalQuery
? `data-rsbuild-original-query="${attributes.originalQuery}"`
? `data-rb-original-query="${attributes.originalQuery}"`
: '';
const isAsyncAttr = attributes.isAsync ? 'data-rsbuild-async' : '';
const isAsyncAttr = attributes.isAsync ? 'data-rb-async' : '';

if (origin instanceof HTMLScriptElement) {
const script = document.createElement('script');
Expand All @@ -104,13 +104,13 @@ function createElement(
script.crossOrigin = crossOrigin;
}
if (attributes.times) {
script.dataset.rsbuildRetryTimes = String(attributes.times);
script.dataset.rbRetryTimes = String(attributes.times);
}
if (attributes.isAsync) {
script.dataset.rsbuildAsync = '';
script.dataset.rbAsync = '';
}
if (attributes.originalQuery !== undefined) {
script.dataset.rsbuildOriginalQuery = attributes.originalQuery;
script.dataset.rbOriginalQuery = attributes.originalQuery;
}

return {
Expand All @@ -135,10 +135,10 @@ function createElement(
link.crossOrigin = crossOrigin;
}
if (attributes.times) {
link.dataset.rsbuildRetryTimes = String(attributes.times);
link.dataset.rbRetryTimes = String(attributes.times);
}
if (attributes.originalQuery !== undefined) {
link.dataset.rsbuildOriginalQuery = attributes.originalQuery;
link.dataset.rbOriginalQuery = attributes.originalQuery;
}
return {
element: link,
Expand Down Expand Up @@ -170,8 +170,8 @@ function reloadElementResource(

if (origin instanceof HTMLImageElement) {
origin.src = attributes.url;
origin.dataset.rsbuildRetryTimes = String(attributes.times);
origin.dataset.rsbuildOriginalQuery = String(attributes.originalQuery);
origin.dataset.rbRetryTimes = String(attributes.times);
origin.dataset.rbOriginalQuery = String(attributes.originalQuery);
}
}

Expand Down Expand Up @@ -217,7 +217,7 @@ function retry(config: RuntimeRetryOptions, e: Event) {
}

// If the retry times has exceeded the maximum, fail
const existRetryTimes = Number(target.dataset.rsbuildRetryTimes) || 0;
const existRetryTimes = Number(target.dataset.rbRetryTimes) || 0;
if (existRetryTimes === config.max!) {
if (typeof config.onFail === 'function') {
const context: AssetsRetryHookContext = {
Expand All @@ -236,8 +236,7 @@ function retry(config: RuntimeRetryOptions, e: Event) {
const nextDomain = findNextDomain(domain, config.domain!);

// 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);
const originalQuery = target.dataset.rbOriginalQuery ?? getQueryFromUrl(url);

// this function is the same as async chunk retry
function getUrlRetryQuery(existRetryTimes: number): string {
Expand Down Expand Up @@ -266,7 +265,7 @@ function retry(config: RuntimeRetryOptions, e: Event) {
}

const isAsync =
Boolean(target.dataset.rsbuildAsync) ||
Boolean(target.dataset.rbAsync) ||
(target as HTMLScriptElement).async ||
(target as HTMLScriptElement).defer;

Expand All @@ -280,7 +279,7 @@ function retry(config: RuntimeRetryOptions, e: Event) {

const element = createElement(target, attributes)!;

if (config.onRetry && typeof config.onRetry === 'function') {
if (typeof config.onRetry === 'function') {
const context: AssetsRetryHookContext = {
times: existRetryTimes,
domain,
Expand All @@ -301,7 +300,7 @@ function load(config: RuntimeRetryOptions, e: Event) {
}
const { target, tagName, url } = targetInfo;
const domain = findCurrentDomain(url, config.domain!);
const retryTimes = Number(target.dataset.rsbuildRetryTimes) || 0;
const retryTimes = Number(target.dataset.rbRetryTimes) || 0;
if (retryTimes === 0) {
return;
}
Expand Down
Loading