Skip to content

Instantly share code, notes, and snippets.

@caseywatts
Created October 30, 2025 22:40
Show Gist options
  • Select an option

  • Save caseywatts/d3c9769438a435dded01c6bbd26a7216 to your computer and use it in GitHub Desktop.

Select an option

Save caseywatts/d3c9769438a435dded01c6bbd26a7216 to your computer and use it in GitHub Desktop.
Pagefind with auto-updating query parameter
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