Adds router error handler again

parent 08ff3d07
Pipeline #23673 passed with stages
in 13 minutes and 9 seconds
......@@ -3,7 +3,7 @@ import { AfterViewInit, Component, Inject, OnInit, PLATFORM_ID } from '@angular/
import { ActivatedRoute, NavigationEnd, RouteConfigLoadEnd, RouteConfigLoadStart, Router } from '@angular/router';
import { SwUpdate } from '@angular/service-worker';
import { TranslateService } from '@ngx-translate/core';
import { DeprecatedDb, DeprecatedKeys } from '../../../lib/enums/enums';
import { DeprecatedDb, DeprecatedKeys, StorageKey } from '../../../lib/enums/enums';
import { INamedType } from '../../../lib/interfaces/interfaces';
// tslint:disable-next-line:max-line-length
import { QuizManagerDetailsOverviewComponent } from '../../quiz/quiz-manager/quiz-manager-details/quiz-manager-details-overview/quiz-manager-details-overview.component';
......@@ -75,6 +75,32 @@ export class RootComponent implements OnInit, AfterViewInit {
Object.values(DeprecatedDb).forEach(deprecatedDb => {
indexedDB.deleteDatabase(deprecatedDb).addEventListener('success', result => {});
});
/* Reload the page if the fetch of production chunks failed
* https://stackoverflow.com/a/49805926
*/
// 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
console.error('error while loading route', err);
if (err.originalStack && err.originalStack.indexOf('Error: Loading chunk') >= 0) {
// Check if is the first time the error happend
console.error('loading chunk failed');
if (sessionStorage.getItem(StorageKey.ChunkError) !== err.originalStack) {
// Save the last error to avoid an infinite reload loop if the chunk really does not exists after reload
sessionStorage.setItem(StorageKey.ChunkError, err.originalStack);
console.error('no previous reload found, so forcing reload now');
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);
};
}
this.userService.loadConfig();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment