Skip to content

Commit d147035

Browse files
committed
Fix search: remove duplicate results and fix result titles
- Replace pagefind-searchbox with pagefind-input to remove the dropdown autocomplete (results only show in the page body) - Add data-pagefind-body to main content area in Layout so Pagefind only indexes page content, not header/nav/footer - Add data-pagefind-ignore to Header to prevent the site title being used as the result title for every page - Result titles now correctly show the blog post title
1 parent ff4f3d7 commit d147035

4 files changed

Lines changed: 22 additions & 2 deletions

File tree

snapshots.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,3 +8,8 @@
88
url: /posts/dont-use-aws-ses/
99
- name: Tag page
1010
url: /tags/aws
11+
- name: Search page
12+
url: /search/
13+
- name: Search results
14+
url: /search/?q=aws
15+
wait_for_selector: "pagefind-results a"

src/components/Header/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import Background from "./header.jpg";
55
const Header = () => (
66
<div
77
className="vh-10 dt w-100 tc bg-dark-gray cover"
8+
data-pagefind-ignore
89
style={{
910
background: `url(${Background}) no-repeat center`
1011
}}

src/components/Layout/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const TemplateWrapper = ({ children }) => (
2525
<Header />
2626
<Buttons />
2727
<div className="flex justify-center">
28-
<div className="fl bg-dark-gray pa3 pl3 pl3-m pr3 pr3-m w100 pl5-ns pr5-ns w-90-m w-70-ns">
28+
<div className="fl bg-dark-gray pa3 pl3 pl3-m pr3 pr3-m w100 pl5-ns pr5-ns w-90-m w-70-ns" data-pagefind-body>
2929
{children}
3030
</div>
3131
</div>

src/pages/search.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,23 @@ const SearchPage = () => {
2020
script.onload = () => {
2121
// Insert the web components after the script loads
2222
containerRef.current.innerHTML = `
23-
<pagefind-searchbox></pagefind-searchbox>
23+
<pagefind-input></pagefind-input>
2424
<pagefind-results></pagefind-results>
2525
`;
26+
27+
// Check for a ?q= query parameter and pre-fill the search
28+
const params = new URLSearchParams(window.location.search);
29+
const query = params.get("q");
30+
if (query) {
31+
// Wait for the web component to render its internal input
32+
setTimeout(() => {
33+
const input = containerRef.current.querySelector("pagefind-input input");
34+
if (input) {
35+
input.value = query;
36+
input.dispatchEvent(new Event("input", { bubbles: true }));
37+
}
38+
}, 100);
39+
}
2640
};
2741
document.head.appendChild(script);
2842

0 commit comments

Comments
 (0)