From 41417034ad1284513a63f8fd96db2b915f60e48a Mon Sep 17 00:00:00 2001 From: Bentley O'Kane-Chase Date: Tue, 17 Sep 2024 18:39:29 +1000 Subject: [PATCH 1/3] Add tests for finding last location --- .../__tests__/locationHistory.spec.ts | 21 +++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/packages/vue-router/__tests__/locationHistory.spec.ts b/packages/vue-router/__tests__/locationHistory.spec.ts index fe7f94e723d..196dcc75d88 100644 --- a/packages/vue-router/__tests__/locationHistory.spec.ts +++ b/packages/vue-router/__tests__/locationHistory.spec.ts @@ -84,4 +84,25 @@ describe('Location History', () => { expect(locationHistory.canGoBack(1, 0, 1)).toEqual(true); expect(locationHistory.canGoBack(2, 0, 1)).toEqual(false); }); + + it('should correctly find the last location', () => { + const [home, pageA, pageB, pageC] = [ + { pathname: '/home' }, + { pathname: '/page-a', pushedByRoute: '/home' }, + { pathname: '/page-b', pushedByRoute: '/page-a' }, + { pathname: '/page-c', pushedByRoute: '/page-b' }, + ]; + + locationHistory.add(home); + locationHistory.add(pageA); + locationHistory.add(pageB); + locationHistory.add(pageC); + + expect(locationHistory.findLastLocation(pageB)).toEqual(pageA); + expect(locationHistory.findLastLocation(pageB, -2)).toEqual(home); + + expect(locationHistory.findLastLocation(pageC)).toEqual(pageB); + expect(locationHistory.findLastLocation(pageC, -2)).toEqual(pageA); + expect(locationHistory.findLastLocation(pageC, -3)).toEqual(home); + }); }); From f03cc5a04d4353eebe37bd7083029b0efc0f6a9d Mon Sep 17 00:00:00 2001 From: Bentley O'Kane-Chase Date: Tue, 17 Sep 2024 18:41:01 +1000 Subject: [PATCH 2/3] Fix findLastLocation so that deltas less than -1 are still relative to the specified route --- packages/vue-router/src/locationHistory.ts | 13 ++++++------- 1 file changed, 6 insertions(+), 7 deletions(-) diff --git a/packages/vue-router/src/locationHistory.ts b/packages/vue-router/src/locationHistory.ts index f48499254ea..53859baaee2 100644 --- a/packages/vue-router/src/locationHistory.ts +++ b/packages/vue-router/src/locationHistory.ts @@ -239,15 +239,14 @@ export const createLocationHistory = () => { } } } - if (delta < -1) { - return locationHistory[locationHistory.length - 1 + delta]; - } else { - for (let i = locationHistory.length - 2; i >= 0; i--) { - const ri = locationHistory[i]; - if (ri) { - if (ri.pathname === routeInfo.pushedByRoute) { + for (let i = locationHistory.length - 2; i >= 0; i--) { + const ri = locationHistory[i]; + if (ri) { + if (ri.pathname === routeInfo.pushedByRoute) { + if (delta >= -1) { return ri; } + return locationHistory[i + 1 + delta] } } } From 90b09ba3eb4d2990491e8eae37bb7fde6649e139 Mon Sep 17 00:00:00 2001 From: Bentley O'Kane-Chase Date: Tue, 17 Sep 2024 19:04:35 +1000 Subject: [PATCH 3/3] Simplify Co-authored-by: xxllxhdj <12881488+xxllxhdj@users.noreply.github.com> --- packages/vue-router/src/locationHistory.ts | 3 --- 1 file changed, 3 deletions(-) diff --git a/packages/vue-router/src/locationHistory.ts b/packages/vue-router/src/locationHistory.ts index 53859baaee2..9e12afd799a 100644 --- a/packages/vue-router/src/locationHistory.ts +++ b/packages/vue-router/src/locationHistory.ts @@ -243,9 +243,6 @@ export const createLocationHistory = () => { const ri = locationHistory[i]; if (ri) { if (ri.pathname === routeInfo.pushedByRoute) { - if (delta >= -1) { - return ri; - } return locationHistory[i + 1 + delta] } }