Skip to content

Commit a09d7d4

Browse files
authored
fix(vue): back button now selects correct route when navigating from view multiple times (#24060)
resolves #23987
1 parent ea34e50 commit a09d7d4

File tree

3 files changed

+67
-2
lines changed

3 files changed

+67
-2
lines changed

packages/vue-router/src/locationHistory.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,11 +105,15 @@ export const createLocationHistory = () => {
105105

106106
const size = () => locationHistory.length;
107107

108-
const updateByHistoryPosition = (routeInfo: RouteInfo) => {
108+
const updateByHistoryPosition = (routeInfo: RouteInfo, updateEntries: boolean) => {
109109
const existingRouteIndex = locationHistory.findIndex(r => r.position === routeInfo.position);
110110
if (existingRouteIndex === -1) return;
111111

112112
locationHistory[existingRouteIndex].pathname = routeInfo.pathname;
113+
114+
if (updateEntries) {
115+
locationHistory[existingRouteIndex].pushedByRoute = routeInfo.pushedByRoute;
116+
}
113117
}
114118

115119
/**

packages/vue-router/src/router.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,23 @@ export const createIonRouter = (opts: IonicVueRouterOptions, router: Router) =>
275275
* new items within the stack.
276276
*/
277277
if (historySize > historyDiff && routeInfo.tab === undefined) {
278-
locationHistory.updateByHistoryPosition(routeInfo);
278+
/**
279+
* When going from /a --> /a/1 --> /b, then going
280+
* back to /a, then going /a --> /a/2 --> /b, clicking
281+
* the ion-back-button should return us to /a/2, not /a/1.
282+
* However, since the route entry for /b already exists,
283+
* we need to update other information such as the "pushedByRoute"
284+
* so we know which route pushed this new route.
285+
*
286+
* However, when using router.go with a stride of >1 or <-1,
287+
* we should not update this additional information because
288+
* we are traversing through the history, not pushing new states.
289+
* Going from /a --> /b --> /c, then doing router.go(-2), then doing
290+
* router.go(2) to go from /a --> /c should not update the route
291+
* listing to say that /c was pushed by /a.
292+
*/
293+
const hasDeltaStride = delta !== undefined && Math.abs(delta) !== 1;
294+
locationHistory.updateByHistoryPosition(routeInfo, !hasDeltaStride);
279295
} else {
280296
locationHistory.add(routeInfo);
281297
}

packages/vue/test-app/tests/e2e/specs/routing.js

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -316,6 +316,51 @@ describe('Routing', () => {
316316
cy.ionPageDoesNotExist('routing');
317317
cy.ionPageDoesNotExist('routingparameter');
318318
})
319+
320+
// Verifies fix for https://github.com/ionic-team/ionic-framework/issues/23987
321+
it('should choose correct view when navigating back', () => {
322+
cy.visit('http://localhost:8080');
323+
324+
cy.routerPush('/routing');
325+
cy.ionPageVisible('routing');
326+
cy.ionPageHidden('home');
327+
328+
cy.routerPush('/routing/123/view');
329+
cy.ionPageVisible('routingparameterview');
330+
cy.ionPageHidden('routing');
331+
332+
cy.routerPush('/routing/child');
333+
cy.ionPageVisible('routingchild');
334+
cy.ionPageHidden('routing');
335+
336+
cy.ionBackClick('routingchild');
337+
cy.ionPageVisible('routingparameterview');
338+
cy.ionPageDoesNotExist('routingchild');
339+
340+
cy.ionBackClick('routingparameterview');
341+
cy.ionPageVisible('routing');
342+
cy.ionPageDoesNotExist('routingparameterview');
343+
344+
cy.ionBackClick('routing');
345+
cy.ionPageVisible('home');
346+
cy.ionPageDoesNotExist('routing');
347+
348+
cy.routerPush('/routing');
349+
cy.ionPageVisible('routing');
350+
cy.ionPageHidden('home');
351+
352+
cy.routerPush('/routing/456/view');
353+
cy.ionPageVisible('routingparameterview');
354+
cy.ionPageHidden('routing');
355+
356+
cy.routerPush('/routing/child');
357+
cy.ionPageVisible('routingchild');
358+
cy.ionPageHidden('routing');
359+
360+
cy.ionBackClick('routingchild');
361+
cy.ionPageVisible('routingparameterview');
362+
cy.ionPageDoesNotExist('routingchild');
363+
})
319364
});
320365

321366
describe('Routing - Swipe to Go Back', () => {

0 commit comments

Comments
 (0)