Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 32766f4

Browse files
committed
Add docsearch callback again when the component is loaded to mitigate race condition
1 parent 44a5e29 commit 32766f4

File tree

1 file changed

+42
-2
lines changed

1 file changed

+42
-2
lines changed

src/components/search/search.js

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,48 @@ import "./search.scss";
44
import "./algolia.css";
55

66
class Search extends React.Component {
7+
constructor(props) {
8+
super(props);
9+
10+
this.state = {
11+
enabled: true
12+
};
13+
}
14+
componentDidMount() {
15+
if (window.docsearch) {
16+
window.docsearch({
17+
// the following information is copied from the gatsby-config.js file
18+
// if you change anything below, make sure to also change it in that
19+
// file. We're duplicating this code to avoid a race condition in the
20+
// MutationObserver logic, where the docsearch script (npm package)
21+
// was loaded *before* this component was loaded.
22+
apiKey: "acfb7def12803db2cd4ac0539b2b571a",
23+
indexName: "hackerone",
24+
inputSelector: "#algolia-doc-search",
25+
transformData: suggestions => {
26+
// ideally, we'd have an `id` or `name` attribute for all headers, so that DocSearch
27+
// properly indexes the anchors. Since we don't have that yet, we've went with this
28+
// temporary workaround to remove the ___gatsby anchor from the URL. This code can be
29+
// removed when the appropriate attributes are added. Ref T19586.
30+
return suggestions.map(suggestion => {
31+
delete suggestion.anchor;
32+
33+
suggestion.url = suggestion.url.replace(/#gatsby-focus-wrapper$/, "");
34+
35+
return suggestion;
36+
});
37+
}
38+
});
39+
} else {
40+
console.warn("Search has failed to load and now is being disabled");
41+
this.setState({ enabled: false });
42+
}
43+
}
44+
745
render() {
8-
return (
46+
const { enabled } = this.state;
47+
48+
return enabled ? (
949
<form className="search">
1050
<input
1151
className="search__input"
@@ -15,7 +55,7 @@ class Search extends React.Component {
1555
aria-label="Search docs"
1656
/>
1757
</form>
18-
);
58+
) : null;
1959
}
2060
}
2161

0 commit comments

Comments
 (0)