diff --git a/packages/docusaurus-theme-search-algolia/src/theme/SearchBar/index.js b/packages/docusaurus-theme-search-algolia/src/theme/SearchBar/index.js index 6a79cadf4178..6ef3809b0198 100644 --- a/packages/docusaurus-theme-search-algolia/src/theme/SearchBar/index.js +++ b/packages/docusaurus-theme-search-algolia/src/theme/SearchBar/index.js @@ -5,47 +5,48 @@ * LICENSE file in the root directory of this source tree. */ -import React, { - useState, - useEffect, - useContext, - useRef, - useCallback, -} from 'react'; +import React, {useRef, useCallback} from 'react'; import classnames from 'classnames'; -import DocusaurusContext from '@docusaurus/context'; +import useDocusaurusContext from '@docusaurus/useDocusaurusContext'; import './styles.css'; +const loadJS = () => import('docsearch.js'); +let loadedJs = false; + const Search = props => { - const [isEnabled, setIsEnabled] = useState(true); + const initialized = useRef(false); const searchBarRef = useRef(null); - const context = useContext(DocusaurusContext); + const {siteConfig = {}} = useDocusaurusContext(); + const { + themeConfig: {algolia}, + } = siteConfig; - useEffect(() => { - const {siteConfig = {}} = context; - const { - themeConfig: {algolia}, - } = siteConfig; + const initAlgolia = () => { + if (!initialized.current) { + window.docsearch({ + appId: algolia.appId, + apiKey: algolia.apiKey, + indexName: algolia.indexName, + inputSelector: '#search_input_react', + algoliaOptions: algolia.algoliaOptions, + }); + initialized.current = true; + } + }; - // https://github.com/algolia/docsearch/issues/352 - const isClient = typeof window !== 'undefined'; - if (isClient) { - import('docsearch.js').then(({default: docsearch}) => { - docsearch({ - appId: algolia.appId, - apiKey: algolia.apiKey, - indexName: algolia.indexName, - inputSelector: '#search_input_react', - algoliaOptions: algolia.algoliaOptions, - }); + const loadAlgoliaJS = () => { + if (!loadedJs) { + loadJS().then(a => { + loadedJs = true; + window.docsearch = a.default; + initAlgolia(); }); } else { - console.warn('Search has failed to load and now is being disabled'); - setIsEnabled(false); + initAlgolia(); } - }, []); + }; const toggleSearchIconClick = useCallback( e => { @@ -58,7 +59,7 @@ const Search = props => { [props.isSearchBarExpanded], ); - return isEnabled ? ( + return (