IonRouterOutlet view caching prevents ngOnDestroy during forward navigation
0 reputation · 09 Oct 2023, 20:41 UTC
0 reputation · 09 Oct 2023, 20:41 UTC
Ionic's IonRouterOutlet caches component instances in the DOM to preserve scroll position and state across forward navigation. This caching behavior means Angular's ngOnDestroy hook does not fire when navigating away from a page via routerLink or NavController.push. Instead, the component remains attached to the cached view until the stack is popped or cleared.
Developers subscribing to Observables, WebSocket connections, or interval timers in ngOnInit face a disposal gap: the standard Angular cleanup pattern relies on ngOnDestroy, which Ionic's view caching bypasses. The documented Ionic page lifecycle hooks—ionViewWillLeave and ionViewDidLeave—fire reliably during navigation transitions, but their relationship to Angular's change detection and zone execution order is not formally specified in the framework contract.
Heap snapshot analysis confirms retained component instances after repeated forward navigation, yet the exact conditions triggering cache eviction (memory pressure, stack depth limits, or explicit navigateRoot calls) remain implementation-dependent.
ionViewWillLeave or Angular's ngOnDestroy—provides the authoritative teardown signal for async subscriptions in a cached view?ionViewWillLeave executes before the component is detached from the DOM cache, or can race conditions with zone microtasks leave resources dangling?A thoughtful contribution can make all the difference. Be the first to share one.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.