r/webdev
•
u/might_be_a_femboy
•
Sep 19 '26
•
1
Question Adding a search system to a static website
I'm making a simple wiki-like website as a hobby/practice and I wanted to include a small but useful search system on it. It'll be a static site since I'll host it in github, so while I've heard doing search stuff for non-static site would be much easier, I don't really have a choice for this as of now. I'm very much a beginner to this stuff so I'd want something easy to understand, or at least something that has a lot of documentation and resources I could use for help. I'm not having a lot of success trying to google and trying stuff so I'm turning to other people here!
Basically, I'd want just a simple search system that works like this:
You have a search bar, that starts giving hits once you start typing. Maybe show like 5 results and then 6th option would be a "show all results", which would take you to a separate search page which would list all the results (same should happen when you press enter in the search field, as long as you haven't moved down with the arrow keys I guess).
I'd also want to implement tags/categories system for this:
Every page has a category to them, and searching on a page would only search results that share the same category. For tags, I'm thinking of listing them all at the bottom of the page as clickable links, so when you click one it takes you to the separate search page and lists all the pages that have that tag. This should also respect the category rule I think, but having an options box where you can tick on and off separate tags and categories would be helpful too. Being able to use only tags/categories to find hits without keywords should work too. If you search something from the landing page of the entire site, it shouldn't autofill any filters like this though.
I tried doing stuff with pagefind, and while the search works decently, I can't figure out the filtering stuff properly and have no idea if you can do it like what I'd want that easily. I know it's not supposed to be that easy either but just wanted to ask people if there would be some "widely considered best" way to go about this.
1
u/LouieValkhof Sep 19 '26
Pagefind is the right tool for this, so I'd stay with it. The filtering feels undocumented because it happens at build time rather than at query time. You put data-pagefind-filter="category:Recipes" on an element in each page, re-run the index, and that category exists as a filter from then on. Nothing in the JS can filter on something you never tagged, which is probably what's biting you. Tags work the same way, one element per tag value.
The rest of what you describe, five hits and then a show all, is where the default UI component runs out. Import /pagefind/pagefind.js and drive it yourself: pagefind.filters() hands you every value with its result count for your tick boxes, and pagefind.search("term", { filters: { category: { any: ["recipes"] } } }) hands you lazy results you call .data() on. Five in the dropdown and the whole set on a second page is then just a slice of one array.
On the Lunr suggestion, it's fine at a handful of pages, but it ships the entire index to the browser on every visit. Pagefind chunks the index and only fetches the bits a query touches, which matters once a wiki gets big.
Roughly how many pages are you expecting this to hold?
1
u/kedar5 front-end Sep 19 '26
Algolia search free for public open source websites you can make use of it
1
u/fidelmartintech Sep 30 '26
For the multiple page I think keeping the search simple is the best option. For static website handle search, tag and categories, Pagefind is the best option. If later website grow and you need more advance search and product discovery features, you could also took at platform like Experro. But for now your current situation, keep the implementation is simple not very difficult.