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): use base url to navigate to search page #2838

Merged
merged 4 commits into from
Jun 2, 2020

Conversation

tetunori
Copy link
Contributor

@tetunori tetunori commented May 29, 2020

Motivation

To resolve #2827.

Have you read the Contributing Guidelines on pull requests?

Yes.

Test Plan

  • Linted (yarn prettier && yarn lint).
  • Tested (yarn test)
    • but target file did not have any tests.
  • Replacing the changed file on my test site to ensure that the defect is fixed.

Add baseUrl to search page query URL.
@tetunori tetunori requested a review from yangshun as a code owner May 29, 2020 13:48
@facebook-github-bot facebook-github-bot added the CLA Signed Signed Facebook CLA label May 29, 2020
@docusaurus-bot
Copy link
Contributor

docusaurus-bot commented May 29, 2020

Deploy preview for docusaurus-2 ready!

Built with commit 22985fb

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

Copy link
Contributor

@yangshun yangshun left a comment

Choose a reason for hiding this comment

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

Thanks! This fix is in the right direction, but we should just use the useBaseUrl() hook in @docusaurus/useBaseUrl

@yangshun yangshun changed the title fix: consider baseUrl fix(v2): use baseUrl on search page May 30, 2020
@yangshun yangshun added the pr: bug fix This PR fixes a bug in a past release. label May 30, 2020
@yangshun yangshun requested a review from lex111 May 30, 2020 05:29
@tetunori
Copy link
Contributor Author

tetunori commented May 30, 2020

@yangshun thanks for your review!
I've replaced useDocusaurusContext() with useBaseUrl().
lint/prettier/test was OK and this fix seems to work fine on my local site .
but I'm not 100% sure about how to use useBaseUrl() function so please re-review my new code. Thanks.
(Since this is my 1st PR in GitHub, I'm excited about your reaction!)

@tetunori
Copy link
Contributor Author

tetunori commented May 30, 2020

I found a side effect and it might be caused by useBaseUrl().
Other than top page, search result page URL was wrong.
For example, we are in the page <url>/<baseUrl>/docs/intro and search there, destination is expected as <url>/<baseUrl>/search?q=<word> but actual was <url>/<baseUrl>/docs/search?q=<word>. Please confirm useBaseUrl() behavior.

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.

@tetunori due to the Rules of hooks, in this case it will not be possible to use useBaseUrl hook. Therefore, your original solution was correct.


const SEARCH_PARAM_QUERY = 'q';

function useSearchQuery() {
const history = useHistory();
const location = useLocation();
const baseUrl = useBaseUrl('');
Copy link
Contributor

Choose a reason for hiding this comment

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

You should use it as such -

const newPath = useBaseUrl(`search?q=${searchValue}`);

Copy link
Contributor

Choose a reason for hiding this comment

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

Don’t call Hooks inside loops, conditions, or nested functions

Would that work? I do not think so. Therefore siteConfig.baseUrl is a good option.

Copy link
Contributor

@yangshun yangshun May 31, 2020

Choose a reason for hiding this comment

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

This is a hook, why would it not work? How would useHistory() and useLocation() work then?

Copy link
Contributor

Choose a reason for hiding this comment

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

Try it yourself and you will see that it does not work.

    navigateToSearchPage: (searchValue) => { 
      const searchUrl = useBaseUrl(`search?q=${searchValue}`); // Not work - call hook in nested functions.
      history.push(searchUrl);
    },

Copy link
Contributor

Choose a reason for hiding this comment

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

If we put it at the top level of the function scope, alongside useLocation() and useHistory(), and not in the navigateToSearchPage() function, would that not work? That is my suggestion.

Copy link
Contributor

Choose a reason for hiding this comment

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

Nope, this way also will not work.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Try it yourself and you will see that it does not work.

    navigateToSearchPage: (searchValue) => { 
      const searchUrl = useBaseUrl(`search?q=${searchValue}`); // Not work - call hook in nested functions.
      history.push(searchUrl);
    },

I confirmed this did not work because of the error "Invalid hook call".

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If we put it at the top level of the function scope, alongside useLocation() and useHistory(), and not in the navigateToSearchPage() function, would that not work? That is my suggestion.

Thanks, but in the top level of the function scope, we cannot get searchValue defined in navigateToSearchPage(). So your suggestion would not work, I think.

Copy link
Contributor

@yangshun yangshun Jun 1, 2020

Choose a reason for hiding this comment

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

You're right, I failed to realize we need searchValue. Hmm I guess this is fine then. Mind leaving a comment in the code and why we're doing this? Could add a link to this PR in the comment to give more context :)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@yangshun @lex111 Thanks for your reviews, guys.
I will add the comment including this PR URL.

@tetunori
Copy link
Contributor Author

tetunori commented Jun 1, 2020

Added link to this PR and did following tests.

  • Linted (yarn prettier && yarn lint).
  • Tested (yarn test)
    • but target file did not have any tests.
  • Replacing the changed file on my test site to ensure that the defect is fixed.

@lex111 lex111 changed the title fix(v2): use baseUrl on search page fix(v2): use base url to navigate to search page Jun 2, 2020
@lex111 lex111 merged commit e63c468 into facebook:master Jun 2, 2020
@lex111
Copy link
Contributor

lex111 commented Jun 2, 2020

@tetunori thank you!

@tetunori tetunori deleted the resolve-2827 branch June 2, 2020 11:21
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.

The baseUrl is not reflected in the URL generated by SearchBar.
5 participants