Skip to content

fix(angular): go back in browser history when popping to the previous view - #31752

Open
ShaneK wants to merge 1 commit into
mainfrom
FW-7386
Open

ShaneK wants to merge 1 commit into
mainfrom
FW-7386

Conversation

@ShaneK

@ShaneK ShaneK commented Nov 13, 2026

Copy link
Copy Markdown
Member

Issue number: resolves #23448


What is the current behavior?

Currently, ion-back-button, NavController.pop(), the hardware back button and the swipe to go back gesture all navigate to the previous view's URL, which pushes a new browser history entry. Pressing the browser's back button after that returns you to the page you just left, and the history keeps growing as you go back and forth.

What is the new behavior?

When the previous view is the entry directly behind the current one in browser history, popping now calls location.back() instead of navigating by URL, so the entry is popped instead of a new one being pushed.

To know that, NavController keeps track of the navigation ids of the history entries the router writes, and each view records the entry it was shown in. If anything makes that unclear (a URL changed outside the router with Location.go(), a hash change, a canceled or failed navigation that may have left the browser somewhere else), we drop the tracking and fall back to the old behavior rather than guess.

Routes with a guard or resolver that would run on the way back also keep navigating by URL. A history navigation that gets canceled leaves the browser on the wrong entry, so I didn't want to risk that.

The stack controller also no longer removes the last view when the router flags a browser back/forward navigation as replaceUrl, since those don't replace anything and it was dropping views from the tab stacks.

Does this introduce a breaking change?

  • Yes
  • No

Other information

This only covers popping one view. pop() with deep > 1, going back after switching tabs and the defaultHref fallback still push a new entry like before. The defaultHref case is what #17752 was about, so that one isn't fixed here.

If a pop() and another navigation start in the same task (e.g., a button with both a routerLink and (click)="navCtrl.pop()", which the lazy tabs test app has), the pop still wins like it did before. The router only handles location.back() after the current task ends, so in that case we navigate by URL instead.

Going back with ion-back-button now leaves forward entries in browser history, which makes an existing bug easier to hit. Angular gives an entry a new navigation id each time the browser lands on it, so after going back twice and then pressing the browser's forward button twice, NavController guesses the second forward as back and resets the stack. The same thing happens on main with only the browser's back and forward buttons, so I left it for a separate fix. (This is already an issue tracked as FW-7353, the community issue is #28307)

@ShaneK
ShaneK requested a review from a team as a code owner November 13, 2026 17:19
@ShaneK
ShaneK requested a review from brandyscarney November 13, 2026 17:19
@vercel

vercel Bot commented Nov 13, 2026

Copy link
Copy Markdown

The latest updates on your projects. Learn more about Vercel for GitHub.

Project Deployment Actions Updated
ionic-framework Ready Ready Preview Nov 13, 2026 5:19pm UTC

Request Review

@github-actions github-actions Bot added the package: angular @ionic/angular package label Nov 13, 2026

This branch was successfully deployed

1 active deployment
Preview — f683355b Deployed Nov 13, 2026 by vercel[bot]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

package: angular @ionic/angular package

Projects

None yet

Development

Successfully merging this pull request may close these issues.

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

1 participant