Display search query in URL #629
-
Hi all! I would like to exploit Pagefind also from the search page URL. An explanatory example: when I visit the search page of Eleventy’s documentation, while I type the search query the URL is automatically updated. If I search “if”, the page URL becomes: Is there a way I could make Pagefind on my website do the same? |
Beta Was this translation helpful? Give feedback.
Replies: 2 comments 3 replies
-
You can use processTerm: function (term) {
history.replaceState({}, "", `?q=${encodeURIComponent(term)}`);
return term;
}, |
Beta Was this translation helpful? Give feedback.
-
@xplosionmind if you add the query string to the URL, you should also use it to run the search when arriving on the page. Here's how I did it on my search page: const params = new URLSearchParams(window.location.search);
if (params.has('q')) {
search_ui.triggerSearch(params.get('q'));
} My full code here: https://github.com/nhoizey/nicolas-hoizey.photo/blob/main/src/search/index.njk |
Beta Was this translation helpful? Give feedback.
@xplosionmind if you add the query string to the URL, you should also use it to run the search when arriving on the page.
Here's how I did it on my search page:
My full code here: https://github.com/nhoizey/nicolas-hoizey.photo/blob/main/src/search/index.njk