Skip to content

bug: angular ion-back-button pushes new entry to stack instead of popping #23448

Closed

Description

@StefanNedelchev

Bug Report

Ionic version:
5.6.9

Platform
Web

Imagine that you have the following stack in the history: [A, B]. You are currently on page B and press a <ion-back-button> element.

Current behavior:
The back button correctly navigates to the previous page A but does it by adding a new entry (let's call it C), leaving us with the following stack: [A, B, C]. In this case C and A are the same routes. As a result, using the native back button (or browser back button) afterwards returns the user back to page B while naturally you would not expect it. This seriously breaks the experience when the app is installed as a PWA resulting in large history stacks with repeating routes. Android users might have hard time closing the app if they rely on the native back button

Expected behavior:
The back button should navigate to page A while removing B from the stack, making it impossible for the user to return back to B using native navigation.

Steps to reproduce:

  1. Generate a fresh Ionic Angular project with the CLI.
  2. Create additional page if necessary and put <ion-back-button> in its toolbar.
  3. Add a button on the main page which navigates to the 2nd page using the default ionic strategy.
  4. After being navigated to the 2nd page, press the ion-back-button.
  5. After being returned back to the main page, press the browser's back button (or the native back button if you are on Android).
  6. The issue should be reproduced now

Other information:
This issue is not new, I've been experiencing it since I started using Ionic a couple of months ago. Also i don't know if the issue exists on native Android/iOS apps because my project is just a PWA.

Activity

  1. added
    package: angular
    type: bug
    on Jun 14, 2021
  2. changed the title [-]bug: Angular <ion-back-button> adds a new entry to the history (web)[/-] [+]bug: angular ion-back-button pushes new entry to stack instead of popping[/+] on Jun 14, 2021
  3. StefanNedelchev commented on Aug 14, 2021

    @StefanNedelchev
    Author

    Any updated on this case?

  4. StefanNedelchev commented on Nov 21, 2021

    @StefanNedelchev
    Author

    Sorry for the bump but do you plan fixing this for v6 of Ionic?

  5. TomDemulierChevret commented on Jan 7, 2022

    @TomDemulierChevret
    Contributor

    Any news on this ?
    It affects our app.

  6. StefanNedelchev commented on Jan 7, 2022

    @StefanNedelchev
    Author

    Any news on this ? It affects our app.

    doesn't seem to be fixed even in Ionic 6

  7. TomDemulierChevret commented on Jan 10, 2022

    @TomDemulierChevret
    Contributor

    @hardmaster92 Thanks for the feedback.

  8. added
    type: bug
    and removed
    type: bug
    on Feb 2, 2022
  9. StefanNedelchev commented on Sep 7, 2022

    @StefanNedelchev
    Author

    Any news on this issue besides the fact it's been tracked internally for 15 months?

  10. RRGT19 commented on Oct 22, 2022

    @RRGT19

    Still happening, any help or workaround would be appreciated.

  11. 4 remaining items

  12. crysislinux commented on Dec 18, 2024

    @crysislinux

    Edited: the button below has an issue. If you switch to B tab from A and then go back to A, the back button in a sub route of A back to B instead of A..
    Fixed by replacing navCtrl.back() with navCtrl.pop().

    Using navCtrl.pop() has the same issue as ion-back-button, I guess the issue is in NavController instead of the button, using navCtrl.back() fix the issue, but it may navigate in the wrong stack

    It seems not hard to create a custom back button that works the same way as ion-back-button, of course, with this issue fixed.

    Here is a version I am using

    back-button.component.html

    @if (visible()) {
      <ion-button fill="clear" [color]="color()" (click)="goBack()">
        <ion-icon [name]="icon()"></ion-icon>{{ text() }}
      </ion-button>
    }

    back-button.component.ts

    import { Component, input, inject, computed } from '@angular/core';
    import { Router, ActivatedRoute, UrlTree } from '@angular/router';
    import { IonIcon, IonButton, IonRouterOutlet, NavController } from '@ionic/angular/standalone';
    import { Color } from '@ionic/core';
    import { addIcons } from 'ionicons';
    import { chevronBackOutline } from 'ionicons/icons';
    
    @Component({
      selector: 'app-back-button',
      templateUrl: './back-button.component.html',
      standalone: true,
      imports: [
        IonIcon,
        IonButton
      ]
    })
    export class BackButtonComponent {
      readonly color = input<Color | undefined>();
      readonly text = input<string | undefined>('Back');
      readonly icon = input<string | undefined>('chevron-back-outline');
      readonly defaultHref = input<string | undefined>();
    
      private router = inject(Router);
      private navCtrl = inject(NavController);
      private outlet = inject(IonRouterOutlet);
      private activatedRoute = inject(ActivatedRoute);
    
      readonly visible = computed(() => {
        return this.canGoBack() || this.defaultHref();
      });
    
      private canGoBack = computed(() => this.outlet.canGoBack(1));
    
      constructor() {
        addIcons({ chevronBackOutline });
      }
    
      goBack() {
        if (this.canGoBack()) {
          this.navCtrl.pop();
        } else {
          const href = this.normalizeDefaultHref(this.defaultHref());
          if (href) {
            this.navCtrl.setDirection('back');
            this.router.navigateByUrl(href, { replaceUrl: true });
          }
        }
      }
    
      private normalizeDefaultHref(href: string | undefined): string | UrlTree | undefined {
        if (!href) {
          return undefined;
        }
    
        if (href.startsWith('/')) {
          return href;
        }
        // it's a relative route
        const urlTree = this.router.createUrlTree([href], { relativeTo: this.activatedRoute });
        return this.router.serializeUrl(urlTree);
      }
    }
  13. Leomhl commented on Apr 10, 2025

    @Leomhl

    The same issue is happening to me, Ionic 8.
    Specifically for Android users, "edge swipe right to left". The same idea as back button.

  14. argoxang commented on May 23, 2025

    @argoxang

    Solution for vue:
    My company uses Ionic mostly for mobile apps and web is for fast devolopment. A workaround we implemented is to use createMemoryHistory for andriod and for the rest platforms the prefered createWebHistory while creating the router.
    In index.ts add
    import { createRouter, createMemoryHistory, createWebHistory } from '@ionic/vue-router'
    import { isPlatform } from '@ionic/vue';
    and this in createRouter:
    history: isPlatform('android') ? createMemoryHistory(process.env.BASE_URL) : createWebHistory(process.env.BASE_URL),

  15. crysislinux commented on May 28, 2025

    @crysislinux

    @argoxang that's an excellent/clever solution since we don't need the browser history at all in a mobile app. However we are using Angular so it doesn't work for us. Maybe it's time to implement a similar solution in Angular Router.

  16. added a commit that references this issue on Nov 19, 2026
    fd0ac5e
  17. ShaneK commented on Nov 20, 2026

    @ShaneK
    Member

    Hey, thanks for the report! This issue was fixed in v9.0.13!

  18. StefanNedelchev commented on Nov 20, 2026

    @StefanNedelchev
    Author

    @ShaneK but are you going to backport it to v8? This is a critical fix that we've been waiting for at least 5 years. We have a project that is still not ready to migarte to v9 because of the bundle size issues.

  19. ShaneK commented on Nov 23, 2026

    @ShaneK
    Member

    Hey @StefanNedelchev, I'm sorry but we only backport security concern issues to v8.8.x since it's in maintenance mode at this time. Do you have a report of outstanding bundle size issues? There were concerns in early v9 releases, but I do believe they've all been addressed at this time (#31303, #31594).

    I'm not aware of any remaining bundle size issues that would impact v9 Angular though. If you know of an issue I'm missing or if you could create an issue for that, we'd love to take a look!

  20. StefanNedelchev commented on Nov 24, 2026

    @StefanNedelchev
    Author

    @ShaneK Last time I checked i still experienced double initial load size with Ionic 9 + Angular... I'll try again and see if something changes.

  21. ShaneK commented on Nov 24, 2026

    @ShaneK
    Member

    Hey @StefanNedelchev, I spent some time trying to reproduce this and I couldn't find a size regression between v8 and v9, but I did find something that'd explain the doubling. I built the same tabs app on 8.8.21 and 9.0.13 and checked what the browser actually loads on the first page. With the same import style in both versions, v9 comes out about 2-3% larger than v8, whether that's lazy IonicModule or standalone.

    Where I could get it to roughly double was moving from IonicModule (the lazy build) to standalone components imported from the @ionic/angular barrel. With the barrel, the bundler puts every component the app uses anywhere into the initial chunk, so in my test app things like ion-datetime, ion-modal and ion-select were in the first load even though only lazy routes use them. That isn't new in v9 though, v8 does the exact same thing with @ionic/angular/standalone. It's more likely to show up now because standalone became the default import path in v9.

    If that sounds like your setup, there are two ways around it. If you want to keep using IonicModule, import it from @ionic/angular/lazy and the size stays the same as v8 (that's also what npx @ionic/migrate does for you). If you want standalone, the per-component entry points we added in #31303 fix the code splitting, e.g. import { IonToolbar } from '@ionic/angular/ion-toolbar', and in my test that came out smaller than v8's lazy build.

    If you're still seeing double after that, I'd love a reproduction so we can figure out what's different about your app!

  22. StefanNedelchev commented on Nov 24, 2026

    @StefanNedelchev
    Author

    @ShaneK yeah, the reason is exactly what you explained - ionic 9 having standalone as default. I wasn't aware that in v9 I can still use the module approach so thank you for the heads up, I'll definitely try using the /lazy path. I'll let you know if I succeed in doing this.

    But I'm looking forward to having this resolved because not only can't I use standalone components but also the modern vite/esbuild approach.

  23. StefanNedelchev commented on Nov 27, 2026

    @StefanNedelchev
    Author

    @ShaneK sorry for the late update but I finally took the time to trry this. I successfully managed to upgrade to Ionic 9 by keeping the legacy IonicModule usage from the /lazy path. I tested the ion-back-button but to me the behavior doesn't seem to have changed (v9.0.14). For example, I navigate 3 steps deep into route tree, something like this:

    • A -> A/B -> A/B/C

    I click the ion-back-button 2 times to get back to "A" (which happens successfully). Then I click the browser back button (or use Andoird's back button), and I see that the first time I once again end up on "A/B". After another click I end up in "A/B/C". So just like it used to be before - these 2 routes had never been popped out of the stack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    package: angulartype: bug

    Type

    Fields

    Stage

    None yet

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions