Skip to content
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

fix(router): pop to previous route with params #24315

Merged
merged 1 commit into from Dec 6, 2021
Merged

Conversation

sean-perkins
Copy link
Contributor

@sean-perkins sean-perkins commented Dec 4, 2021

Pull request checklist

Please check if your PR fulfills the following requirements:

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)
  • Build (npm run build) was run locally and any changes were pushed
  • Lint (npm run lint) has passed locally and any fixes were made for failures

Pull request type

Please check the type of change your PR introduces:

  • Bugfix
  • Feature
  • Code style update (formatting, renaming)
  • Refactoring (no functional changes, no api changes)
  • Build related changes
  • Documentation content changes
  • Other (please describe):

What is the current behavior?

The function used to determine the correct path to resolve for ion-router does not take route parameters into consideration. It only compares the route ids, with the first definition always taking priority over all other routes. This means that if a router has multiple routes sharing the same component, the first definition will always be the route that is determined when running the matching algorithm.

i.e.:

<ion-route url="/" component="my-page" />
<ion-route url="/:s1" component="my-page" />
<ion-route url="/:s1/:s2" component="my-page" />
<ion-route url="/:s1/:s2/:s3" component="my-page" />

/ will always resolve as the path when determining the route to navigate to when going back to my-page component.

Issue Number: Resolves #24223

What is the new behavior?

When calculating the best matched route in the chain, routes that both match the active route's id (page component match) and match the definition of route params will be weighted higher.

Does this introduce a breaking change?

  • Yes
  • No

Other information

The behavior before this PR (URL resets on pop or back interaction):

Kapture.2021-12-02.at.22.10.15.mp4

The behavior after this PR:

Kapture.2021-12-02.at.22.07.51.mp4

Copy link
Contributor

@amandaejohnston amandaejohnston left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had a couple thoughts, but I trust your judgment on whether changes actually need to be made; I might be misunderstanding the logic.

core/src/components/router/utils/matching.ts Show resolved Hide resolved
core/src/components/router/utils/matching.ts Outdated Show resolved Hide resolved
Copy link
Contributor

@liamdebeasi liamdebeasi left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Other than the Object.keys change, this PR looks good. Can we also change the commit subject to be something more descriptive? Maybe something like fix(router): popping route now accounts for route params.

Might be good to have the issue author verify the fix in their app if they have not already.

Great job!

Copy link
Contributor

@amandaejohnston amandaejohnston left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ditto 👍

const pathWithParams = routeIdParams.map(key => `:${key}`);
for (let j = 0; j < pathWithParams.length; j++) {
// Skip results where the path variable is not a match
if (pathWithParams[j].toLowerCase() !== routeChain.path[j]) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this assume that all segments of routeChain.path[j] are parameter (i.e. :param) ?
Would it work if some of the segments are fixed string ? (i.e. /fixed/:s1/:s2)

@vicb
Copy link
Contributor

vicb commented Jan 22, 2022

@sean-perkins Is the test app shown in the "Other information" section of the PR description available somewhere ? Shouldn't it be added as an e2e test ?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
package: core @ionic/core package
Projects
None yet
Development

Successfully merging this pull request may close these issues.

bug: ion-router can set wrong url after nav changes
4 participants