fix(vue): routing history is correctly replaced when overwriting browser history (#24670)

resolves #23873
This commit is contained in:
Liam DeBeasi
2022-02-02 13:35:52 -05:00
committed by GitHub
parent bf9b4dfb4e
commit 0b18260da6
6 changed files with 341 additions and 145 deletions

View File

@ -85,11 +85,25 @@ export const createLocationHistory = () => {
locationHistory.push(routeInfo);
}
const clearHistory = () => {
locationHistory.length = 0;
/**
* Wipes the location history arrays.
* You can optionally provide a routeInfo
* object which will wipe that entry
* and every entry that appears after it.
*/
const clearHistory = (routeInfo?: RouteInfo) => {
Object.keys(tabsHistory).forEach(key => {
tabsHistory[key] = [];
});
if (routeInfo) {
const existingRouteIndex = locationHistory.findIndex(r => r.position === routeInfo.position);
if (existingRouteIndex === -1) return;
locationHistory.splice(existingRouteIndex);
} else {
locationHistory.length = 0;
}
}
const getTabsHistory = (tab: string): RouteInfo[] => {
let history;
@ -105,17 +119,6 @@ export const createLocationHistory = () => {
const size = () => locationHistory.length;
const updateByHistoryPosition = (routeInfo: RouteInfo, updateEntries: boolean) => {
const existingRouteIndex = locationHistory.findIndex(r => r.position === routeInfo.position);
if (existingRouteIndex === -1) return;
locationHistory[existingRouteIndex].pathname = routeInfo.pathname;
if (updateEntries) {
locationHistory[existingRouteIndex].pushedByRoute = routeInfo.pushedByRoute;
}
}
/**
* Finds and returns the location history item
* given the state of browser's history API.
@ -205,7 +208,6 @@ export const createLocationHistory = () => {
return {
current,
updateByHistoryPosition,
size,
last,
previous,
@ -214,6 +216,7 @@ export const createLocationHistory = () => {
update,
getFirstRouteInfoForTab,
getCurrentRouteInfoForTab,
findLastLocation
findLastLocation,
clearHistory
}
}