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鈥檒l occasionally send you account related emails.

Already on GitHub? Sign in to your account

The baseUrl is not reflected in the URL generated by SearchBar. #2827

Closed
tetunori opened this issue May 28, 2020 · 0 comments 路 Fixed by #2838
Closed

The baseUrl is not reflected in the URL generated by SearchBar. #2827

tetunori opened this issue May 28, 2020 · 0 comments 路 Fixed by #2838
Labels
bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers

Comments

@tetunori
Copy link
Contributor

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

馃悰 Bug Report

When we input a word in the SearchBar, the page should transit to "Search results" page.
However, the URL of this page does not reflect the baseUrl setting.
For example, consider the case url='https://xyz.github.io', baseUrl='/test123/' and a search-target-word is apple.
I thinks expected generated URL should be 'https://xyz.github.io/test123/search?q=apple' but actual result is 'https://xyz.github.io/search?q=apple' so that we cannot access to "Search results" page from the Search bar.

Have you read the Contributing Guidelines on issues?

Yes!

To Reproduce

(Set baseUrl other than / beforehand.)

  1. Input a word in the SearchBar and hit Enter key.
    The issue occurred.

Expected behavior

The generated URL should be <url> + <baseUrl> + 'search?q=<word>'.

Actual Behavior

The generated URL is wrong because the URL is <url> + '/search?q=<word>' and does not include baseUrl.

Your Environment

  • Docusaurus version used: v2.0.0-alpha.56
  • Environment name and version: Chrome 81.0.4044.138, Node.js v12.16.2
  • Operating system and version: Windows 10 Home

Reproducible Demo

https://tetunori.github.io/p5.toio/
https://github.com/tetunori/p5.toio

Notes

I've just looked at the source codes for a moment and found that, at here, it seems this URL does not reflect baseURL.
So I think we need add/change the code like below and it worked fine in my environment.
Please confirm it.

import {useHistory, useLocation} from '@docusaurus/router';
import ExecutionEnvironment from '@docusaurus/ExecutionEnvironment';
import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; // Add

const SEARCH_PARAM_QUERY = 'q';

function useSearchQuery() {
  const history = useHistory();
  const location = useLocation();
  const {                         // Add
    siteConfig: {baseUrl} = {},   // Add
  } = useDocusaurusContext();     // Add

  return {
    searchValue:
      (ExecutionEnvironment.canUseDOM &&
        new URLSearchParams(location.search).get(SEARCH_PARAM_QUERY)) ||
      '',
    updateSearchPath: (searchValue) => {
      const searchParams = new URLSearchParams(location.search);

      if (searchValue) {
        searchParams.set(SEARCH_PARAM_QUERY, searchValue);
      } else {
        searchParams.delete(SEARCH_PARAM_QUERY);
      }

      history.replace({
        search: searchParams.toString(),
      });
    },
    navigateToSearchPage: (searchValue) => {
      // history.push(`/search?q=${searchValue}`); // <- 馃悰?
      history.push(baseUrl+`search?q=${searchValue}`);  // Changed
    },
  };
}

export default useSearchQuery;
@tetunori tetunori added bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers labels May 28, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug An error in the Docusaurus core causing instability or issues with its execution status: needs triage This issue has not been triaged by maintainers
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant