Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,9 @@ plugins: [
// optional code to be executed in the browser context if after all retries chunk is not loaded.
// if not set - nothing will happen and error will be returned to the chunk loader.
lastResortScript: "window.location.href='/500.html';",
// optional value to be executed in the browser context if after all retries chunk is not loaded.
// if set - error will be returned to the chunk loader with url which tried.
allLogUrl: false,
}),
];
```
Expand Down
18 changes: 17 additions & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,11 @@ export interface RetryChunkLoadPluginOptions {
* e.g. `function(retryAttempt) { return retryAttempt * 1000 }`
*/
retryDelay?: number | string;
/**
* optional value to be executed in the browser context if after all retries chunk is not Loaded.
* if set - error will be returned to the chunk loader with url which tried.
*/
allLogUrl?: boolean;
}

export class RetryChunkLoadPlugin {
Expand All @@ -42,6 +47,7 @@ export class RetryChunkLoadPlugin {
compiler.hooks.thisCompilation.tap(pluginName, compilation => {
const { mainTemplate, runtimeTemplate } = compilation;
const maxRetryValueFromOptions = Number(this.options.maxRetries);
const isAllLogUrl = this.options.allLogUrl;
const maxRetries =
Number.isInteger(maxRetryValueFromOptions) &&
maxRetryValueFromOptions > 0
Expand Down Expand Up @@ -71,22 +77,32 @@ export class RetryChunkLoadPlugin {
'',
`
if(typeof ${RuntimeGlobals.require} !== "undefined") {
var allChunkRequest = [];
var oldGetScript = ${RuntimeGlobals.getChunkScriptFilename};
var oldLoadScript = ${RuntimeGlobals.ensureChunk};
var queryMap = {};
var countMap = {};
var getRetryDelay = ${getRetryDelay}
${RuntimeGlobals.getChunkScriptFilename} = function(chunkId){

var result = oldGetScript(chunkId);
return result + (queryMap.hasOwnProperty(chunkId) ? '?' + queryMap[chunkId] : '');
var urlWithQueryParameter = result + (queryMap.hasOwnProperty(chunkId) ? '?' + queryMap[chunkId] : '');
if(${isAllLogUrl}){
allChunkRequest.push(urlWithQueryParameter)
}
return urlWithQueryParameter;
};
${RuntimeGlobals.ensureChunk} = function(chunkId){
var result = oldLoadScript(chunkId);
return result.catch(function(error){
var retries = countMap.hasOwnProperty(chunkId) ? countMap[chunkId] : ${maxRetries};
if (retries < 1) {
var realSrc = oldGetScript(chunkId);
if(${isAllLogUrl}){
error.message = 'Loading chunk ' + chunkId + ' failed after ${maxRetries} retries.All tried url:'+ allChunkRequest.join(';')+'\\n(' + realSrc + ')';
}else{
error.message = 'Loading chunk ' + chunkId + ' failed after ${maxRetries} retries.\\n(' + realSrc + ')';
}
error.request = realSrc;${
this.options.lastResortScript
? this.options.lastResortScript
Expand Down
3 changes: 3 additions & 0 deletions test/integration/test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,12 @@ const cases: (RetryChunkLoadPluginOptions | undefined)[] = [
{ chunks: [] },
{ chunks: ['main'] },
{ chunks: ['main'], retryDelay: 3000 },

{ chunks: ['main'] },
{
chunks: ['main'],
retryDelay: 'function(retryAttempt) { return retryAttempt * 1000 }',
allLogUrl: true,
},
{
chunks: ['main'],
Expand Down