diff --git a/www/src/components/search-form.js b/www/src/components/search-form.js index d8efe61ad7150..9ad8865e6d137 100644 --- a/www/src/components/search-form.js +++ b/www/src/components/search-form.js @@ -273,6 +273,10 @@ class SearchForm extends Component { navigate(`${a.pathname}${a.hash}`) } init() { + if (this.algoliaInitialized) { + return + } + window.addEventListener( `autocomplete:selected`, this.autocompleteSelected, @@ -290,6 +294,7 @@ class SearchForm extends Component { keyboardShortcuts: [`s`], }, }) + this.algoliaInitialized = true } componentDidMount() { if ( @@ -306,13 +311,23 @@ class SearchForm extends Component { document.head.appendChild(link) } } + componentWillUnmount() { + window.removeEventListener( + `autocomplete:selected`, + this.autocompleteSelected, + true + ) + } loadAlgoliaJS() { - !loadedJs && + if (!loadedJs) { loadJS().then(a => { loadedJs = true window.docsearch = a.default this.init() }) + } else { + this.init() + } } render() { const { focussed } = this.state