Skip to content

Commit

Permalink
fix issue where invalidateRootCache didn't happen when the page first…
Browse files Browse the repository at this point in the history
… loaded
  • Loading branch information
artlowel committed Sep 22, 2023
1 parent 5ad621b commit 8fbe8c1
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 2 deletions.
6 changes: 4 additions & 2 deletions src/app/core/server-check/server-check.guard.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -63,16 +63,18 @@ describe('ServerCheckGuard', () => {
});

describe(`listenForRouteChanges`, () => {
it(`should invalidate the root cache on every NavigationStart event`, () => {
it(`should invalidate the root cache when the method is first called, and then on every NavigationStart event`, () => {
testScheduler.run(() => {
guard.listenForRouteChanges();
expect(rootDataServiceStub.invalidateRootCache).toHaveBeenCalledTimes(1);

eventSubject.next(new NavigationStart(1,''));
eventSubject.next(new NavigationEnd(1,'', ''));
eventSubject.next(new NavigationStart(2,''));
eventSubject.next(new NavigationEnd(2,'', ''));
eventSubject.next(new NavigationStart(3,''));
});
expect(rootDataServiceStub.invalidateRootCache).toHaveBeenCalledTimes(3);
expect(rootDataServiceStub.invalidateRootCache).toHaveBeenCalledTimes(4);
});
});
});
4 changes: 4 additions & 0 deletions src/app/core/server-check/server-check.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ export class ServerCheckGuard implements CanActivateChild {
* operation, the cached version is used.
*/
listenForRouteChanges(): void {
// we'll always be too late for the first NavigationStart event with the router subscribe below,
// so this statement is for the very first route operation
this.rootDataService.invalidateRootCache();

this.router.events.pipe(
filter(event => event instanceof NavigationStart),
).subscribe(() => {
Expand Down

0 comments on commit 8fbe8c1

Please sign in to comment.