What’s Next? Search Me
The long and the short of it: the blog finally has search functionality again, something it lost with the move to Eleventy back in March. This is not something I was sure would ever happen.
I’d avoided the consistently recommended Pagefind because I didn’t like that it used a separate page for search and unrolled the results into the page rather than what I was used to, which was a search form you could place in the sidebar that loaded a search results page.
Turns out that’s just the default Pagefind behavior out of the box. By utilizing its API, you mostly can do whatever you want.
Thanks to Matti for getting this started off something I’d said in the #software channel on the omg.lol Discord, complete with animated gif showing search behaving exactly as described above, which I used as the basis for figuring this out. I started tackling it yesterday in the midst of all the drama and by bedtime all I had left was styling the search box and preventing a quick flash of the blog’s front page whenever you landed on the search results page.
That last was an unfortunate artifact of doing this the way I wanted, where akin to both WordPress and weblog.lol search results appear at something like /?search=terms
rather than on a separate page like /search
. It’s something of an issuing cousin to the infamous Flash Of Unstyled Content.
In the end, this was solved by a bit on CSS which hides the usual content that appears at my site root of /
by default.
#site-content {
display: none;
}
Then a script in <head>
unhides that content on any page whose address in the location bar doesn’t contain search=
.
<script>
(function() {
if (!window.location.search.includes("search=")) {
var style = document.createElement("style");
style.innerHTML = "#site-content { display: block !important; }";
document.head.appendChild(style);
}
})();
</script>
This is pretty super-ridiculous, I find, but nonetheless it works and rids me of something that would have driven my OCD pretty solidly batshit every time I needed to search for something.
Unsurprisingly, right when everything was put together exactly as I wanted it, a major problem occurred: Pagefind did not appear to actually be indexing, or perhaps simply finding in the indexed content, everything it should be. I discovered this while running some tests, and found that a search for the word capacity was finding only three posts when in reality there were over a hundred posts using the word.
To make it all the weirder still, if you limited your search to capac, a non-word no one would ever type into the search box, Pagefind found all the relevant posts containing the word capacity. The problem appears to be something in the way Pagefind does what’s apparently called “stemming”—not to be confused with stimming, the self-regulatory behavior in which I had to engage as these weird problems continued to crop up.
It took all day, but eventually I found my way to a post from Bob Monsour—who coincidentally was in my mentions on Mastodon about other Pagefind stuff—and lo and behold, this solves the problem.
As is usual with posts about code requirements in making this blog function the way it’s supposed to: yes, I consulted the occasional LLM. They are copyright and climate monstrosities, but with the blog such an enormous part of my self-regulatory regimen (regime?), it needs to work the way I need it to work. It’s rank hypocrisy to be sure, but I’ll live with it for as long as I need this blog to exist to help keep my nervous system at even just a somewhat evenish keel.
At any rate, the point is that search is back, and functioning the way I want and need it to, and since search links in existing posts already use the /?search=
format, I don’t even need to do any clean up. Everything now should just work.
Now that I’ve said multiple times that it’s working, I fully expect now that it’s gone live for something to fail, possibly in some spectacular fashion.