@rsbuild/plugin-assets-retry 1.2.0 → 1.2.1
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/README.md +11 -3
- package/README.zh-CN.md +11 -3
- package/dist/index.cjs +1 -1
- package/dist/index.js +1 -1
- package/dist/runtime/asyncChunkRetry.js +2 -3
- package/dist/runtime/asyncChunkRetry.min.js +1 -1
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -94,13 +94,21 @@ const defaultAssetsRetryOptions = {
|
|
|
94
94
|
- **Type:** `string[]`
|
|
95
95
|
- **Default:** `[]`
|
|
96
96
|
|
|
97
|
-
Specifies the retry domain when assets fail to load. In the `domain` array, the first item is the
|
|
97
|
+
Specifies the retry domain when assets fail to load. In the `domain` array, the first item is the default domain of static assets, and the following items are backup domains. When a asset request for a domain fails, Rsbuild will find that domain in the array and replace it with the next domain in the array.
|
|
98
98
|
|
|
99
99
|
For example:
|
|
100
100
|
|
|
101
101
|
```js
|
|
102
|
-
|
|
103
|
-
|
|
102
|
+
// rsbuild.config.ts
|
|
103
|
+
defineConfig({
|
|
104
|
+
plugins: [
|
|
105
|
+
pluginAssetsRetry({
|
|
106
|
+
domain: ["cdn1.com", "cdn2.com", "cdn3.com"],
|
|
107
|
+
})
|
|
108
|
+
],
|
|
109
|
+
output: {
|
|
110
|
+
assetPrefix: "https://cdn1.com", // or "//cdn1.com"
|
|
111
|
+
},
|
|
104
112
|
});
|
|
105
113
|
```
|
|
106
114
|
|
package/README.zh-CN.md
CHANGED
|
@@ -92,13 +92,21 @@ const defaultOptions = {
|
|
|
92
92
|
- **类型:** `string[]`
|
|
93
93
|
- **默认值:** `[]`
|
|
94
94
|
|
|
95
|
-
指定资源加载失败时的重试域名列表。在 `domain`
|
|
95
|
+
指定资源加载失败时的重试域名列表。在 `domain` 数组中,第一项是静态资源默认所在的域名,后面几项为备用域名。当某个域名的资源请求失败时,Rsbuild 会在数组中找到该域名,并替换为数组的下一个域名。
|
|
96
96
|
|
|
97
97
|
比如:
|
|
98
98
|
|
|
99
99
|
```js
|
|
100
|
-
|
|
101
|
-
|
|
100
|
+
// rsbuild.config.ts
|
|
101
|
+
defineConfig({
|
|
102
|
+
plugins: [
|
|
103
|
+
pluginAssetsRetry({
|
|
104
|
+
domain: ["cdn1.com", "cdn2.com", "cdn3.com"],
|
|
105
|
+
})
|
|
106
|
+
],
|
|
107
|
+
output: {
|
|
108
|
+
assetPrefix: "https://cdn1.com", // 或者 "//cdn1.com"
|
|
109
|
+
},
|
|
102
110
|
});
|
|
103
111
|
```
|
|
104
112
|
|
package/dist/index.cjs
CHANGED
|
@@ -306,7 +306,7 @@ var __webpack_exports__ = {};
|
|
|
306
306
|
const { inlineScript = true } = userOptions;
|
|
307
307
|
const getScriptPath = (environment)=>{
|
|
308
308
|
const distDir = environment.config.output.distPath.js;
|
|
309
|
-
return external_node_path_default().posix.join(distDir, "assets-retry.1-2-
|
|
309
|
+
return external_node_path_default().posix.join(distDir, "assets-retry.1-2-1.js");
|
|
310
310
|
};
|
|
311
311
|
const formatOptions = (config)=>{
|
|
312
312
|
const options = {
|
package/dist/index.js
CHANGED
|
@@ -282,7 +282,7 @@ const pluginAssetsRetry = (userOptions = {})=>({
|
|
|
282
282
|
const { inlineScript = true } = userOptions;
|
|
283
283
|
const getScriptPath = (environment)=>{
|
|
284
284
|
const distDir = environment.config.output.distPath.js;
|
|
285
|
-
return __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].posix.join(distDir, "assets-retry.1-2-
|
|
285
|
+
return __WEBPACK_EXTERNAL_MODULE_node_path_c5b9b54f__["default"].posix.join(distDir, "assets-retry.1-2-1.js");
|
|
286
286
|
};
|
|
287
287
|
const formatOptions = (config)=>{
|
|
288
288
|
const options = {
|
|
@@ -53,16 +53,15 @@ function getCurrentRetry(chunkId, existRetryTimes, isCssAsyncChunk) {
|
|
|
53
53
|
return isCssAsyncChunk ? (_retryCssCollector_chunkId = retryCssCollector[chunkId]) === null || _retryCssCollector_chunkId === void 0 ? void 0 : _retryCssCollector_chunkId[existRetryTimes] : (_retryCollector_chunkId = retryCollector[chunkId]) === null || _retryCollector_chunkId === void 0 ? void 0 : _retryCollector_chunkId[existRetryTimes];
|
|
54
54
|
}
|
|
55
55
|
function initRetry(chunkId, isCssAsyncChunk) {
|
|
56
|
-
var _config_domain;
|
|
57
56
|
var originalScriptFilename = isCssAsyncChunk ? originalGetCssFilename(chunkId) : originalGetChunkScriptFilename(chunkId);
|
|
58
57
|
if (!originalScriptFilename) {
|
|
59
58
|
throw new Error('only support cssExtract');
|
|
60
59
|
}
|
|
61
60
|
var originalPublicPath = __RUNTIME_GLOBALS_PUBLIC_PATH__;
|
|
62
|
-
var originalSrcUrl = originalPublicPath
|
|
61
|
+
var originalSrcUrl = originalPublicPath[0] === '/' && originalPublicPath[1] !== '/' ? window.origin + originalPublicPath + originalScriptFilename : originalPublicPath + originalScriptFilename;
|
|
63
62
|
var originalQuery = getQueryFromUrl(originalSrcUrl);
|
|
64
63
|
var existRetryTimes = 0;
|
|
65
|
-
var nextDomain = (
|
|
64
|
+
var nextDomain = findCurrentDomain(originalSrcUrl);
|
|
66
65
|
return {
|
|
67
66
|
nextDomain: nextDomain,
|
|
68
67
|
nextRetryUrl: getNextRetryUrl(originalSrcUrl, nextDomain, nextDomain, existRetryTimes, originalQuery),
|
|
@@ -1 +1 @@
|
|
|
1
|
-
var n=__RETRY_OPTIONS__,r=n.max||3,e={},
|
|
1
|
+
var n=__RETRY_OPTIONS__,r=n.max||3,e={},t={};function i(r){for(var e=n.domain||[],t="",i=0;i<e.length;i++)if(-1!==r.indexOf(e[i])){t=e[i];break}return t||window.origin}var o=/[?#].*$/;function _(r,e,t,i,_){var a;return r.replace(e,t).replace(o,"")+(a=i+1,!0===n.addQuery?""!==_?"".concat(_,"&retry=").concat(a):"?retry=".concat(a):"function"==typeof n.addQuery?n.addQuery({times:a,originalQuery:_}):"")}var a={},l={},c=__RUNTIME_GLOBALS_ENSURE_CHUNK__,u=__RUNTIME_GLOBALS_GET_CHUNK_SCRIPT_FILENAME__,f=__RUNTIME_GLOBALS_GET_MINI_CSS_EXTRACT_FILENAME__||__RUNTIME_GLOBALS_GET_CSS_FILENAME__||function(){return null},s=__RUNTIME_GLOBALS_LOAD_SCRIPT__,d="[@rsbuild/plugin-assets-retry] ";function y(o){var s=Array.prototype.slice.call(arguments);s[10]||(s[10]={count:0,cssFailedCount:0});var S=s[10],R=c.apply(null,s);try{var p=u(o),v=f(o);"undefined"!=typeof window&&(p&&(window.__RB_ASYNC_CHUNKS__[p]=!0),v&&(window.__RB_ASYNC_CHUNKS__[v]=!0))}catch(n){console.error(d,"get original script or CSS filename error",n)}return S&&"number"==typeof S.count&&"number"==typeof S.cssFailedCount?(S.count+=1,R.catch(function(c){var R,p,v,E,U,L,m=S.count-1,N=S.cssFailedCount,g=!!(null==c?void 0:null===(R=c.message)||void 0===R?void 0:R.includes("CSS chunk"));g&&(S.cssFailedCount+=1);var A=g?N:m-N;try{var C=function(r,o,c){var s,d,y,S=c?null===(s=t[r])||void 0===s?void 0:s[o]:null===(d=e[r])||void 0===d?void 0:d[o],R=o+1;if(0===o||void 0===S)y=function(n,r){var e,t=r?f(n):u(n);if(!t)throw Error("only support cssExtract");var o=__RUNTIME_GLOBALS_PUBLIC_PATH__,a="/"===o[0]&&"/"!==o[1]?window.origin+o+t:o+t,l=(e=a.split("?")[1])?"?".concat(e.split("#")[0]):"",c=i(a);return{nextDomain:c,nextRetryUrl:_(a,c,c,0,l),originalScriptFilename:t,originalSrcUrl:a,originalQuery:l}}(r,c),c?t[r]=[]:e[r]=[];else{var p,v,E,U,L=S.originalScriptFilename,m=S.originalSrcUrl,N=S.originalQuery,g=(p=S.nextDomain,v=n.domain||[],E=i(p),U=v.indexOf(E),v[(U+1)%v.length]||p);y={nextDomain:g,nextRetryUrl:_(S.nextRetryUrl,S.nextDomain,g,o,N),originalScriptFilename:L,originalSrcUrl:m,originalQuery:N}}return c?(t[r][R]=y,l[r]=y):(e[r][R]=y,a[r]=y),y}(o,A,g);p=C.originalScriptFilename,v=C.nextRetryUrl,E=C.nextDomain}catch(n){throw console.error(d,"failed to get nextRetryUrl",n),c}var T=function(n){return{times:n,domain:E,url:v,tagName:g?"link":"script",isAsyncChunk:!0}},I=T(A);if(A>=r)throw c.message=(null===(U=c.message)||void 0===U?void 0:U.includes("retries:"))?c.message:"Loading chunk ".concat(o,' from "').concat(p,'" failed after ').concat(r,' retries: "').concat(c.message,'"'),"function"==typeof n.onFail&&n.onFail(I),c;var w=n.test;if(w){if("string"==typeof w){var O=new RegExp(w);w=function(n){return O.test(n)}}if("function"!=typeof w||!w(v))throw c}if(n.domain&&-1===n.domain.indexOf(E))throw c;"function"==typeof n.onRetry&&n.onRetry(I);var x="function"==typeof n.delay?n.delay(I):null!==(L=n.delay)&&void 0!==L?L:0;return(x>0?new Promise(function(n){return setTimeout(n,x)}):Promise.resolve()).then(function(){return y.apply(y,s)}).then(function(r){var e=(null==S?void 0:S.count)===m+2;if("function"==typeof n.onSuccess&&e){var t=T(A+1);n.onSuccess(t)}return r})})):R}function S(){var n=Array.prototype.slice.call(arguments),r=a[n[3]];return r&&(n[0]=r.nextRetryUrl),s.apply(null,n)}function R(n,r){var e=l[r];return e&&e.nextRetryUrl||__RUNTIME_GLOBALS_PUBLIC_PATH__+n}!function(){if("undefined"==typeof window||window.__RB_ASYNC_CHUNKS__||(window.__RB_ASYNC_CHUNKS__={}),"undefined"!=typeof __RUNTIME_GLOBALS_REQUIRE__)try{__RUNTIME_GLOBALS_ENSURE_CHUNK__=y,__RUNTIME_GLOBALS_LOAD_SCRIPT__=S,__RUNTIME_GLOBALS_RSBUILD_LOAD_STYLESHEET__=R}catch(n){console.error(d,"Register async chunk retry runtime failed",n)}}();
|