You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
bug: angular ion-back-button pushes new entry to stack instead of popping #23448
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:
Generate a fresh Ionic Angular project with the CLI.
Create additional page if necessary and put <ion-back-button> in its toolbar.
Add a button on the main page which navigates to the 2nd page using the default ionic strategy.
After being navigated to the 2nd page, press the ion-back-button.
After being returned back to the main page, press the browser's back button (or the native back button if you are on Android).
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.
Reacted by Indraraj, Bertrand, Demulier--Chevret Tom, Robert R. Gómez T., Steve Schwarz, Nicolas Amaya, nlko and rebeccakoho
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
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.
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),
@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.
@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.
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!
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!
@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.
@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.
Activity
[-]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[/+]StefanNedelchev commentedon Aug 14, 2021
Any updated on this case?
StefanNedelchev commentedon Nov 21, 2021
Sorry for the bump but do you plan fixing this for v6 of Ionic?
TomDemulierChevret commentedon Jan 7, 2022
Any news on this ?
It affects our app.
StefanNedelchev commentedon Jan 7, 2022
doesn't seem to be fixed even in Ionic 6
TomDemulierChevret commentedon Jan 10, 2022
@hardmaster92 Thanks for the feedback.
StefanNedelchev commentedon Sep 7, 2022
Any news on this issue besides the fact it's been tracked internally for 15 months?
RRGT19 commentedon Oct 22, 2022
Still happening, any help or workaround would be appreciated.
4 remaining items
crysislinux commentedon Dec 18, 2024
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 replacingnavCtrl.back()withnavCtrl.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.tsLeomhl commentedon Apr 10, 2025
The same issue is happening to me, Ionic 8.
Specifically for Android users, "edge swipe right to left". The same idea as back button.
argoxang commentedon May 23, 2025
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),
crysislinux commentedon May 28, 2025
@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.
fix(angular): go back in browser history when popping to the previous…
ShaneK commentedon Nov 20, 2026
Hey, thanks for the report! This issue was fixed in v9.0.13!
StefanNedelchev commentedon Nov 20, 2026
@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.
ShaneK commentedon Nov 23, 2026
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!
StefanNedelchev commentedon Nov 24, 2026
@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.
ShaneK commentedon Nov 24, 2026
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.21and9.0.13and 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 lazyIonicModuleor standalone.Where I could get it to roughly double was moving from
IonicModule(the lazy build) to standalone components imported from the@ionic/angularbarrel. With the barrel, the bundler puts every component the app uses anywhere into the initial chunk, so in my test app things likeion-datetime,ion-modalandion-selectwere 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/lazyand the size stays the same as v8 (that's also whatnpx @ionic/migratedoes 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!
StefanNedelchev commentedon Nov 24, 2026
@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
/lazypath. 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.
StefanNedelchev commentedon Nov 27, 2026
@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
IonicModuleusage from the/lazypath. I tested theion-back-buttonbut 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:I click the
ion-back-button2 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.