Skip to content

fix(vue-router): Prev route last pathname incorrect #24433

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
47 commits
Select commit Hold shift + click to select a range
3cff3d8
added mount/demount method check if leaving view exists when calling …
tigohenryschultz Dec 16, 2021
39c21dd
Refactored routing code to properly handle replaced routes and mainta…
tigohenryschultz Dec 16, 2021
4332e0b
Consolidate mounting/unmounting
tigohenryschultz Dec 16, 2021
ff44798
revert code format
tigohenryschultz Dec 17, 2021
87254dd
Cleaned up handleNavigateBack to fail first instead of multiple nest…
tigohenryschultz Dec 17, 2021
0ff060a
Cleaned up locatioHistory, reduced method sizes, added explicit varia…
tigohenryschultz Dec 17, 2021
3c8b2f7
added explicit variables names to make readability easier
tigohenryschultz Dec 17, 2021
18e5203
Fixed for tabs, pushedByRoute should be the last non-tab route pushed…
tigohenryschultz Dec 17, 2021
27d91a4
Removed prevRouteLastPathname and use lastPathinfo when determining l…
tigohenryschultz Dec 20, 2021
7675960
Removed console warn when navigating to/from new ion-router-outlet pages
tigohenryschultz Dec 20, 2021
53264b4
Handle undefined viewItem when navigating backwards, added console wa…
tigohenryschultz Dec 20, 2021
b2cbd24
Updated comments for RouteInfo types
tigohenryschultz Dec 20, 2021
d6c7ed2
Fixed condition for tab being null
tigohenryschultz Dec 21, 2021
fad776b
cleaned up test view structure
tigohenryschultz Dec 21, 2021
c986988
mark packages as eslint root, added echos
tigohenryschultz Dec 21, 2021
0ddbdc4
Added sync-dev script to copy transpiled code. Code formated and fix …
tigohenryschultz Dec 28, 2021
c528c0c
use existing route id if set, else generate new route id
tigohenryschultz Dec 28, 2021
4588d31
Added comments, additional scripts
tigohenryschultz Dec 30, 2021
ed983c9
Added ability to find view stack regardless of outletId, use exact ma…
tigohenryschultz Dec 30, 2021
cbdb4d5
Added ability to getOutletByPath
tigohenryschultz Dec 30, 2021
cd09dd1
Added getOutletByPath to locate the correct outletId for a views root…
tigohenryschultz Dec 30, 2021
bb82c95
Force first outlet to be 1 => '/'
tigohenryschultz Dec 30, 2021
ae789b1
Check for undefined and remove unmounting for 'push'/'forward' action…
tigohenryschultz Dec 30, 2021
7f4a932
Fix some tests, handle fallback outlet ids
tigohenryschultz Dec 30, 2021
ccb0eba
Fixed logic for setupViewItem in wrong outlet
tigohenryschultz Jan 4, 2022
17a4215
Out now handles page transitions for lower level outlets too
tigohenryschultz Jan 5, 2022
6cd8377
Remove ion-page-hidden from entering el if it has it.
tigohenryschultz Jan 6, 2022
a406514
Fixed test cases
tigohenryschultz Jan 6, 2022
3d6a93c
All test cases fixed. No longer do view item get added to the wrong o…
tigohenryschultz Jan 6, 2022
47a98ab
Fixes can-go-back
czzplnm Jan 7, 2022
0a529fd
fixes ion-page-hidden for certain route scenarios
czzplnm Jan 7, 2022
e305e52
Fixed history stack index logic
czzplnm Jan 8, 2022
155b7fe
removed prevRouteLastPathname
czzplnm Jan 8, 2022
98ea81e
Fixed condition for default-href test scenario
czzplnm Jan 8, 2022
ad61a33
When using router.go, pushedByRoute can get lost, retrieve that from …
tigohenryschultz Jan 10, 2022
0136a17
Fixed tab id for route
tigohenryschultz Jan 10, 2022
4d83b73
Fixed test case
tigohenryschultz Jan 10, 2022
6a32802
Added test case for router.replace bug issue: 24432
tigohenryschultz Jan 10, 2022
af3a5ed
Fixed router direction and removing/unmounting replaced routes
tigohenryschultz Jan 11, 2022
2e3c7a7
Fix unit tests, fixed unmounted views
tigohenryschultz Jan 12, 2022
6ca6185
Keep track of matched paths to know when we should replaced
tigohenryschultz Jan 12, 2022
76f90fc
Fixed test condition for pageid's that don't exist anymore
tigohenryschultz Jan 12, 2022
935f37e
added jest and test:debug command to debug in VScode
tigohenryschultz Jan 12, 2022
b34bc41
Fixed unit tests, pages exist until router.replaced or router.push ov…
tigohenryschultz Jan 12, 2022
e478090
Added Liams fix from: https://github.com/ionic-team/ionic-framework/i…
tigohenryschultz Jan 12, 2022
7bd1b57
Added test case for all properly being ion-page-hidden when navigatin…
tigohenryschultz Jan 12, 2022
904761d
Code formatted and cleaned up code
czzplnm Jan 13, 2022
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion packages/vue-router/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@
"module": "./dist/index.esm.js",
"types": "./dist/types/index.d.ts",
"files": [
"dist/"
"dist/",
"dist-transpiled/"
],
"repository": {
"type": "git",
Expand Down Expand Up @@ -66,5 +67,15 @@
"json",
"jsx"
]
},
"eslintConfig": {
"root": true,
"parserOptions": {
"ecmaVersion": 6,
"sourceType": "module"
},
"env": {
"es6": true
}
}
}
168 changes: 133 additions & 35 deletions packages/vue-router/src/locationHistory.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
import { RouteInfo } from './types';

