Skip to content

Commit 24f7de4

Browse files
authored
Merge pull request graphql#939 from graphql/fix_search
Fix search for gatsby site
2 parents 4fcbcbd + 071afbd commit 24f7de4

File tree

5 files changed

+60
-27
lines changed

5 files changed

+60
-27
lines changed

src/components/Footer/index.tsx

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -108,21 +108,6 @@ const Footer = ({sourcePath}: { sourcePath: string }) => {
108108
</section>
109109
</footer>
110110

111-
<script
112-
type="text/javascript"
113-
src="https://cdn.jsdelivr.net/docsearch.js/1/docsearch.min.js"
114-
></script>
115-
<script
116-
dangerouslySetInnerHTML={{
117-
__html: `
118-
docsearch({
119-
apiKey: 'd103541f3e6041148aade2e746ed4d61',
120-
indexName: 'graphql',
121-
inputSelector: '#algolia-search-input'
122-
});
123-
`,
124-
}}
125-
/>
126111
</div>
127112
)
128113
}

src/components/Header/index.tsx

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,7 @@ import { Link } from "gatsby"
33
import HeaderLinks from "../HeaderLinks"
44
import Search from "../Search"
55

6-
interface Props {
7-
noSearch?: boolean
8-
}
9-
const Header = ({ noSearch }: Props) => {
6+
const Header = () => {
107
return (
118
<header>
129
<section>
@@ -20,8 +17,8 @@ const Header = ({ noSearch }: Props) => {
2017
/>
2118
GraphQL
2219
</Link>
23-
<HeaderLinks section={"home"} />
24-
{noSearch || <Search />}
20+
<HeaderLinks/>
21+
<Search />
2522
</section>
2623
</header>
2724
)

src/components/Link/index.tsx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import React from "react"
22
import { Link } from "gatsby"
33

44
interface Props {
5-
children: React.ReactNode
5+
children?: React.ReactNode
66
href: string,
77
className?:string
88
}

src/components/Search/index.tsx

Lines changed: 54 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,61 @@
1-
import React from "react"
1+
import React, {useEffect} from "react"
2+
3+
// Added to the global runtime by the script tag further down the file.
4+
declare const docsearch: any | undefined
5+
6+
7+
8+
// Runs the new docsearch function over possible search inputs
9+
const runDocsearchIfPossible = () => {
10+
if (typeof docsearch !== 'undefined') {
11+
const searches = ["algolia-search-input", "hero-search-input"]
12+
for (const searchID of searches) {
13+
if (!document.getElementById(searchID)) continue
14+
15+
docsearch({
16+
apiKey: 'd103541f3e6041148aade2e746ed4d61',
17+
indexName: 'graphql',
18+
inputSelector: `#${searchID}`
19+
});
20+
}
21+
}
22+
}
23+
24+
const Search = ({searchID}: { searchID?: string}): JSX.Element => {
25+
const searchInputID = searchID || "algolia-search-input"
26+
27+
// This extra bit of mis-direction ensures that non-essential code runs after
28+
// the page is loaded
29+
useEffect(() => {
30+
runDocsearchIfPossible()
31+
32+
if (document.getElementById("algolia-search")) return
33+
34+
const searchScript = document.createElement('script');
35+
searchScript.id = "algolia-search"
36+
const searchCSS = document.createElement('link');
37+
38+
searchScript.src = "https://cdn.jsdelivr.net/docsearch.js/1/docsearch.min.js";
39+
searchScript.async = true;
40+
searchScript.onload = () => {
41+
if (typeof docsearch !== 'undefined') {
42+
runDocsearchIfPossible()
43+
44+
searchCSS.rel = 'stylesheet';
45+
searchCSS.href = 'https://cdn.jsdelivr.net/docsearch.js/1/docsearch.min.css'
46+
searchCSS.type = 'text/css';
47+
document.body.appendChild(searchCSS);
48+
}
49+
}
50+
51+
document.body.appendChild(searchScript);
52+
}, []);
53+
254

3-
const Search = (): JSX.Element => {
455
return (
556
<div className="algolia-search-wrapper">
657
<input
7-
id="algolia-search-input"
58+
id={searchInputID}
859
type="text"
960
placeholder="Search docs..."
1061
aria-label="Search docs"

src/pages/index.tsx

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ export default ({ pageContext }) => {
1616
return (
1717
<Layout className={"index"} title="GraphQL | A query language for your API" pageContext={pageContext}>
1818
<section className="fixedSearch">
19-
<Search />
19+
<Search searchID="hero-search-input" />
2020
</section>
2121
<Hero />
2222
<section className="lead">
@@ -39,4 +39,4 @@ export default ({ pageContext }) => {
3939
<WhosUsing />
4040
</Layout>
4141
)
42-
}
42+
}

0 commit comments

Comments
 (0)