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

perf(v2): load algolia JS only when user interacts with search #2076

Merged
merged 2 commits into from
Dec 2, 2019
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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, {useState, 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, setInitialized] = useState(false);
endiliey marked this conversation as resolved.
Show resolved Hide resolved
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) {
window.docsearch({
appId: algolia.appId,
apiKey: algolia.apiKey,
indexName: algolia.indexName,
inputSelector: '#search_input_react',
algoliaOptions: algolia.algoliaOptions,
});
setInitialized(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 => {
Copy link
Contributor

Choose a reason for hiding this comment

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

Wanna add some error handling?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I dont know what kind of error should be introduced. Previously doesnt have it too.

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 => {
Expand All @@ -58,7 +59,7 @@ const Search = props => {
[props.isSearchBarExpanded],
);

return isEnabled ? (
return (
<div className="navbar__search" key="search-box">
<span
aria-label="expand searchbar"
Expand All @@ -80,12 +81,14 @@ const Search = props => {
{'search-bar-expanded': props.isSearchBarExpanded},
{'search-bar': !props.isSearchBarExpanded},
)}
onClick={loadAlgoliaJS}
onMouseOver={loadAlgoliaJS}
onFocus={toggleSearchIconClick}
onBlur={toggleSearchIconClick}
ref={searchBarRef}
/>
</div>
) : null;
);
};

export default Search;