//@TODO: declare types
export const createLocationHistory = () => {
const locationHistory: RouteInfo[] = [];
const replacedRoutes: RouteInfo[] = [];
const tabsHistory: { [k: string]: RouteInfo[] } = {};

/**
* @TODO: refactor method, add is misleading as the behavior could remove,replace or add, maybe a method called `handle`
* @param routeInfo
*/
const add = (routeInfo: RouteInfo) => {
switch (routeInfo.routerAction) {
case "replace":
Expand Down Expand Up @@ -41,43 +47,63 @@ export const createLocationHistory = () => {
}
}

/**
*
* @param routeInfo
*/
const replaceRoute = (routeInfo: RouteInfo) => {
const routeInfos = getTabsHistory(routeInfo.tab);
routeInfos && routeInfos.pop();
if (routeInfos !== undefined) {
routeInfos.pop();
}

locationHistory.pop();
addRoute(routeInfo);
}

/**
*
* @param routeInfo
*/
const pop = (routeInfo: RouteInfo) => {
const tabHistory = getTabsHistory(routeInfo.tab);
let ri;
let _routeInfo
if (tabHistory) {
/**
* When navigating between tabs, each tab is added onto history stack, but using device back we will pop all instances of that tab from the stack
*/
// Pop all routes until we are back
ri = tabHistory[tabHistory.length - 1];
while (ri && ri.id !== routeInfo.id) {
_routeInfo = lastTab(routeInfo.tab);// tabHistory[tabHistory.length - 1];
while (_routeInfo && _routeInfo.id !== routeInfo.id) {
tabHistory.pop();
ri = tabHistory[tabHistory.length - 1];
_routeInfo = lastTab(routeInfo.tab);
}
// Replace with updated route
tabHistory.pop();
tabHistory.push(routeInfo);
}

ri = locationHistory[locationHistory.length - 1];
while (ri && ri.id !== routeInfo.id) {
_routeInfo = last();
while (_routeInfo && _routeInfo.id !== routeInfo.id) {
locationHistory.pop();
ri = locationHistory[locationHistory.length - 1];
_routeInfo = last();
}
// Replace with updated route
locationHistory.pop();
locationHistory.push(routeInfo);
}

/**
*
* @param routeInfo
*/
const addRoute = (routeInfo: RouteInfo) => {
const tabHistory = getTabsHistory(routeInfo.tab);
if (tabHistory) {
// If the latest routeInfo is the same (going back and forth between tabs), replace it
if (tabHistory[tabHistory.length - 1] && tabHistory[tabHistory.length - 1].id === routeInfo.id) {
const previousTabHistory = tabHistory[tabHistory.length - 1];
const hasPreviousHistory = previousTabHistory !== undefined;
if (hasPreviousHistory && previousTabHistory.id === routeInfo.id) {
tabHistory.pop();
}
tabHistory.push(routeInfo);
Expand All @@ -91,6 +117,11 @@ export const createLocationHistory = () => {
tabsHistory[key] = [];
});
}

/**
*
* @param tab
*/
const getTabsHistory = (tab: string): RouteInfo[] => {
let history;
if (tab) {
Expand All @@ -105,15 +136,40 @@ export const createLocationHistory = () => {

const size = () => locationHistory.length;

const updateByHistoryPosition = (routeInfo: RouteInfo, updateEntries: boolean) => {
/**
* When performing a router.replace, we need to replace the routeInfo at the route index
* @param routeInfo
*/
const updateByHistoryPosition = (routeInfo: RouteInfo) => {
const existingRouteIndex = locationHistory.findIndex(r => r.position === routeInfo.position);
if (existingRouteIndex === -1) return;
if (existingRouteIndex === -1) {
console.error('Trying to update history position for non-existing route');
return;
}

locationHistory[existingRouteIndex].pathname = routeInfo.pathname;
let previous = locationHistory[existingRouteIndex];

if (updateEntries) {
locationHistory[existingRouteIndex].pushedByRoute = routeInfo.pushedByRoute;
//is a different matchedPath, same matched paths do not need to be replaced
if (previous.matchedPath !== routeInfo.matchedPath) {
replacedRoutes.push(previous);
}

locationHistory[existingRouteIndex] = routeInfo;
}

/**
* When a route is `updateByHistoryPosition` and replaced, it is added to replacedRoutes
*/
const getReplacedRoutes = () => {
return replacedRoutes;
}

/**
* When the RouteInfo is properly demounted, remove from here
* @param index
*/
const clearReplacedRouteByIndex = (index: number) => {
replacedRoutes.splice(index, 1);
}

/**
Expand All @@ -131,9 +187,31 @@ export const createLocationHistory = () => {
const index = currentHistory - initialHistory;
return locationHistory[index] || last();
}

/**
*
* @param index
*/
const getHistoryByIndex = (index: number) => {
return locationHistory[index];
}

/**
* This assumes the user is at the current index
*/
const previous = () => locationHistory[locationHistory.length - 2] || last();
/**
* This assumes the user is at the current index
*/
const last = () => locationHistory[locationHistory.length - 1];
const lastTab = (tab: string) => {
let tabHistory = getTabsHistory(tab);
if (tabHistory === undefined) {
return undefined;
}

return tabHistory[tabHistory.length - 1];
}
/**
* With the introduction of router.go support, we no longer remove
* items from locationHistory as they may be needed again in the future.
Expand Down Expand Up @@ -162,6 +240,32 @@ export const createLocationHistory = () => {
return undefined;
}

/**
*
* @param routeInfo
* @param delta
*/
const findTabLastLocation = (routeInfo: RouteInfo, delta: number = -1): RouteInfo | undefined => {
const routeInfos = getTabsHistory(routeInfo.tab);
if (routeInfos === undefined) {
return undefined;
}
if (delta < -1) {
return routeInfos[routeInfos.length - 1 + delta];
}

for (let i = routeInfos.length - 2; i >= 0; i--) {
const ri = routeInfos[i];
if (ri) {
if (ri.pathname === routeInfo.pushedByRoute) {
return ri;
}
}
}

return undefined;
}

/**
* Finds and returns the previous view based upon
* what originally pushed it (pushedByRoute).
Expand All @@ -173,39 +277,33 @@ export const createLocationHistory = () => {
* not update pushedByRoute anyways.
*/
const findLastLocation = (routeInfo: RouteInfo, delta: number = -1): RouteInfo | undefined => {
const routeInfos = getTabsHistory(routeInfo.tab);
if (routeInfos) {
if (delta < -1) {
return routeInfos[routeInfos.length - 1 + delta];
} else {
for (let i = routeInfos.length - 2; i >= 0; i--) {
const ri = routeInfos[i];
if (ri) {
if (ri.pathname === routeInfo.pushedByRoute) {
return ri;
}
}
}
}
const lastTabLocation = findTabLastLocation(routeInfo, delta);
if (lastTabLocation !== undefined) {
return lastTabLocation;
}

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 ri;
}
}
}

return undefined;
}

return {
current,
updateByHistoryPosition,
getHistoryByIndex,
getReplacedRoutes,
clearReplacedRouteByIndex,
size,
last,
previous,
Expand Down
Loading