diff --git a/apps/browser/src/platform/popup/view-cache/popup-router-cache.service.ts b/apps/browser/src/platform/popup/view-cache/popup-router-cache.service.ts index 3215893c634..aa0c0854eff 100644 --- a/apps/browser/src/platform/popup/view-cache/popup-router-cache.service.ts +++ b/apps/browser/src/platform/popup/view-cache/popup-router-cache.service.ts @@ -8,8 +8,9 @@ import { NavigationEnd, Router, UrlSerializer, + UrlTree, } from "@angular/router"; -import { filter, first, firstValueFrom, map, Observable, switchMap, tap } from "rxjs"; +import { filter, first, firstValueFrom, map, Observable, of, switchMap, tap } from "rxjs"; import { GlobalStateProvider } from "@bitwarden/common/platform/state"; @@ -31,6 +32,11 @@ export class PopupRouterCacheService { private hasNavigated = false; + private _hasRestoredCache = false; + get hasRestoredCache() { + return this._hasRestoredCache; + } + constructor() { // init history with existing state this.history$() @@ -107,21 +113,34 @@ export class PopupRouterCacheService { // if no history is present, fallback to vault page await this.router.navigate([""]); } + + /** + * Mark the cache as restored to prevent the router `popupRouterCacheGuard` from + * redirecting to the last visited route again this session. + */ + markCacheRestored() { + this._hasRestoredCache = true; + } } /** * Redirect to the last visited route. Should be applied to root route. **/ -export const popupRouterCacheGuard = (() => { +export const popupRouterCacheGuard = ((): Observable => { const popupHistoryService = inject(PopupRouterCacheService); const urlSerializer = inject(UrlSerializer); + if (popupHistoryService.hasRestoredCache) { + return of(true); + } + return popupHistoryService.last$().pipe( map((url: string) => { if (!url) { return true; } + popupHistoryService.markCacheRestored(); return urlSerializer.parse(url); }), );