I think shortly after I added the next and previous buttons to twoprops.net, I have wanted to add a random button as well. My desire to do so has only grown as the site has added pages, and now with over 600, I imagine it must be a daunting proposition for somebody to want to explore the site without just sticking with the very early or very recent pages.

Why not?

Well, there is a lot of philosophy that drives this site. It is a completely “static” site:: no code executes on the server to build pages in real time. All the pages exist as static HTML text files. All the webserver does is send those pages when requested. I’ve also avoided JavaScript whenever possible. The result is an efficient site that loads quickly while still (I think) looking good. When “AI” scrapers come knocking, my modest little VPS handles them with ease because there isn’t much to do except answer requests with a simple text file.

Everywhere I searched for “how to generate a random link button” says you must use JavaScript or old CGI technology to do so. I didn’t want to. I wanted to keep the site lean and pure. I also didn’t want every page load to have to include an index of pages or other cruft that would slow things down.

Then I had an epiphany.

this column gets kind of technical from here

First, the system doesn’t have to be perfect. You’re looking for one random page out of 600+. If some pages (the latest one, for example) sometimes get missed, no big deal.

I only update the site once or twice a day at most.

So why not do this:

1. Build a list of pages on the server once a day.

This could just be a text file with the name of each page listed one-to-a-line. Since that’s a fairly accurate description of my home page, it should be fairly simple to build. Simple enough, I thought, to just use bash. Here’s the code so far (runs, but not refined or tested):

#!/bin/bash
set -euo pipefail

indexfile="/home/twoprops/quartz/content/index.md"
linksfile="/home/twoprops/randlinks"

while IFS= read -r line; do
  echo "$line" | sed -n 's/^## \[\[\([^|]*\).*\]\]/\1/p' >>$linksfile.tmp
  echo "$line" | sed -n 's/^## .*(\(.*\))/\2/p' >>$linksfile.tmp
done < "$indexfile"
mv -f $linksfile.tmp $linksfile.txt

That gives us randlinks.txt which has the file name of each column on twoprops.net listed one-to-a-line. I can set up a cron job to re-create the file once a day.

2. Create a script to run frequently that creates a file random.html that consists of a redirect to a random page.

Then I can write a little more bash that will run every 20 seconds or so and will create a file in the web server’s public directory called, cleverly, random.html. It will just consist of standard HTML headers with a redirect directive, but the target of the redirect will be selected randomly from the randlinks.txt file every time random.html is re-created.

Six hundred columns. That’s a lot of editing. So I’ll probably write a quick bash script to make that change as well, but it will only have to run once.

There are some rough edges in this idea. If someone manages to hit the random button twice within twenty seconds, they’ll get the same “random” page a second time. There are probably edge cases. If the site grows to hundreds of thousands of pages (ha!) there might be performance issues on my low-end server. But a random button that randomly (but rarely) misbehaves is kind of fitting, don’t you think?

Watch for the random link coming soon!

—2p

← previous|random|next →