@rsbuild/plugin-assets-retry 1.0.0-alpha.8 → 1.0.0-alpha.9
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
CHANGED
|
@@ -106,7 +106,7 @@ var init_AssetsRetryPlugin = __esm({
|
|
|
106
106
|
if (!this.scriptPath) {
|
|
107
107
|
this.scriptPath = import_node_path.default.posix.join(
|
|
108
108
|
this.distDir,
|
|
109
|
-
`assets-retry.${"1.0.0-alpha.
|
|
109
|
+
`assets-retry.${"1.0.0-alpha.9"}.js`
|
|
110
110
|
);
|
|
111
111
|
}
|
|
112
112
|
return this.scriptPath;
|
package/dist/index.js
CHANGED
|
@@ -21,7 +21,7 @@ function findCurrentDomain(url) {
|
|
|
21
21
|
break;
|
|
22
22
|
}
|
|
23
23
|
}
|
|
24
|
-
return domain ||
|
|
24
|
+
return domain || window.origin;
|
|
25
25
|
}
|
|
26
26
|
function findNextDomain(url) {
|
|
27
27
|
var _config$domain2;
|
|
@@ -50,8 +50,28 @@ function getUrlRetryQuery(existRetryTimes, originalQuery) {
|
|
|
50
50
|
}
|
|
51
51
|
return '';
|
|
52
52
|
}
|
|
53
|
-
function
|
|
54
|
-
|
|
53
|
+
function removeDomainFromUrl(url) {
|
|
54
|
+
var protocolStartIndex = url.indexOf('//');
|
|
55
|
+
|
|
56
|
+
// case /app/main/static/js/index.js
|
|
57
|
+
if (protocolStartIndex === -1 && url.startsWith('/')) {
|
|
58
|
+
return url;
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
// case "//cdn.com/app/main/static/js/index.js"
|
|
62
|
+
// case "http://cdn.com/app/main/static/js/index.js"
|
|
63
|
+
var protocolEndIndex = protocolStartIndex + 2;
|
|
64
|
+
var pathStartIndex = url.indexOf('/', protocolEndIndex);
|
|
65
|
+
return url.slice(pathStartIndex);
|
|
66
|
+
}
|
|
67
|
+
|
|
68
|
+
// "http://cdn.com/app/main/static/js/index.js?query=1#hash" -> "/app/main/static/js/index.js"
|
|
69
|
+
function getAbsolutePathFromUrl(url) {
|
|
70
|
+
return cleanUrl(removeDomainFromUrl(url));
|
|
71
|
+
}
|
|
72
|
+
function getNextRetryUrl(existRetryTimes, nextDomain, originalSrcUrl) {
|
|
73
|
+
var absolutePath = getAbsolutePathFromUrl(originalSrcUrl);
|
|
74
|
+
return nextDomain + absolutePath + getUrlRetryQuery(existRetryTimes, getQueryFromUrl(originalSrcUrl));
|
|
55
75
|
}
|
|
56
76
|
function getCurrentRetry(chunkId) {
|
|
57
77
|
return retryCollector[chunkId];
|
|
@@ -61,11 +81,11 @@ function initRetry(chunkId) {
|
|
|
61
81
|
var originalScriptFilename = originalGetChunkScriptFilename(chunkId);
|
|
62
82
|
var originalSrcUrl = __RUNTIME_GLOBALS_PUBLIC_PATH__ + originalScriptFilename;
|
|
63
83
|
var existRetryTimes = 1;
|
|
64
|
-
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$ :
|
|
84
|
+
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;
|
|
65
85
|
return {
|
|
66
86
|
existRetryTimes: existRetryTimes,
|
|
67
87
|
nextDomain: nextDomain,
|
|
68
|
-
nextRetryUrl: getNextRetryUrl(existRetryTimes, nextDomain, originalSrcUrl
|
|
88
|
+
nextRetryUrl: getNextRetryUrl(existRetryTimes, nextDomain, originalSrcUrl),
|
|
69
89
|
originalScriptFilename: originalScriptFilename,
|
|
70
90
|
originalSrcUrl: originalSrcUrl
|
|
71
91
|
};
|
|
@@ -83,7 +103,7 @@ function nextRetry(chunkId) {
|
|
|
83
103
|
nextRetry = {
|
|
84
104
|
existRetryTimes: existRetryTimes,
|
|
85
105
|
nextDomain: nextDomain,
|
|
86
|
-
nextRetryUrl: getNextRetryUrl(existRetryTimes, nextDomain, originalSrcUrl
|
|
106
|
+
nextRetryUrl: getNextRetryUrl(existRetryTimes, nextDomain, originalSrcUrl),
|
|
87
107
|
originalScriptFilename: originalScriptFilename,
|
|
88
108
|
originalSrcUrl: originalSrcUrl
|
|
89
109
|
};
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var _ref,_RUNTIME_GLOBALS_GET,config=__RETRY_OPTIONS__,maxRetries=config.max||3,retryCollector={};function findCurrentDomain(n){for(var e
|
|
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{existRetryTimes:1,nextDomain:o,nextRetryUrl:getNextRetryUrl(1,o,t),originalScriptFilename:i,originalSrcUrl:t}}function nextRetry(n){var r,e=getCurrentRetry(n);if(e){var i=e.originalScriptFilename,t=e.originalSrcUrl,o=e.existRetryTimes+1,_=findNextDomain(e.nextDomain);r={existRetryTimes:o,nextDomain:_,nextRetryUrl:getNextRetryUrl(o,_,t),originalScriptFilename:i,originalSrcUrl:t}}else r=initRetry(n);return retryCollector[n]=r,r}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__;function ensureChunk(n){var r=originalEnsureChunk(n),e=originalGetChunkScriptFilename(n),i=originalGetCssFilename(n);return"undefined"!=typeof window&&(e&&(window.__RB_ASYNC_CHUNKS__[e]=!0),i&&(window.__RB_ASYNC_CHUNKS__[i]=!0)),r.catch((function(r){var e,i=nextRetry(n),t=i.existRetryTimes,o=i.originalSrcUrl,_=i.nextRetryUrl,a=i.nextDomain,u=Boolean(null==r||null===(e=r.message)||void 0===e?void 0:e.includes("CSS chunk")),c=function(n){return{times:n,domain:a,url:_,tagName:u?"link":"script",isAsyncChunk:!0}},l=c(t-1);if(t>maxRetries)throw r.message="Loading chunk ".concat(n," from ").concat(o," failed after ").concat(maxRetries,' retries: "').concat(r.message,'"'),"function"==typeof config.onFail&&config.onFail(l),r;var f=config.test;if(f){if("string"==typeof f){var s=new RegExp(f);f=function(n){return s.test(n)}}if("function"!=typeof f||!f(_))throw r}if(config.domain&&config.domain.length>0&&-1===config.domain.indexOf(a))throw r;return"function"==typeof config.onRetry&&config.onRetry(l),ensureChunk(n).then((function(r){if("function"==typeof config.onSuccess){var e,i=c(t);(null!==(e=getCurrentRetry(n))&&void 0!==e?e:{}).existRetryTimes===t&&config.onSuccess(i)}return r}))}))}function loadScript(n,r,e,i){var t=getCurrentRetry(i);return originalLoadScript(t?t.nextRetryUrl:n,r,e,i)}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("[@rsbuild/plugin-assets-retry] Register async chunk retry runtime failed",n)}}registerAsyncChunkRetry();
|
|
@@ -1 +1 @@
|
|
|
1
|
-
"use strict";var TAG_TYPE={link:HTMLLinkElement,script:HTMLScriptElement,img:HTMLImageElement},TYPES=Object.keys(TAG_TYPE);function findCurrentDomain(e,n){for(var t="",r=0;r<n.length;r++)if(-1!==e.indexOf(n[r])){t=n[r];break}return t||
|
|
1
|
+
"use strict";var TAG_TYPE={link:HTMLLinkElement,script:HTMLScriptElement,img:HTMLImageElement},TYPES=Object.keys(TAG_TYPE);function findCurrentDomain(e,n){for(var t="",r=0;r<n.length;r++)if(-1!==e.indexOf(n[r])){t=n[r];break}return t||window.origin}function findNextDomain(e,n){var t=findCurrentDomain(e,n),r=n.indexOf(t);return n[(r+1)%n.length]||e}function getRequestUrl(e){return e instanceof HTMLScriptElement||e instanceof HTMLImageElement?e.src:e instanceof HTMLLinkElement?e.href:null}var defaultConfig={max:3,type:TYPES,domain:[],crossOrigin:!1};function validateTargetInfo(e,n){var t,r=n.target,i=null===(t=r.tagName)||void 0===t?void 0:t.toLocaleLowerCase(),a=e.type,o=getRequestUrl(r);return!!(i&&-1!==a.indexOf(i)&&TAG_TYPE[i]&&r instanceof TAG_TYPE[i]&&o)&&{target:r,tagName:i,url:o}}var postfixRE=/[?#].*$/;function cleanUrl(e){return e.replace(postfixRE,"")}function getQueryFromUrl(e){var n=e.split("?")[1];return n?"?".concat(n.split("#")[0]):""}function createElement(e,n){var t=!0===n.crossOrigin?"anonymous":n.crossOrigin,r=t?'crossorigin="'.concat(t,'"'):"",i=n.times?'data-rsbuild-retry-times="'.concat(n.times,'"'):"",a=n.originalQuery?'data-rsbuild-original-query="'.concat(n.originalQuery,'"'):"",o=n.isAsync?"data-rsbuild-async":"";if(e instanceof HTMLScriptElement){var c=document.createElement("script");return c.src=n.url,t&&(c.crossOrigin=t),n.times&&(c.dataset.rsbuildRetryTimes=String(n.times)),n.isAsync&&(c.dataset.rsbuildAsync=""),void 0!==n.originalQuery&&(c.dataset.rsbuildOriginalQuery=n.originalQuery),{element:c,str:'<script src="'.concat(n.url,'" ').concat(r," ").concat(i," ").concat(o," ").concat(a,">")+"<\/script>"}}if(e instanceof HTMLLinkElement){var s=document.createElement("link");return s.rel=e.rel||"stylesheet",e.as&&(s.as=e.as),s.href=n.url,t&&(s.crossOrigin=t),n.times&&(s.dataset.rsbuildRetryTimes=String(n.times)),void 0!==n.originalQuery&&(s.dataset.rsbuildOriginalQuery=n.originalQuery),{element:s,str:'<link rel="'.concat(s.rel,'" href="').concat(n.url,'" ').concat(r," ").concat(i," ").concat(s.as?'as="'.concat(s.as,'"'):""," ").concat(a,"></link>")}}}function reloadElementResource(e,n,t){e instanceof HTMLScriptElement&&(t.isAsync?document.body.appendChild(n.element):document.write(n.str)),e instanceof HTMLLinkElement&&document.getElementsByTagName("head")[0].appendChild(n.element),e instanceof HTMLImageElement&&(e.src=t.url,e.dataset.rsbuildRetryTimes=String(t.times),e.dataset.rsbuildOriginalQuery=String(t.originalQuery))}function retry(e,n){var t,r=validateTargetInfo(e,n);if(!1!==r){var i=r.target,a=r.tagName,o=r.url;if("undefined"==typeof window||!Object.keys(window.__RB_ASYNC_CHUNKS__||{}).some((function(e){return-1!==o.indexOf(e)}))){var c=e.test;if(c){if("string"==typeof c){var s=new RegExp(c);c=function(e){return s.test(e)}}if("function"!=typeof c||!c(o))return}var l=findCurrentDomain(o,e.domain);if(!(e.domain&&e.domain.length>0&&-1===e.domain.indexOf(l))){var u=Number(i.dataset.rsbuildRetryTimes)||0;if(u!==e.max){var d=findNextDomain(l,e.domain),f=null!==(t=i.dataset.rsbuildOriginalQuery)&&void 0!==t?t:getQueryFromUrl(o),m=Boolean(i.dataset.rsbuildAsync)||i.async||i.defer,y={url:cleanUrl(o.replace(l,d))+function(n){return!0===e.addQuery?""!==f?"".concat(f,"&retry=").concat(n):"?retry=".concat(n):"function"==typeof e.addQuery?e.addQuery({times:n,originalQuery:f}):""}(u+1),times:u+1,crossOrigin:e.crossOrigin,isAsync:m,originalQuery:f},g=createElement(i,y);if(e.onRetry&&"function"==typeof e.onRetry){var p={times:u,domain:l,url:o,tagName:a,isAsyncChunk:!1};e.onRetry(p)}reloadElementResource(i,g,y)}else if("function"==typeof e.onFail){var v={times:u,domain:l,url:o,tagName:a,isAsyncChunk:!1};e.onFail(v)}}}}}function load(e,n){var t=validateTargetInfo(e,n);if(!1!==t){var r=t.target,i=t.tagName,a=t.url,o=findCurrentDomain(a,e.domain),c=Number(r.dataset.rsbuildRetryTimes)||0;if(0!==c&&"function"==typeof e.onSuccess){var s={times:c,domain:o,url:a,tagName:i,isAsyncChunk:!1};e.onSuccess(s)}}}function resourceMonitor(e,n){"undefined"!=typeof window&&void 0!==window.document&&(document.addEventListener("error",(function(n){n&&n.target instanceof Element&&e(n)}),!0),document.addEventListener("load",(function(e){e&&e.target instanceof Element&&n(e)}),!0))}function init(e){var n={};for(var t in defaultConfig)n[t]=defaultConfig[t];for(var r in e)n[r]=e[r];Array.isArray(n.type)&&0!==n.type.length||(n.type=defaultConfig.type),Array.isArray(n.domain)&&0!==n.domain.length||(n.domain=defaultConfig.domain),Array.isArray(n.domain)&&(n.domain=n.domain.filter(Boolean)),"undefined"==typeof window||window.__RB_ASYNC_CHUNKS__||(window.__RB_ASYNC_CHUNKS__={});try{resourceMonitor((function(e){try{retry(n,e)}catch(e){console.error("retry error captured",e)}}),(function(e){try{load(n,e)}catch(e){console.error("load error captured",e)}}))}catch(e){console.error("monitor error captured",e)}}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@rsbuild/plugin-assets-retry",
|
|
3
|
-
"version": "1.0.0-alpha.
|
|
3
|
+
"version": "1.0.0-alpha.9",
|
|
4
4
|
"description": "Assets retry plugin for Rsbuild",
|
|
5
5
|
"homepage": "https://rsbuild.dev",
|
|
6
6
|
"repository": {
|
|
@@ -32,10 +32,10 @@
|
|
|
32
32
|
"@types/serialize-javascript": "^5.0.4",
|
|
33
33
|
"terser": "5.31.1",
|
|
34
34
|
"typescript": "^5.5.2",
|
|
35
|
-
"@rsbuild/core": "1.0.0-alpha.
|
|
35
|
+
"@rsbuild/core": "1.0.0-alpha.9"
|
|
36
36
|
},
|
|
37
37
|
"peerDependencies": {
|
|
38
|
-
"@rsbuild/core": "^1.0.0-alpha.
|
|
38
|
+
"@rsbuild/core": "^1.0.0-alpha.9"
|
|
39
39
|
},
|
|
40
40
|
"publishConfig": {
|
|
41
41
|
"access": "public",
|