Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Angular - Load Production chunk files failed after deploy

I have clients that have the browser open all day so after I make a deployment they see the application broken and they need to reload the page to fix it.

The server failed to load a chunk file because of the NO-CACHE hash added by @angular/cli production build.

Error: error: Uncaught (in promise): Error: Loading chunk 11 failed.

I want to reload the page after a deployment.

These are my tools:

  • I have access to my NGINX configuration.
  • I have access to Jenkins (Blue Ocean)
  • I have implemented HttpClient Interceptors in the project.
like image 884
Eugenio Valeiras Avatar asked Nov 01 '25 23:11

Eugenio Valeiras


2 Answers

I managed the error of the route replacing its error handler and reloading if it found an error loading the chunk.

// Keep the original error handler
const oldHandler = this.router.errorHandler;
// Replace route error handler
this.router.errorHandler =  (err: any) => {
  // Check if there is an error loading the chunk
  if (err.originalStack && err.originalStack.indexOf('Error: Loading chunk') >= 0) {
    // Check if is the first time the error happend
    if (localStorage.getItem('lastChunkError') !== err.originalStack) {
      // Save the last error to avoid an infinite reload loop if the chunk really does not exists after reload
      localStorage.setItem('lastChunkError', err.originalStack);
      location.reload(true);
    } else {
      // The chunk really does not exists after reload
      console.error('We really don\'t find the chunk...');
    }
  }
  // Run original handler
  oldHandler(err);
};

I hope it helps you

like image 65
Sergio Contreras Sustaita Avatar answered Nov 03 '25 15:11

Sergio Contreras Sustaita


I solved this issue moving my application to AWS and saving the files in S3!

like image 28
Eugenio Valeiras Avatar answered Nov 03 '25 15:11

Eugenio Valeiras



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!