Fun with intelligent 404 pages

[Tweet : ADN : nvALT]

Did somebody say 404?

Up until today, I’d been redirecting 404 errors on my new site to an archive version of the old site. I’m confident enough at this point that all of my previous content (that I’m concerned with) is properly redirecting and search engines are all updated with the new permalink structure. So I turned that off.

Just in case, though, I thought I’d smarten up the 404 to offer possible options based on the URL that a person comes in on. C’mon, it’s Sunday.

I decided to do it on the client side with JavaScript rather than adding databases or dynamic server-side scripts to my static site. It’s not at all a new concept, I’m just excited that I pulled it off pretty well on my static site.

It uses the search.json that I generate when my site builds. It contains post titles, summaries, tags and keywords (all part of my work in progress content exposure project). Then it…

  • dissects the request url path and sanitizes the string (removes dashes, slashes, percents and numbers)
  • runs a fuzzy string match1 with the url terms against the contents of search.json
  • sorts the list by match score, trims it to 15 and adds it to the page

This means that bad or truncated urls can still lead a person to the content that they were looking for. It also, just for fun, means that you can easily search the site using keywords with a simple url, such as brettterpstra.com/applescript or brettterpstra.com/jekyll.

I’m not posting the scripts for now, as building the search.json requires a rather involved set of custom plugins and the JavaScript isn’t much good without it. If you’re terribly curious, though, just drop me a line.

While I’m nerd-boasting about stuff, check out my blink(1) telling me that my Jekyll site is generating.

YouTube Video

Because I use a system that schedules builds for future dates, it’s cool to know when the process starts and is running in the background. It took some coding to make it start and stop blinking continuously, but I now have a notify function in my Rakefile that takes RGB color codes and a start/stop argument to blink in any color for different tasks.

If it notices that I’m at my machine when it completes a task, it takes over my audio momentarily, unmutes it as needed and announces the status using the Zarvox voice (try it: say -v Zarvox "boom, generated" or say -v Zarvox "deee ploid"). If not, it sends me a push notification on my phone. I gotta stop with this stuff.

  1. It currently only finds characters in the order they are in the search (with whatever in-between each one), but I’d eventually like to account for transposed words. That might be a little heavy for a 404 page, though.