ActivityPub-federated video streaming platform using P2P directly in your web browser
Only reuse component when navigating back
Chocobozzz0df194d6405f
diff --git a/client/src/app/app.component.ts b/client/src/app/app.component.tsindex 620537b8ee..2fa7d5874a 100644--- a/client/src/app/app.component.ts+++ b/client/src/app/app.component.ts@@ -1,13 +1,14 @@import { getLocaleDirection, NgClass, PlatformLocation } from '@angular/common'-import { AfterViewInit, Component, inject, LOCALE_ID, OnDestroy, OnInit, viewChild, DOCUMENT } from '@angular/core'+import { AfterViewInit, Component, DOCUMENT, inject, LOCALE_ID, OnDestroy, OnInit, viewChild } from '@angular/core'import { DomSanitizer, SafeHtml } from '@angular/platform-browser'-import { Event, GuardsCheckStart, RouteConfigLoadEnd, RouteConfigLoadStart, Router, RouterOutlet } from '@angular/router'+import { Event, GuardsCheckStart, NavigationStart, RouteConfigLoadEnd, RouteConfigLoadStart, Router, RouterOutlet } from '@angular/router'import {AuthService,Hotkey,HotkeysService,MarkdownService,PeerTubeRouterService,+ RouterStatusService,ScreenService,ScrollService,ServerService,@@ -82,6 +83,7 @@ export class AppComponent implements OnInit, AfterViewInit, OnDestroy {private scrollService = inject(ScrollService)private userLocalStorage = inject(UserLocalStorageService)private peertubeModal = inject(PeertubeModalService)+ private routerStatus = inject(RouterStatusService)menu = inject(MenuService)@@ -229,6 +231,15 @@ export class AppComponent implements OnInit, AfterViewInit, OnDestroy {eventsObs.pipe(filter((e: Event): e is RouteConfigLoadEnd => e instanceof RouteConfigLoadEnd)).subscribe(() => this.loadingBar.useRef().complete())++ // We need this to prevent circular dependency between the router and the custom reuse strategy+ eventsObs.pipe(+ filter((e: Event): e is NavigationStart => e instanceof NavigationStart)+ ).subscribe(() => {+ const current = this.router.currentNavigation()++ this.routerStatus.isNavigatingBack = current?.trigger === 'popstate' || current?.extras.state?.trigger === 'popstate'+ })}private async injectBroadcastMessage () {diff --git a/client/src/app/core/routing/custom-reuse-strategy.ts b/client/src/app/core/routing/custom-reuse-strategy.tsindex 326a00a72b..b7d0713008 100644--- a/client/src/app/core/routing/custom-reuse-strategy.ts+++ b/client/src/app/core/routing/custom-reuse-strategy.ts@@ -1,10 +1,15 @@-import { ComponentRef, Injectable } from '@angular/core'+import { ComponentRef, inject, Injectable } from '@angular/core'import { ActivatedRouteSnapshot, DetachedRouteHandle, RouteReuseStrategy } from '@angular/router'-import { logger } from '@root-helpers/logger'+import debug from 'debug'import { DisableForReuseHook } from './disable-for-reuse-hook'+import { RouterStatusService } from './router-status.service'++const debugLogger = debug('peertube:router:CustomReuseStrategy')@Injectable()export class CustomReuseStrategy implements RouteReuseStrategy {+ private routerStatus = inject(RouterStatusService)+storedRouteHandles = new Map<string, DetachedRouteHandle>()recentlyUsed: string@@ -22,7 +27,7 @@ export class CustomReuseStrategy implements RouteReuseStrategy {const key = this.generateKey(route)this.recentlyUsed = key- logger.info(`Storing component ${key} to reuse later.`)+ debugLogger(`Storing component ${key} to reuse later.`)const componentRef = (handle as any).componentRef as ComponentRef<DisableForReuseHook>componentRef.instance.disableForReuse()@@ -36,7 +41,13 @@ export class CustomReuseStrategy implements RouteReuseStrategy {// Return true if we have a stored route object for the next routeshouldAttach (route: ActivatedRouteSnapshot): boolean {const key = this.generateKey(route)- return this.isReuseEnabled(route) && this.storedRouteHandles.has(key)+ const isNavigatingBack = this.routerStatus.isNavigatingBack++ const should = !!key && isNavigatingBack === true && this.isReuseEnabled(route) && this.storedRouteHandles.has(key)++ if (key) debugLogger(`Should attach ${key}? Answer: ${should}`, { isNavigatingBack })++ return should}// If we returned true in shouldAttach(), now return the actual route data for restoration@@ -46,7 +57,7 @@ export class CustomReuseStrategy implements RouteReuseStrategy {const key = this.generateKey(route)this.recentlyUsed = key- logger.info(`Reusing component ${key}.`)+ debugLogger(`Reusing component ${key}.`)const handle = this.storedRouteHandles.get(key)if (!handle) return handle@@ -65,7 +76,7 @@ export class CustomReuseStrategy implements RouteReuseStrategy {this.storedRouteHandles.forEach((r, key) => {if (key === this.recentlyUsed) return- logger.info(`Removing stored component ${key}`)+ debugLogger(`Removing stored component ${key}`);(r as any).componentRef.destroy()this.storedRouteHandles.delete(key)})diff --git a/client/src/app/core/routing/index.ts b/client/src/app/core/routing/index.tsindex bb5bda5775..5e133f5f07 100644--- a/client/src/app/core/routing/index.ts+++ b/client/src/app/core/routing/index.ts@@ -11,3 +11,4 @@ export * from './scroll.service'export * from './server-config-resolver.service'export * from './unlogged-guard.service'export * from './user-right-guard.service'+export * from './router-status.service'diff --git a/client/src/app/core/routing/redirect.service.ts b/client/src/app/core/routing/redirect.service.tsindex c2baac1d4a..f6cb27f0b3 100644--- a/client/src/app/core/routing/redirect.service.ts+++ b/client/src/app/core/routing/redirect.service.ts@@ -122,8 +122,10 @@ export class RedirectService {logger.info(`Redirecting to default route ${this.defaultRoute}...`, { skipLocationChange })- this.router.navigateByUrl(this.defaultRoute, { skipLocationChange })- .then(() => this.redirectingToHomepage = false)+ this.router.navigateByUrl(this.defaultRoute, {+ skipLocationChange,+ state: { trigger: this.router.currentNavigation()?.trigger }+ }).then(() => this.redirectingToHomepage = false).catch(err => {this.redirectingToHomepage = falsediff --git a/client/src/app/core/routing/router-status.service.ts b/client/src/app/core/routing/router-status.service.tsnew file mode 100644index 0000000000..6e79361ee7--- /dev/null+++ b/client/src/app/core/routing/router-status.service.ts@@ -0,0 +1,6 @@+import { Injectable } from '@angular/core'++@Injectable({ providedIn: 'root' })+export class RouterStatusService {+ isNavigatingBack = false+}diff --git a/packages/core-utils/src/common/url.ts b/packages/core-utils/src/common/url.tsindex 405c3877f8..a7e77cc6da 100644--- a/packages/core-utils/src/common/url.ts+++ b/packages/core-utils/src/common/url.ts@@ -1,8 +1,8 @@import { Video, VideoPlaylist } from '@peertube/peertube-models'import { secondsToTime } from './date.js'-function addQueryParams (url: string, params: { [ id: string ]: string }) {- const objUrl = new URL(url)+function addQueryParams (url: string, params: { [ id: string ]: string }, baseUrl?: string) {+ const objUrl = new URL(url, baseUrl)for (const key of Object.keys(params)) {objUrl.searchParams.append(key, params[key])@@ -11,8 +11,8 @@ function addQueryParams (url: string, params: { [ id: string ]: string }) {return objUrl.toString()}-function removeQueryParams (url: string) {- const objUrl = new URL(url)+function removeQueryParams (url: string, baseUrl?: string) {+ const objUrl = new URL(url, baseUrl)objUrl.searchParams.forEach((_v, k) => objUrl.searchParams.delete(k))