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(v2): normalizeUrl edge cases #3427

Merged
merged 2 commits into from
Sep 29, 2020

Conversation

ayshiff
Copy link
Contributor

@ayshiff ayshiff commented Sep 9, 2020

Motivation

This PR fixes edge cases for the normalizeUrl util function like the ones introduced in the unit tests.

Have you read the Contributing Guidelines on pull requests?

Yes

Test Plan

This PR fixes #3374 and undo #3377.

New unit tests added:

      {
        input: ['/', ''],
        output: '/',
      },
      {
        input: ['', '/'],
        output: '/',
      },
      {
        input: ['/'],
        output: '/',
      },
      {
        input: [''],
        output: '',
      }

The thing was that for each part of our rawUrls (except for the last one) we removed the / in:

component.replace(/[/]+$/, i < urls.length - 1 ? '' : ‘/‘)

I added a condition to check if the current element is a slash and if so we can continue the process.

Here is the new logic inside our for loop:

Another workaround would be to add a condition to the returned string like: return str === '' ? '/'  : str as I think this change target only an edge case.

NOTE: I am note sure about this solution ! It would be great if someone could give me their opinion on this 👍

Related PRs

This PR undo the #3377 changes.

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Sep 9, 2020
@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Sep 9, 2020

Deploy preview for docusaurus-2 ready!

Built without sensitive environment variables with commit 05d305d

https://deploy-preview-3427--docusaurus-2.netlify.app

@slorber
Copy link
Collaborator

slorber commented Sep 10, 2020

Thanks @ayshiff . That seems to solve the edge cases.

However, you should not revert the changes in the other PR, I'd like to keep them, as I don't think it's a good idea to enable users to provide routeBasePath = '' and it also contains other useful refactors.

It would be hard for me to review this 🤪 , but I guess if we have tests and they all pass it should be good.

What about adding more fancy edge case tests? I'm thinking of things like:

{
        input: ['/', '',"hello","","/","/","","/","/world"],
        output: '/hello/world',
},

You see what I mean?

@ayshiff
Copy link
Contributor Author

ayshiff commented Sep 11, 2020

@slorber I understand ! I'll keep the changes in the other PR.
I will add more fancy edge case tests then 👍

@ayshiff
Copy link
Contributor Author

ayshiff commented Sep 11, 2020

I spotted and fixed two new edge cases:

{
  input: ['', '/hello'],
  expected: '/hello',
  received: 'hello'
},
{
  input: ['hello/', ''],
  expected: 'hello/',
  received: 'hello'
}

For the starting slash the problem was that for every element which has an index greater than 1, we removed the starting slashes. So if we have something like ['', '/test'] it build the url: test.

We now have a new variable hasStartingSlash to cover that case.

The same problem was present for the ending slash, and the new variable hasEndingSlash should fix it 👍

Copy link
Contributor

@lex111 lex111 left a comment

Choose a reason for hiding this comment

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

Seems legit.

@slorber
Copy link
Collaborator

slorber commented Sep 23, 2020 via email

@slorber slorber added the pr: bug fix This PR fixes a bug in a past release. label Sep 29, 2020
@slorber
Copy link
Collaborator

slorber commented Sep 29, 2020

Thanks for your work @ayshiff , seems to be more reliable now ;)

@slorber slorber merged commit c8d4805 into facebook:master Sep 29, 2020
@lex111 lex111 added this to the v2.0.0-alpha.65 milestone Sep 29, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
CLA Signed Signed Facebook CLA pr: bug fix This PR fixes a bug in a past release.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

normalizeUrl bug leads to routeBasePath issues
5 participants