akveo / ngx-admin-bundle-support

Support repository for ngx-admin backend bundles with issues tracking, instructions and code samples
58 stars 32 forks source link

Error: Loading chunk #23

Closed francisedulab closed 4 years ago

francisedulab commented 4 years ago

Hi , After npm run build:prod when we deploy the code to the production server on lms.admissiondesk.org we get this error on the first load :

ERROR Error: "Uncaught (in promise): Error: Loading chunk 6 failed. (missing: http://lms.admissiondesk.org/6-es2015.8c364b8094b352391c69.js)

after refresh

ERROR Error: Uncaught (in promise): Error: Loading chunk 9 failed. (missing: http://lms.admissiondesk.org/9-es2015.801f1f734c8f87d9bd0f.js) Error: Loading chunk 9 failed.

on some machines this error does not occur and it loads directly now after reading online we found this solution https://stackoverflow.com/questions/44034039/angular-2-error-loading-chunk-failed can you let us know were can we handle the same in our project this error does not occur with previous versions of ngx-admin only the purchased one has this error .

worldfellow commented 4 years ago

To solve this problem you need to create a global error handler create a file named global-error-handler.ts in your /app directory and paste the following contents

import { ErrorHandler } from '@angular/core';

@Injectable() export class GlobalErrorHandler implements ErrorHandler {

handleError(error: any): void { const chunkFailedMessage = /Loading chunk [\d]+ failed/;

if (chunkFailedMessage.test(error.message)) {
  window.location.reload();
}

} }

after this register your handler in app.module.ts

providers: [ { provide: APP_INITIALIZER, useFactory: init_app, deps: [Injector], multi: true,

},
{
  provide: ErrorHandler,
  useClass: GlobalErrorHandler,
  deps: [APP_INITIALIZER]
}

],

thats it problem solved 👍

valentinkononov commented 4 years ago

thanks for a nice solution!