@rsbuild/plugin-assets-retry 1.0.3 → 1.0.5
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.cjs +139 -307
- package/dist/index.js +116 -278
- package/dist/runtime/asyncChunkRetry.d.ts +5 -5
- package/dist/runtime/asyncChunkRetry.js +81 -44
- package/dist/runtime/asyncChunkRetry.min.js +1 -1
- package/package.json +8 -7
|
@@ -73,8 +73,12 @@ function getNextRetryUrl(existRetryTimes, nextDomain, originalSrcUrl) {
|
|
|
73
73
|
var absolutePath = getAbsolutePathFromUrl(originalSrcUrl);
|
|
74
74
|
return nextDomain + absolutePath + getUrlRetryQuery(existRetryTimes, getQueryFromUrl(originalSrcUrl));
|
|
75
75
|
}
|
|
76
|
-
|
|
77
|
-
|
|
76
|
+
|
|
77
|
+
// shared between ensureChunk and loadScript
|
|
78
|
+
var globalCurrRetrying = {};
|
|
79
|
+
function getCurrentRetry(chunkId, existRetryTimes) {
|
|
80
|
+
var _retryCollector$chunk;
|
|
81
|
+
return (_retryCollector$chunk = retryCollector[chunkId]) === null || _retryCollector$chunk === void 0 ? void 0 : _retryCollector$chunk[existRetryTimes];
|
|
78
82
|
}
|
|
79
83
|
function initRetry(chunkId) {
|
|
80
84
|
var _config$domain$, _config$domain3;
|
|
@@ -83,32 +87,32 @@ function initRetry(chunkId) {
|
|
|
83
87
|
var existRetryTimes = 1;
|
|
84
88
|
var nextDomain = (_config$domain$ = (_config$domain3 = config.domain) === null || _config$domain3 === void 0 ? void 0 : _config$domain3[0]) !== null && _config$domain$ !== void 0 ? _config$domain$ : window.origin;
|
|
85
89
|
return {
|
|
86
|
-
existRetryTimes: existRetryTimes,
|
|
87
90
|
nextDomain: nextDomain,
|
|
88
91
|
nextRetryUrl: getNextRetryUrl(existRetryTimes, nextDomain, originalSrcUrl),
|
|
89
92
|
originalScriptFilename: originalScriptFilename,
|
|
90
93
|
originalSrcUrl: originalSrcUrl
|
|
91
94
|
};
|
|
92
95
|
}
|
|
93
|
-
function nextRetry(chunkId) {
|
|
94
|
-
var currRetry = getCurrentRetry(chunkId);
|
|
96
|
+
function nextRetry(chunkId, existRetryTimes) {
|
|
97
|
+
var currRetry = getCurrentRetry(chunkId, existRetryTimes);
|
|
95
98
|
var nextRetry;
|
|
96
|
-
|
|
99
|
+
var nextExistRetryTimes = existRetryTimes + 1;
|
|
100
|
+
if (existRetryTimes === 0 || currRetry === undefined) {
|
|
97
101
|
nextRetry = initRetry(chunkId);
|
|
102
|
+
retryCollector[chunkId] = [];
|
|
98
103
|
} else {
|
|
99
104
|
var originalScriptFilename = currRetry.originalScriptFilename,
|
|
100
105
|
originalSrcUrl = currRetry.originalSrcUrl;
|
|
101
|
-
var existRetryTimes = currRetry.existRetryTimes + 1;
|
|
102
106
|
var nextDomain = findNextDomain(currRetry.nextDomain);
|
|
103
107
|
nextRetry = {
|
|
104
|
-
existRetryTimes: existRetryTimes,
|
|
105
108
|
nextDomain: nextDomain,
|
|
106
|
-
nextRetryUrl: getNextRetryUrl(
|
|
109
|
+
nextRetryUrl: getNextRetryUrl(nextExistRetryTimes, nextDomain, originalSrcUrl),
|
|
107
110
|
originalScriptFilename: originalScriptFilename,
|
|
108
111
|
originalSrcUrl: originalSrcUrl
|
|
109
112
|
};
|
|
110
113
|
}
|
|
111
|
-
retryCollector[chunkId] = nextRetry;
|
|
114
|
+
retryCollector[chunkId][nextExistRetryTimes] = nextRetry;
|
|
115
|
+
globalCurrRetrying[chunkId] = nextRetry;
|
|
112
116
|
return nextRetry;
|
|
113
117
|
}
|
|
114
118
|
|
|
@@ -119,33 +123,62 @@ var originalGetCssFilename = (_ref = (_RUNTIME_GLOBALS_GET = __RUNTIME_GLOBALS_G
|
|
|
119
123
|
return null;
|
|
120
124
|
};
|
|
121
125
|
var originalLoadScript = __RUNTIME_GLOBALS_LOAD_SCRIPT__;
|
|
126
|
+
var ERROR_PREFIX = '[@rsbuild/plugin-assets-retry] ';
|
|
122
127
|
|
|
123
128
|
// if users want to support es5, add Promise polyfill first https://github.com/webpack/webpack/issues/12877
|
|
124
|
-
function ensureChunk(chunkId
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
var
|
|
129
|
+
function ensureChunk(chunkId,
|
|
130
|
+
// args placeholder, to avoid that other webpack runtime would add arg for __webpack_require__.e
|
|
131
|
+
arg0, arg1, arg2, arg3, arg4, arg5, arg6) {
|
|
132
|
+
var callingCounter = arguments.length > 8 && arguments[8] !== undefined ? arguments[8] : {
|
|
133
|
+
count: 0
|
|
134
|
+
};
|
|
135
|
+
for (var _len = arguments.length, args = new Array(_len > 9 ? _len - 9 : 0), _key = 9; _key < _len; _key++) {
|
|
136
|
+
args[_key - 9] = arguments[_key];
|
|
137
|
+
}
|
|
138
|
+
var result = originalEnsureChunk.apply(void 0, [chunkId, arg0, arg1, arg2, arg3, arg4, arg5, arg6, callingCounter].concat(args));
|
|
139
|
+
try {
|
|
140
|
+
var originalScriptFilename = originalGetChunkScriptFilename(chunkId);
|
|
141
|
+
var originalCssFilename = originalGetCssFilename(chunkId);
|
|
128
142
|
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
143
|
+
// mark the async chunk name in the global variables and share it with initial chunk retry to avoid duplicate retrying
|
|
144
|
+
if (typeof window !== 'undefined') {
|
|
145
|
+
if (originalScriptFilename) {
|
|
146
|
+
window.__RB_ASYNC_CHUNKS__[originalScriptFilename] = true;
|
|
147
|
+
}
|
|
148
|
+
if (originalCssFilename) {
|
|
149
|
+
window.__RB_ASYNC_CHUNKS__[originalCssFilename] = true;
|
|
150
|
+
}
|
|
136
151
|
}
|
|
152
|
+
} catch (e) {
|
|
153
|
+
console.error(ERROR_PREFIX, 'get original script or css filename error', e);
|
|
137
154
|
}
|
|
155
|
+
|
|
156
|
+
// if __webpack_require__.e is polluted by other runtime codes, fallback to originalEnsureChunk
|
|
157
|
+
if (typeof (callingCounter === null || callingCounter === void 0 ? void 0 : callingCounter.count) !== 'number') {
|
|
158
|
+
return result;
|
|
159
|
+
}
|
|
160
|
+
callingCounter.count += 1;
|
|
138
161
|
return result.catch(function (error) {
|
|
139
162
|
var _error$message;
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
163
|
+
// the first calling is not retry
|
|
164
|
+
// if the failed request is 4 in network panel, callingCounter.count === 4, the first one is the normal request, and existRetryTimes is 3, retried 3 times
|
|
165
|
+
var existRetryTimes = callingCounter.count - 1;
|
|
166
|
+
var originalSrcUrl;
|
|
167
|
+
var nextRetryUrl;
|
|
168
|
+
var nextDomain;
|
|
169
|
+
try {
|
|
170
|
+
var retryResult = nextRetry(chunkId, existRetryTimes);
|
|
171
|
+
originalSrcUrl = retryResult.originalSrcUrl;
|
|
172
|
+
nextRetryUrl = retryResult.nextRetryUrl;
|
|
173
|
+
nextDomain = retryResult.nextDomain;
|
|
174
|
+
} catch (e) {
|
|
175
|
+
console.error(ERROR_PREFIX, 'failed to get nextRetryUrl', e);
|
|
176
|
+
throw error;
|
|
177
|
+
}
|
|
145
178
|
|
|
146
|
-
// At present, we don't consider the switching domain and addQuery of async
|
|
147
|
-
// 1. Async js chunk will be requested first. It is rare for async
|
|
148
|
-
// 2. the code of loading
|
|
179
|
+
// At present, we don't consider the switching domain and addQuery of async CSS chunk
|
|
180
|
+
// 1. Async js chunk will be requested first. It is rare for async CSS chunk to fail alone.
|
|
181
|
+
// 2. the code of loading CSS in webpack runtime is complex and it may be modified by cssExtractPlugin, increase the complexity of this plugin.
|
|
149
182
|
var isCssAsyncChunkLoadFailed = Boolean(error === null || error === void 0 || (_error$message = error.message) === null || _error$message === void 0 ? void 0 : _error$message.includes('CSS chunk'));
|
|
150
183
|
var createContext = function createContext(times) {
|
|
151
184
|
return {
|
|
@@ -156,9 +189,10 @@ function ensureChunk(chunkId) {
|
|
|
156
189
|
isAsyncChunk: true
|
|
157
190
|
};
|
|
158
191
|
};
|
|
159
|
-
var context = createContext(existRetryTimes
|
|
160
|
-
if (existRetryTimes
|
|
161
|
-
|
|
192
|
+
var context = createContext(existRetryTimes);
|
|
193
|
+
if (existRetryTimes >= maxRetries) {
|
|
194
|
+
var _error$message2;
|
|
195
|
+
error.message = (_error$message2 = error.message) !== null && _error$message2 !== void 0 && _error$message2.includes('retries:') ? error.message : "Loading chunk ".concat(chunkId, " from ").concat(originalSrcUrl, " failed after ").concat(maxRetries, " retries: \"").concat(error.message, "\"");
|
|
162
196
|
if (typeof config.onFail === 'function') {
|
|
163
197
|
config.onFail(context);
|
|
164
198
|
}
|
|
@@ -186,23 +220,26 @@ function ensureChunk(chunkId) {
|
|
|
186
220
|
if (typeof config.onRetry === 'function') {
|
|
187
221
|
config.onRetry(context);
|
|
188
222
|
}
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
193
|
-
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
197
|
-
|
|
223
|
+
var nextPromise = ensureChunk.apply(void 0, [chunkId, arg0, arg1, arg2, arg3, arg4, arg5, arg6, callingCounter].concat(args));
|
|
224
|
+
return nextPromise.then(function (result) {
|
|
225
|
+
// when after retrying the third time
|
|
226
|
+
// ensureChunk(chunkId, { count: 3 }), at that time, existRetryTimes === 2
|
|
227
|
+
// after all, callingCounter.count is 4
|
|
228
|
+
var isLastSuccessRetry = (callingCounter === null || callingCounter === void 0 ? void 0 : callingCounter.count) === existRetryTimes + 2;
|
|
229
|
+
if (typeof config.onSuccess === 'function' && isLastSuccessRetry) {
|
|
230
|
+
var _context = createContext(existRetryTimes + 1);
|
|
231
|
+
config.onSuccess(_context);
|
|
198
232
|
}
|
|
199
233
|
return result;
|
|
200
234
|
});
|
|
201
235
|
});
|
|
202
236
|
}
|
|
203
237
|
function loadScript(originalUrl, done, key, chunkId) {
|
|
204
|
-
var retry =
|
|
205
|
-
|
|
238
|
+
var retry = globalCurrRetrying[chunkId];
|
|
239
|
+
for (var _len2 = arguments.length, args = new Array(_len2 > 4 ? _len2 - 4 : 0), _key2 = 4; _key2 < _len2; _key2++) {
|
|
240
|
+
args[_key2 - 4] = arguments[_key2];
|
|
241
|
+
}
|
|
242
|
+
return originalLoadScript.apply(void 0, [retry ? retry.nextRetryUrl : originalUrl, done, key, chunkId].concat(args));
|
|
206
243
|
}
|
|
207
244
|
function registerAsyncChunkRetry() {
|
|
208
245
|
// init global variables shared between initial-chunk-retry and async-chunk-retry
|
|
@@ -214,7 +251,7 @@ function registerAsyncChunkRetry() {
|
|
|
214
251
|
__RUNTIME_GLOBALS_ENSURE_CHUNK__ = ensureChunk;
|
|
215
252
|
__RUNTIME_GLOBALS_LOAD_SCRIPT__ = loadScript;
|
|
216
253
|
} catch (e) {
|
|
217
|
-
console.error('
|
|
254
|
+
console.error(ERROR_PREFIX, 'Register async chunk retry runtime failed', e);
|
|
218
255
|
}
|
|
219
256
|
}
|
|
220
257
|
}
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var _ref,_RUNTIME_GLOBALS_GET,config=__RETRY_OPTIONS__,maxRetries=config.max||3,retryCollector={};function findCurrentDomain(n){for(var r,e=null!==(r=config.domain)&&void 0!==r?r:[],i="",t=0;t<e.length;t++)if(-1!==n.indexOf(e[t])){i=e[t];break}return i||window.origin}function findNextDomain(n){var r,e=null!==(r=config.domain)&&void 0!==r?r:[],i=findCurrentDomain(n),t=e.indexOf(i);return e[(t+1)%e.length]||n}var postfixRE=/[?#].*$/;function cleanUrl(n){return n.replace(postfixRE,"")}function getQueryFromUrl(n){var r=n.split("?")[1];return r?"?".concat(r.split("#")[0]):""}function getUrlRetryQuery(n,r){return!0===config.addQuery?""!==r?"".concat(r,"&retry=").concat(n):"?retry=".concat(n):"function"==typeof config.addQuery?config.addQuery({times:n,originalQuery:r}):""}function removeDomainFromUrl(n){var r=n.indexOf("//");if(-1===r&&n.startsWith("/"))return n;var e=r+2,i=n.indexOf("/",e);return n.slice(i)}function getAbsolutePathFromUrl(n){return cleanUrl(removeDomainFromUrl(n))}function getNextRetryUrl(n,r,e){return r+getAbsolutePathFromUrl(e)+getUrlRetryQuery(n,getQueryFromUrl(e))}function getCurrentRetry(n){return retryCollector[n]}function initRetry(n){var r,e,i=originalGetChunkScriptFilename(n),t=__RUNTIME_GLOBALS_PUBLIC_PATH__+i,o=null!==(r=null===(e=config.domain)||void 0===e?void 0:e[0])&&void 0!==r?r:window.origin;return{
|
|
1
|
+
"use strict";var _ref,_RUNTIME_GLOBALS_GET,config=__RETRY_OPTIONS__,maxRetries=config.max||3,retryCollector={};function findCurrentDomain(n){for(var r,e=null!==(r=config.domain)&&void 0!==r?r:[],i="",t=0;t<e.length;t++)if(-1!==n.indexOf(e[t])){i=e[t];break}return i||window.origin}function findNextDomain(n){var r,e=null!==(r=config.domain)&&void 0!==r?r:[],i=findCurrentDomain(n),t=e.indexOf(i);return e[(t+1)%e.length]||n}var postfixRE=/[?#].*$/;function cleanUrl(n){return n.replace(postfixRE,"")}function getQueryFromUrl(n){var r=n.split("?")[1];return r?"?".concat(r.split("#")[0]):""}function getUrlRetryQuery(n,r){return!0===config.addQuery?""!==r?"".concat(r,"&retry=").concat(n):"?retry=".concat(n):"function"==typeof config.addQuery?config.addQuery({times:n,originalQuery:r}):""}function removeDomainFromUrl(n){var r=n.indexOf("//");if(-1===r&&n.startsWith("/"))return n;var e=r+2,i=n.indexOf("/",e);return n.slice(i)}function getAbsolutePathFromUrl(n){return cleanUrl(removeDomainFromUrl(n))}function getNextRetryUrl(n,r,e){return r+getAbsolutePathFromUrl(e)+getUrlRetryQuery(n,getQueryFromUrl(e))}var globalCurrRetrying={};function getCurrentRetry(n,r){var e;return null===(e=retryCollector[n])||void 0===e?void 0:e[r]}function initRetry(n){var r,e,i=originalGetChunkScriptFilename(n),t=__RUNTIME_GLOBALS_PUBLIC_PATH__+i,o=null!==(r=null===(e=config.domain)||void 0===e?void 0:e[0])&&void 0!==r?r:window.origin;return{nextDomain:o,nextRetryUrl:getNextRetryUrl(1,o,t),originalScriptFilename:i,originalSrcUrl:t}}function nextRetry(n,r){var e,i=getCurrentRetry(n,r),t=r+1;if(0===r||void 0===i)e=initRetry(n),retryCollector[n]=[];else{var o=i.originalScriptFilename,a=i.originalSrcUrl,l=findNextDomain(i.nextDomain);e={nextDomain:l,nextRetryUrl:getNextRetryUrl(t,l,a),originalScriptFilename:o,originalSrcUrl:a}}return retryCollector[n][t]=e,globalCurrRetrying[n]=e,e}var originalEnsureChunk=__RUNTIME_GLOBALS_ENSURE_CHUNK__,originalGetChunkScriptFilename=__RUNTIME_GLOBALS_GET_CHUNK_SCRIPT_FILENAME__,originalGetCssFilename=null!==(_ref=null!==(_RUNTIME_GLOBALS_GET=__RUNTIME_GLOBALS_GET_MINI_CSS_EXTRACT_FILENAME__)&&void 0!==_RUNTIME_GLOBALS_GET?_RUNTIME_GLOBALS_GET:__RUNTIME_GLOBALS_GET_CSS_FILENAME__)&&void 0!==_ref?_ref:function(){return null},originalLoadScript=__RUNTIME_GLOBALS_LOAD_SCRIPT__,ERROR_PREFIX="[@rsbuild/plugin-assets-retry] ";function ensureChunk(n,r,e,i,t,o,a,l){for(var c=arguments.length>8&&void 0!==arguments[8]?arguments[8]:{count:0},_=arguments.length,u=new Array(_>9?_-9:0),f=9;f<_;f++)u[f-9]=arguments[f];var g=originalEnsureChunk.apply(void 0,[n,r,e,i,t,o,a,l,c].concat(u));try{var R=originalGetChunkScriptFilename(n),d=originalGetCssFilename(n);"undefined"!=typeof window&&(R&&(window.__RB_ASYNC_CHUNKS__[R]=!0),d&&(window.__RB_ASYNC_CHUNKS__[d]=!0))}catch(n){console.error(ERROR_PREFIX,"get original script or css filename error",n)}return"number"!=typeof(null==c?void 0:c.count)?g:(c.count+=1,g.catch((function(_){var f,g,R,d,s=c.count-1;try{var y=nextRetry(n,s);g=y.originalSrcUrl,R=y.nextRetryUrl,d=y.nextDomain}catch(n){throw console.error(ERROR_PREFIX,"failed to get nextRetryUrl",n),_}var m,S=Boolean(null==_||null===(f=_.message)||void 0===f?void 0:f.includes("CSS chunk")),E=function(n){return{times:n,domain:d,url:R,tagName:S?"link":"script",isAsyncChunk:!0}},U=E(s);if(s>=maxRetries)throw _.message=null!==(m=_.message)&&void 0!==m&&m.includes("retries:")?_.message:"Loading chunk ".concat(n," from ").concat(g," failed after ").concat(maxRetries,' retries: "').concat(_.message,'"'),"function"==typeof config.onFail&&config.onFail(U),_;var v=config.test;if(v){if("string"==typeof v){var C=new RegExp(v);v=function(n){return C.test(n)}}if("function"!=typeof v||!v(R))throw _}if(config.domain&&config.domain.length>0&&-1===config.domain.indexOf(d))throw _;return"function"==typeof config.onRetry&&config.onRetry(U),ensureChunk.apply(void 0,[n,r,e,i,t,o,a,l,c].concat(u)).then((function(n){var r=(null==c?void 0:c.count)===s+2;if("function"==typeof config.onSuccess&&r){var e=E(s+1);config.onSuccess(e)}return n}))})))}function loadScript(n,r,e,i){for(var t=globalCurrRetrying[i],o=arguments.length,a=new Array(o>4?o-4:0),l=4;l<o;l++)a[l-4]=arguments[l];return originalLoadScript.apply(void 0,[t?t.nextRetryUrl:n,r,e,i].concat(a))}function registerAsyncChunkRetry(){if("undefined"==typeof window||window.__RB_ASYNC_CHUNKS__||(window.__RB_ASYNC_CHUNKS__={}),"undefined"!=typeof __RUNTIME_GLOBALS_REQUIRE__)try{__RUNTIME_GLOBALS_ENSURE_CHUNK__=ensureChunk,__RUNTIME_GLOBALS_LOAD_SCRIPT__=loadScript}catch(n){console.error(ERROR_PREFIX,"Register async chunk retry runtime failed",n)}}registerAsyncChunkRetry();
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsbuild/plugin-assets-retry",
|
|
3
|
-
"version": "1.0.
|
|
3
|
+
"version": "1.0.5",
|
|
4
4
|
"description": "Assets retry plugin for Rsbuild",
|
|
5
5
|
"homepage": "https://rsbuild.dev",
|
|
6
6
|
"repository": {
|
|
@@ -23,17 +23,18 @@
|
|
|
23
23
|
"dist"
|
|
24
24
|
],
|
|
25
25
|
"devDependencies": {
|
|
26
|
-
"@babel/core": "^7.
|
|
27
|
-
"@babel/preset-env": "^7.
|
|
28
|
-
"@babel/preset-typescript": "^7.
|
|
26
|
+
"@babel/core": "^7.26.0",
|
|
27
|
+
"@babel/preset-env": "^7.26.0",
|
|
28
|
+
"@babel/preset-typescript": "^7.26.0",
|
|
29
|
+
"@rslib/core": "0.0.18",
|
|
29
30
|
"@types/serialize-javascript": "^5.0.4",
|
|
30
31
|
"serialize-javascript": "^6.0.2",
|
|
31
|
-
"terser": "5.
|
|
32
|
+
"terser": "5.36.0",
|
|
32
33
|
"typescript": "^5.6.3",
|
|
33
|
-
"@rsbuild/core": "1.
|
|
34
|
+
"@rsbuild/core": "1.1.4"
|
|
34
35
|
},
|
|
35
36
|
"peerDependencies": {
|
|
36
|
-
"@rsbuild/core": "1.x
|
|
37
|
+
"@rsbuild/core": "1.x"
|
|
37
38
|
},
|
|
38
39
|
"publishConfig": {
|
|
39
40
|
"access": "public",
|