diff --git a/src/app/app-routing.module.ts b/src/app/app-routing.module.ts index 3a47815f42..4a9810e4da 100644 --- a/src/app/app-routing.module.ts +++ b/src/app/app-routing.module.ts @@ -36,7 +36,9 @@ import { ServerCheckGuard } from './core/server-check/server-check.guard'; RouterModule.forRoot([ { path: INTERNAL_SERVER_ERROR, component: ThemedPageInternalServerErrorComponent }, { - path: '', canActivate: [ServerCheckGuard, AuthBlockingGuard], + path: '', + canActivate: [AuthBlockingGuard], + canActivateChild: [ServerCheckGuard], children: [ { path: '', redirectTo: '/home', pathMatch: 'full' }, { diff --git a/src/app/core/server-check/server-check.guard.spec.ts b/src/app/core/server-check/server-check.guard.spec.ts index a9c03d1cd7..749912be85 100644 --- a/src/app/core/server-check/server-check.guard.spec.ts +++ b/src/app/core/server-check/server-check.guard.spec.ts @@ -13,7 +13,8 @@ describe('ServerCheckGuard', () => { let rootDataServiceStub: SpyObj; rootDataServiceStub = jasmine.createSpyObj('RootDataService', { - findRoot: jasmine.createSpy('findRoot') + findRoot: jasmine.createSpy('findRoot'), + invalidateRootCache: jasmine.createSpy('invalidateRootCache') }); router = jasmine.createSpyObj('Router', { navigateByUrl: jasmine.createSpy('navigateByUrl') @@ -37,10 +38,11 @@ describe('ServerCheckGuard', () => { }); it('should not redirect to error page', () => { - guard.canActivate({} as any, {} as any).pipe( + guard.canActivateChild({} as any, {} as any).pipe( take(1) ).subscribe((canActivate: boolean) => { expect(canActivate).toEqual(true); + expect(rootDataServiceStub.invalidateRootCache).not.toHaveBeenCalled(); expect(router.navigateByUrl).not.toHaveBeenCalled(); }); }); @@ -52,10 +54,11 @@ describe('ServerCheckGuard', () => { }); it('should redirect to error page', () => { - guard.canActivate({} as any, {} as any).pipe( + guard.canActivateChild({} as any, {} as any).pipe( take(1) ).subscribe((canActivate: boolean) => { expect(canActivate).toEqual(false); + expect(rootDataServiceStub.invalidateRootCache).toHaveBeenCalled(); expect(router.navigateByUrl).toHaveBeenCalledWith(getPageInternalServerErrorRoute()); }); }); diff --git a/src/app/core/server-check/server-check.guard.ts b/src/app/core/server-check/server-check.guard.ts index 017b75d373..af216bef0e 100644 --- a/src/app/core/server-check/server-check.guard.ts +++ b/src/app/core/server-check/server-check.guard.ts @@ -1,5 +1,5 @@ import { Injectable } from '@angular/core'; -import { ActivatedRouteSnapshot, CanActivate, Router, RouterStateSnapshot } from '@angular/router'; +import { ActivatedRouteSnapshot, CanActivateChild, Router, RouterStateSnapshot } from '@angular/router'; import { Observable } from 'rxjs'; import { map, tap } from 'rxjs/operators'; @@ -16,18 +16,18 @@ import { getFirstCompletedRemoteData } from '../shared/operators'; * A guard that checks if root api endpoint is reachable. * If not redirect to 500 error page */ -export class ServerCheckGuard implements CanActivate { +export class ServerCheckGuard implements CanActivateChild { constructor(private router: Router, private rootDataService: RootDataService) { } /** * True when root api endpoint is reachable. */ - canActivate( + canActivateChild( route: ActivatedRouteSnapshot, state: RouterStateSnapshot): Observable { - return this.rootDataService.findRoot().pipe( + return this.rootDataService.findRoot(false).pipe( getFirstCompletedRemoteData(), map((res: RemoteData) => res.hasSucceeded), tap((hasSucceeded: boolean) => {