mattlewis92 / webpack-retry-chunk-load-plugin

A webpack plugin to retry loading of chunks that failed to load
MIT License
234 stars 43 forks source link

Support for Various Retry options #49

Open sckimynwa opened 1 year ago

sckimynwa commented 1 year ago

Summary

Hi, I'm recently using this plugin to my project. Thanks to this plugin, it reduces my chunk load failed error a lot. as a user of this plugin, I think it would be nice if you provide some various retry options like exponential backoff

For Example,

plugins: [
  new RetryChunkLoadPlugin({
    // optional stringified function to get the cache busting query string appended to the script src
    // if not set will default to appending the string `?cache-bust=true`
    cacheBust: `function() {
      return Date.now();
    }`,
    // optional value to set the amount of time in milliseconds before trying to load the chunk again. Default is 0
    // if string, value must be code to generate a delay value. Receives retryCount as argument 
    // e.g. `function(retryAttempt) { return retryAttempt * 1000 }`
    retryDelay: 3000,
    // optional value to set the maximum number of retries to load the chunk. Default is 1
    maxRetries: 5,
    // retry delay options
    // my suggestion
    retryType : 'linear' | 'exponential',
    retryFunc : SOME FUNCTION TYPE,
    // optional list of chunks to which retry script should be injected
    // if not set will add retry script to all chunks that have webpack script loading
    chunks: ['chunkName'],
    // 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';",
  }),
];

Thank you in advance.

Reference

sckimynwa commented 1 year ago

Or, It would also be nice to support custom delay functions for user I think 🤔

bluthen commented 1 year ago
// optional value to set the amount of time in milliseconds before trying to load the chunk again. Default is 0
    // if string, value must be code to generate a delay value. Receives retryCount as argument 
    // e.g. `function(retryAttempt) { return retryAttempt * 1000 }`

It looks like you can give retryDelay a function. I just use a regular delay, but it is in the readme.

Edit: Looks like this was a version 1 feature not a version 2 feature. So could be a good addition to version 2/3

shridhar-purandare commented 1 year ago

A way to log failed chunk load URLs would be good to debug failed chunk-loading retries.

bluthen commented 1 year ago

I know you can load chunks different ways, but I often handle it where I load. I use specific chunks. I try to keep the chunks related to sections, especially rarely used ones that only get loaded if they are needed.

try {
   mymodules = await import 'somemodule';
} catch(err) { logger.error(err); throw err;}

And I use catchall. https://developer.mozilla.org/en-US/docs/Web/API/Window/error_event

Unhandled errors should be going to console anyway, but if you have like an external logger might be good idea to have that catchall anyway.