diff --git a/packages/vue-router/src/router.ts b/packages/vue-router/src/router.ts index 95443549c9e..55ffc5f20e6 100644 --- a/packages/vue-router/src/router.ts +++ b/packages/vue-router/src/router.ts @@ -664,7 +664,17 @@ export const createIonRouter = ( const lastRoute = locationHistory.getCurrentRouteInfoForTab( routeInfo.tab ); - routeInfo.pushedByRoute = lastRoute?.pushedByRoute; + /** + * The last route in the tab stack is not always the page + * the tab returns to. After the tab button resets the tab + * with router.go(), its child stays last in the stack, and + * copying the child's pushedByRoute onto the tab root would + * show a back button on the root. + */ + routeInfo.pushedByRoute = + lastRoute?.pathname === routeInfo.pathname + ? lastRoute?.pushedByRoute + : undefined; } else if ( routeInfo.routerAction === "push" && routeInfo.routerDirection === "none" && diff --git a/packages/vue/test/base/tests/e2e/playwright/tabs.spec.ts b/packages/vue/test/base/tests/e2e/playwright/tabs.spec.ts index eb5dc1f0a78..6ccfe37f29c 100644 --- a/packages/vue/test/base/tests/e2e/playwright/tabs.spec.ts +++ b/packages/vue/test/base/tests/e2e/playwright/tabs.spec.ts @@ -36,4 +36,26 @@ test.describe('Tabs', () => { await tabClick(page, 'tab1'); await ionPageVisible(page, 'tab1'); }); + + test('tab root should not be able to go back after resetting the tab and switching tabs', async ({ page }) => { + await page.goto('/tabs/tab1'); + await ionPageVisible(page, 'tab1'); + + await page.locator('div.ion-page[data-pageid=tab1] #child-one').click(); + await ionPageVisible(page, 'tab1childone'); + + // Tapping the active tab resets it via router.go(-1) + await tabClick(page, 'tab1'); + await ionPageVisible(page, 'tab1'); + await ionPageDoesNotExist(page, 'tab1childone'); + + // Pushing tab2 overwrites the history entry of tab1's child + await tabClick(page, 'tab2'); + await ionPageVisible(page, 'tab2'); + + await tabClick(page, 'tab1'); + await ionPageVisible(page, 'tab1'); + await expect(page).toHaveURL(/\/tabs\/tab1(\?|$)/); + await expect(page.locator('div.ion-page[data-pageid=tab1]')).not.toHaveClass(/can-go-back/); + }); });