Skip to content

bug: takeUntilDestroyed does not unsubscribe in Ionic due to ion-router-outlet page persistence #30391

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
3 tasks done
maniya108 opened this issue May 5, 2025 · 1 comment
Closed
3 tasks done

Comments

@maniya108
Copy link

maniya108 commented May 5, 2025

Prerequisites

Ionic Framework Version

v8.x

Current Behavior

In an Ionic Angular app, when navigating away from a page using , the component is not destroyed. As a result, Angular’s takeUntilDestroyed() and ngOnDestroy() are never called, leaving active subscriptions running in the background. This causes memory leaks and unintended behavior if the observable continues emitting values.

Expected Behavior

Angular’s ngOnDestroy() (and by extension takeUntilDestroyed()) should be called when a page is navigated away from, or Ionic should provide a reliable lifecycle event or configuration to clean up subscriptions automatically.

Steps to Reproduce

Create a new Ionic Angular app using ionic start myApp blank --type=angular.

Generate two pages: PageA and PageB.

In PageA, subscribe to a timer observable like below:

import { Component, inject } from '@angular/core';
import { interval } from 'rxjs';
import { takeUntilDestroyed } from '@angular/core/rxjs-interop';

@Component({
  selector: 'app-page-a',
  templateUrl: './page-a.component.html',
})
export class PageA {
  constructor() {
    interval(1000)
      .pipe(takeUntilDestroyed())
      .subscribe(() => console.log('Still alive'));
  }

  ngOnDestroy() {
    console.log('PageA destroyed');
  }
}

Add buttons to navigate from PageA to PageB and back.

Observe that the console.log inside ngOnDestroy and the observable continue running even after navigating away from PageA.

Code Reproduction URL

NA

Ionic Info

Ionic Framework: v8.5.0
Angular: v20.x (latest)
@ionic/angular: 8.x
Browser: Chrome 132
Device: Desktop / Android

Additional Information

No response

@ionitron-bot ionitron-bot bot added the triage label May 5, 2025
@ShaneK ShaneK added the ionitron: needs reproduction a code reproduction is needed from the issue author label May 5, 2025
@ionitron-bot ionitron-bot bot removed the triage label May 5, 2025
@ionic-team ionic-team deleted a comment from ionitron-bot bot May 5, 2025
@ShaneK
Copy link
Member

ShaneK commented May 5, 2025

I don't think this is a bug, I think this is intended behavior with the Ionic router, due to how it works with animations. You cannot use takeUntilDestroyed here, instead you should use takeUntil with an observable that you trigger on an Ionic Lifecycle event like ionViewDidLeave.

@ShaneK ShaneK closed this as not planned Won't fix, can't repro, duplicate, stale May 5, 2025
@ShaneK ShaneK removed the ionitron: needs reproduction a code reproduction is needed from the issue author label May 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants