Skip to content

Commit

Permalink
Merge pull request #62 from jknoxville/master
Browse files Browse the repository at this point in the history
Load search index on initial render
  • Loading branch information
lelouch77 authored Oct 30, 2021
2 parents 3f72f7c + f61fa15 commit 44d1acd
Showing 1 changed file with 10 additions and 3 deletions.
13 changes: 10 additions & 3 deletions src/theme/SearchBar/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,17 @@
* LICENSE file in the root directory of this source tree.
*/

import React, { useRef, useCallback } from "react";
import React, { useRef, useCallback, useState } from "react";
import classnames from "classnames";
import { useHistory } from "@docusaurus/router";
import useDocusaurusContext from "@docusaurus/useDocusaurusContext";
import { usePluginData } from '@docusaurus/useGlobalData';
const Search = props => {
const initialized = useRef(false);
const searchBarRef = useRef(null);
const [indexReady, setIndexReady] = useState(false);
const history = useHistory();
const { siteConfig = {} } = useDocusaurusContext();
const { siteConfig = {}, isClient = false} = useDocusaurusContext();
const { baseUrl } = siteConfig;
const initAlgolia = (searchDocs, searchIndex, DocSearch) => {
new DocSearch({
Expand Down Expand Up @@ -60,6 +61,7 @@ const Search = props => {
return;
}
initAlgolia(searchDocs, searchIndex, DocSearch);
setIndexReady(true);
});
initialized.current = true;
}
Expand All @@ -76,6 +78,10 @@ const Search = props => {
[props.isSearchBarExpanded]
);

if (isClient) {
loadAlgolia();
}

return (
<div className="navbar__search" key="search-box">
<span
Expand All @@ -91,7 +97,7 @@ const Search = props => {
<input
id="search_input_react"
type="search"
placeholder="Search"
placeholder={indexReady ? 'Search' : 'Loading...'}
aria-label="Search"
className={classnames(
"navbar__search-input",
Expand All @@ -103,6 +109,7 @@ const Search = props => {
onFocus={toggleSearchIconClick}
onBlur={toggleSearchIconClick}
ref={searchBarRef}
disabled={!indexReady}
/>
</div>
);
Expand Down

0 comments on commit 44d1acd

Please sign in to comment.