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): navbar dropdown crash when item.to is undefined #3662

Merged

Conversation

artemkovalyov
Copy link
Contributor

Motivation

According to the docs, you can create a dropdown in the Navbar. With alpha.66 this fails because there's a path normalizer, which doesn't handle undefined if Navbar item is dropdown instead of a link which has a path.

Have you read the Contributing Guidelines on pull requests?

Yes and already contributed once.

Test Plan

I've written a test:

test('should not fail if path is "undefined" when it is absent in case of dropdown in Navbar ', () => {
    expect(isSamePath(undefined, undefined)).toBeTruthy();
  });

Before my fix:
image

After my fix:
image

Related PRs

This fix actually makes docs relevant as currently, dropdown wouldn't work.

@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label Oct 30, 2020
@artemkovalyov
Copy link
Contributor Author

It's not a very semantical fix as the path shouldn't be checked at all in case there're items on the Navbar item, but apparently, normalization wouldn't be required in this case, and fixing with check undefined returned the Navbar back to business.

@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented Oct 30, 2020

Deploy preview for docusaurus-2 ready!

Built with commit 1724fee

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

@@ -8,5 +8,5 @@
// Compare the 2 paths, ignoring trailing /
export const isSamePath = (path1, path2) => {
const normalize = (str) => (str.endsWith('/') ? str : `${str}/`);
return normalize(path1) === normalize(path2);
return path1 && path2 ? normalize(path1) === normalize(path2) : true;
Copy link
Collaborator

Choose a reason for hiding this comment

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

thanks

What if one path is undefined and the other is defined, wouldn't it wrongly return true?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This function only makes sense if a path exists as a field, otherwise it doesn't matter what it returns. If path is missing this function shouldn't be called at all, which happens when Navbar has a drop-down which holds a list of entries instead of a path.

With this quick fix I made it check for undefined and return arbitrary value further to hope the underlying logic works and with "true" it does work, probably with false as well as there's just no path.

The real fix should be somewhere in the navbar generation and there's obviously no test for a navbar with a drop-down which would catch this.

In 2.0.0-alpha.65 it still works fine. With .66 it breaks. Removing a drop-down from docusaurus config makes it work with with .66 as well.

The fix I propose is doing nothing bad, it simply does the check only if none of the values are undefined and returns smth to avoid 'undefined' further.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's more of a workaround. I'll see if I can fix it in the reverting of the Navbar

@artemkovalyov artemkovalyov requested review from slorber and removed request for lex111 November 1, 2020 22:20
Copy link
Contributor Author

@artemkovalyov artemkovalyov left a comment

Choose a reason for hiding this comment

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

I updated my fix to be more semantic. I expect normalization is not required for external links.

@@ -159,7 +159,7 @@ function NavItemMobile({
const menuListRef = useRef<HTMLUListElement>(null);
const {pathname} = useLocation();
const [collapsed, setCollapsed] = useState(
() => !items?.some((item) => isSamePath(item.to, pathname)) ?? true,
() => !items?.filter((item) => item.to).some((item) => isSamePath(item.to, pathname)) ?? true,
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This now filters out all the navbar links with href instead of to so that we don't get undefined down the line. Path normalization is not required on external links I assume? Or instead of passing further item.to, you have to path just item and later check the path for to and href

@@ -7,6 +7,6 @@

// Compare the 2 paths, ignoring trailing /
export const isSamePath = (path1, path2) => {
const normalize = (str) => (str.endsWith('/') ? str : `${str}/`);
return normalize(path1) === normalize(path2);
const normalize = (str) => (str?.endsWith('/') ? str : `${str}/`);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is another safeguard for undefined. With such a fix it will work even without filtering done above and lead to a consistent Navbar menu structure.

@slorber slorber changed the title Fix for undefined path with dropdown menu in Navbar fix(v2): navbar dropdown crash when item.to is undefined Nov 2, 2020
@slorber
Copy link
Collaborator

slorber commented Nov 2, 2020

Thanks.

I've updated your PR, as I think making isSamePath support undefined will be better for the future, as we'll likely need this function in other themes we implement as well

@slorber slorber merged commit 5aca1d7 into facebook:master Nov 2, 2020
@slorber slorber added the pr: bug fix This PR fixes a bug in a past release. label Nov 2, 2020
@artemkovalyov
Copy link
Contributor Author

Looks good, thanks for acting on this. You're doing a great job guys:)

@slorber
Copy link
Collaborator

slorber commented Nov 3, 2020

thanks :)

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.

5 participants