Gathering detailed insights and metrics for webpack-plugin-import-retry
Gathering detailed insights and metrics for webpack-plugin-import-retry
Gathering detailed insights and metrics for webpack-plugin-import-retry
Gathering detailed insights and metrics for webpack-plugin-import-retry
webpack plugin to modify webpack_require module to retry import async chunk again
npm install webpack-plugin-import-retry
Typescript
Module System
Node Version
NPM Version
JavaScript (98.43%)
HTML (1.23%)
CSS (0.33%)
Total Downloads
0
Last Day
0
Last Week
0
Last Month
0
Last Year
0
MIT License
22 Stars
5 Commits
4 Forks
2 Watchers
1 Branches
1 Contributors
Updated on Sep 19, 2024
Latest Version
1.0.2
Package Id
webpack-plugin-import-retry@1.0.2
Unpacked Size
9.76 kB
Size
3.84 kB
File Count
4
NPM Version
5.6.0
Node Version
8.11.3
Cumulative downloads
Total Downloads
Last Day
0%
NaN
Compared to previous day
Last Week
0%
NaN
Compared to previous week
Last Month
0%
NaN
Compared to previous month
Last Year
0%
NaN
Compared to previous year
webpack plugin to modify webpack_require module to retry import async chunk again
webpack
插件,修改 webpack
内置的脚本加载器,达到 import
时,如果加载异常,进行资源请求重试。
With npm
1npm install webpack-plugin-import-retry --save-dev
With yarn
1yarn add webpack-plugin-import-retry --dev
Just add this plugin as usual.
1// webpack.config.js 2const importRetry = require("webpack-plugin-import-retry"); 3 4module.exports = { 5 // ... 6 output: { 7 //... 8 filename: "[name].[chunkhash].js" 9 }, 10 plugins: [new importRetry(options)] 11};
许多时候用户网络较差,导致请求资源时失败,在用户量较大的时候这个问题会更加明显。
同时也会因为各种问题导致 CDN 域名不可访问,此时可降级请求当前域名的资源,可访问成功。
因此,资源重试显得非常有必要,这个事情可以通过封装 import()
完成,大家共同调用封装后的函数,但是需要大家都遵守规范。
这种方式也不利于使用 注释的功能 即 import(/* webpackChunkName: "floatIndex" */'./views/floatIndex.vue')
。
因此,更好的解决方案,可以采用改写webpack
异步加载的 requireEnsure
函数来实现。
本插件即改写了 requireEnsure
来实现,资源加载失败,重试 2 次,第二次采用当前域名的方式来实现。
针对一个chunk
包含css
与js
两块资源的时候,两者任一资源重试 2 次失败则不再进行重试。
有值且未传入 fmtRequireUrlFuncStr
时,默认第二次重载时将 cdnDomain
替换为 location.hostname
格式化资源 URL
,传入则会替换默认的格式化函数。
入参
1// 默认的函数 2function(htmlNode, chunkId, __webpack_require__, options){ 3 var modifyReloadQry = function(url, reloadTimes){ 4 if(/reloadAssets=(\d+)&?/.test(url)){ 5 return url.replace('reloadAssets='+reloadTimes, 'reloadAssets='+(1+reloadTimes)); 6 } 7 return url + (url.indexOf('?')!=-1 ? '&':'?') + 'reloadAssets='+(reloadTimes+1); 8 } 9 options = options || { 10 LINK: 0, 11 SCRIPT: 0 12 }; 13 var nodeName = htmlNode.nodeName; 14 var reloadTimes = options[nodeName] || 0; 15 var linkKey = 'src'; 16 if(nodeName =='LINK'){ 17 linkKey = 'href'; 18 } 19 if(!htmlNode[linkKey]) return; 20 if(reloadTimes == 0 || reloadTimes > 2) return; 21 var replaceUrl = modifyReloadQry(htmlNode[linkKey], reloadTimes-1); 22 htmlNode[linkKey] = replaceUrl; 23 } 24}
另外一个资源加载重试的组件 assets-reload 本模块通过 script, link, img 等标签上的 onerror 回调来进行资源加载重试,并且规则可定制。
No vulnerabilities found.
Reason
no binaries found in the repo
Reason
license file detected
Details
Reason
Found 0/5 approved changesets -- score normalized to 0
Reason
0 commit(s) and 0 issue activity found in the last 90 days -- score normalized to 0
Reason
no SAST tool detected
Details
Reason
no effort to earn an OpenSSF best practices badge detected
Reason
security policy file not detected
Details
Reason
project is not fuzzed
Details
Reason
branch protection not enabled on development/release branches
Details
Reason
63 existing vulnerabilities detected
Details
Score
Last Scanned on 2025-07-07
The Open Source Security Foundation is a cross-industry collaboration to improve the security of open source software (OSS). The Scorecard provides security health metrics for open source projects.
Learn More