@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 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 currently used domain, 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.
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
- pluginAssetsRetry({
103
- domain: ["https://cdn1.com", "https://cdn2.com", "https://cdn3.com"],
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` 数组中,第一项是当前使用的域名,后面几项为备用域名。当某个域名的资源请求失败时,Rsbuild 会在数组中找到该域名,并替换为数组的下一个域名。
95
+ 指定资源加载失败时的重试域名列表。在 `domain` 数组中,第一项是静态资源默认所在的域名,后面几项为备用域名。当某个域名的资源请求失败时,Rsbuild 会在数组中找到该域名,并替换为数组的下一个域名。
96
96
 
97
97
  比如:
98
98
 
99
99
  ```js
100
- pluginAssetsRetry({
101
- domain: ["https://cdn1.com", "https://cdn2.com", "https://cdn3.com"],
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-0.js");
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-0.js");
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.startsWith('/') ? window.origin + originalPublicPath + originalScriptFilename : originalPublicPath + originalScriptFilename;
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 = ((_config_domain = config.domain) === null || _config_domain === void 0 ? void 0 : _config_domain[0]) || window.origin;
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={},i={},t=/[?#].*$/;function o(r,e,i,o,_){var a;return r.replace(e,i).replace(t,"")+(a=o+1,!0===n.addQuery?""!==_?"".concat(_,"&retry=").concat(a):"?retry=".concat(a):"function"==typeof n.addQuery?n.addQuery({times:a,originalQuery:_}):"")}var _={},a={},l=__RUNTIME_GLOBALS_ENSURE_CHUNK__,c=__RUNTIME_GLOBALS_GET_CHUNK_SCRIPT_FILENAME__,u=__RUNTIME_GLOBALS_GET_MINI_CSS_EXTRACT_FILENAME__||__RUNTIME_GLOBALS_GET_CSS_FILENAME__||function(){return null},d=__RUNTIME_GLOBALS_LOAD_SCRIPT__,s="[@rsbuild/plugin-assets-retry] ";function f(t){var d=Array.prototype.slice.call(arguments);d[10]||(d[10]={count:0,cssFailedCount:0});var y=d[10],S=l.apply(null,d);try{var v=c(t),R=u(t);"undefined"!=typeof window&&(v&&(window.__RB_ASYNC_CHUNKS__[v]=!0),R&&(window.__RB_ASYNC_CHUNKS__[R]=!0))}catch(n){console.error(s,"get original script or CSS filename error",n)}return y&&"number"==typeof y.count&&"number"==typeof y.cssFailedCount?(y.count+=1,S.catch(function(l){var S,v,R,p,E,U,L=y.count-1,m=y.cssFailedCount,g=!!(null==l?void 0:null===(S=l.message)||void 0===S?void 0:S.includes("CSS chunk"));g&&(y.cssFailedCount+=1);var N=g?m:L-m;try{var A=function(r,t,l){var d,s,f,y=l?null===(d=i[r])||void 0===d?void 0:d[t]:null===(s=e[r])||void 0===s?void 0:s[t],S=t+1;if(0===t||void 0===y)f=function(r,e){var i,t,_=e?u(r):c(r);if(!_)throw Error("only support cssExtract");var a=__RUNTIME_GLOBALS_PUBLIC_PATH__,l=a.startsWith("/")?window.origin+a+_:a+_,d=(i=l.split("?")[1])?"?".concat(i.split("#")[0]):"",s=(null===(t=n.domain)||void 0===t?void 0:t[0])||window.origin;return{nextDomain:s,nextRetryUrl:o(l,s,s,0,d),originalScriptFilename:_,originalSrcUrl:l,originalQuery:d}}(r,l),l?i[r]=[]:e[r]=[];else{var v,R,p,E,U=y.originalScriptFilename,L=y.originalSrcUrl,m=y.originalQuery,g=(v=y.nextDomain,R=n.domain||[],p=function(r){for(var e=n.domain||[],i="",t=0;t<e.length;t++)if(-1!==r.indexOf(e[t])){i=e[t];break}return i||window.origin}(v),E=R.indexOf(p),R[(E+1)%R.length]||v);f={nextDomain:g,nextRetryUrl:o(y.nextRetryUrl,y.nextDomain,g,t,m),originalScriptFilename:U,originalSrcUrl:L,originalQuery:m}}return l?(i[r][S]=f,a[r]=f):(e[r][S]=f,_[r]=f),f}(t,N,g);v=A.originalScriptFilename,R=A.nextRetryUrl,p=A.nextDomain}catch(n){throw console.error(s,"failed to get nextRetryUrl",n),l}var C=function(n){return{times:n,domain:p,url:R,tagName:g?"link":"script",isAsyncChunk:!0}},T=C(N);if(N>=r)throw l.message=(null===(E=l.message)||void 0===E?void 0:E.includes("retries:"))?l.message:"Loading chunk ".concat(t,' from "').concat(v,'" failed after ').concat(r,' retries: "').concat(l.message,'"'),"function"==typeof n.onFail&&n.onFail(T),l;var w=n.test;if(w){if("string"==typeof w){var I=new RegExp(w);w=function(n){return I.test(n)}}if("function"!=typeof w||!w(R))throw l}if(n.domain&&-1===n.domain.indexOf(p))throw l;"function"==typeof n.onRetry&&n.onRetry(T);var O="function"==typeof n.delay?n.delay(T):null!==(U=n.delay)&&void 0!==U?U:0;return(O>0?new Promise(function(n){return setTimeout(n,O)}):Promise.resolve()).then(function(){return f.apply(f,d)}).then(function(r){var e=(null==y?void 0:y.count)===L+2;if("function"==typeof n.onSuccess&&e){var i=C(N+1);n.onSuccess(i)}return r})})):S}function y(){var n=Array.prototype.slice.call(arguments),r=_[n[3]];return r&&(n[0]=r.nextRetryUrl),d.apply(null,n)}function S(n,r){var e=a[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__=f,__RUNTIME_GLOBALS_LOAD_SCRIPT__=y,__RUNTIME_GLOBALS_RSBUILD_LOAD_STYLESHEET__=S}catch(n){console.error(s,"Register async chunk retry runtime failed",n)}}();
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)}}();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@rsbuild/plugin-assets-retry",
3
- "version": "1.2.0",
3
+ "version": "1.2.1",
4
4
  "repository": "https://github.com/rspack-contrib/rsbuild-plugin-assets-retry",
5
5
  "license": "MIT",
6
6
  "type": "module",