Skip to content

Commit

Permalink
fix(vue): incorrect view rendered when using router.go(-n) (#29877)
Browse files Browse the repository at this point in the history
resolves #28201

This PR fixes the navigation issue related to `router.go` that was
identified in issue #28201. After working on this issue I realised that
@xxllxhdj has already created a PR for this in #29847. While their fix
is great, I have added tests to replicate the issue, reused existing
code and `undefined` will be returned in unexpected situations - which
matches the existing functionality.

## What is the current behavior?

If a user navigates from `/home` -> `/pageA` -> `/pageB` -> `/pageC` ->
back to `/pageB` -> then `router.go(-2)` is called the URL will be
updated to `/home` correctly, but the app will try to render `/pageA`.

This happens for any delta < -1.

## What is the new behavior?

The app will correctly render `/pageA`, which matches the URL.

## Does this introduce a breaking change?

- [ ] Yes
- [X] No

---------

Co-authored-by: xxllxhdj <12881488+xxllxhdj@users.noreply.github.com>
  • Loading branch information
bentleyo and xxllxhdj authored Oct 22, 2024
1 parent 47ba703 commit e32fbe0
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 9 deletions.
21 changes: 21 additions & 0 deletions packages/vue-router/__tests__/locationHistory.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
14 changes: 5 additions & 9 deletions packages/vue-router/src/locationHistory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -239,15 +239,11 @@ 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) {
return ri;
}
for (let i = locationHistory.length - 2; i >= 0; i--) {
const ri = locationHistory[i];
if (ri) {
if (ri.pathname === routeInfo.pushedByRoute) {
return locationHistory[i + 1 + delta]
}
}
}
Expand Down

0 comments on commit e32fbe0

Please sign in to comment.