Skip to content

Commit 74136ab

Browse files
committed
fix: have docsearch.js dynamically imported
This makes it work with server rendered gatsby.
1 parent 70424aa commit 74136ab

File tree

1 file changed

+20
-8
lines changed

1 file changed

+20
-8
lines changed

site/src/components/docsearch/index.js

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import React from 'react';
2-
import docsearch from 'docsearch.js';
32
import classNames from 'classnames';
43

54
import { ReactComponent as SearchIcon } from '../svgs/magnifier.svg';
@@ -10,16 +9,27 @@ class DocSearch extends React.Component {
109
focused: false,
1110
};
1211

13-
componentDidMount() {
12+
hasSearchInited = false;
13+
14+
initSearch = () => {
15+
if (this.hasSearchInited) {
16+
return;
17+
}
1418
// Initialize Algolia search.
15-
docsearch({
16-
apiKey: '2ed553faf01ab789d57bd2882ddd2b2d',
17-
indexName: 'wpack_io',
18-
inputSelector: '#algolia-doc-search',
19+
this.hasSearchInited = true;
20+
import('docsearch.js').then(({ default: docsearch }) => {
21+
docsearch({
22+
apiKey: '2ed553faf01ab789d57bd2882ddd2b2d',
23+
indexName: 'wpack_io',
24+
inputSelector: '#algolia-doc-search',
25+
});
1926
});
20-
}
27+
};
2128

22-
handleFocus = () => this.setState({ focused: true });
29+
handleFocus = () => {
30+
this.initSearch();
31+
this.setState({ focused: true });
32+
};
2333

2434
handleBlur = () => this.setState({ focused: false });
2535

@@ -31,6 +41,8 @@ class DocSearch extends React.Component {
3141
className={classNames('wpackio-docsearch', {
3242
'wpackio-docsearch--focused': focused,
3343
})}
44+
onMouseEnter={this.initSearch}
45+
onTouchStart={this.initSearch}
3446
>
3547
<div className="control has-icons-left">
3648
<input

0 commit comments

Comments
 (0)