Created
October 30, 2025 22:40
-
-
Save caseywatts/d3c9769438a435dded01c6bbd26a7216 to your computer and use it in GitHub Desktop.
Pagefind with auto-updating query parameter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const params = new Proxy(new URL(window.location), { | |
| get: (url, prop) => url.searchParams.get(prop), | |
| set: (url, prop, value) => { | |
| url.searchParams.set(prop, value); | |
| window.history.replaceState({}, "", url); | |
| }, | |
| }); | |
| window.addEventListener("DOMContentLoaded", async (event) => { | |
| // initialize the search UI | |
| const search = await new PagefindUI({ | |
| element: "#search", | |
| showSubResults: true, | |
| excerptLength: 40, | |
| }); | |
| // read in initial query from URL | |
| await search.triggerSearch(params.q); | |
| // whenever the search input is updated, update its query parameter in the URL | |
| const inputElement = document.querySelector("#search input"); | |
| inputElement.addEventListener("input", (e) => { | |
| params.q = e.target.value; | |
| }); | |
| }); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment