NTHP

Random Post Links

You may have noticed that the random post button keeps taking you to the same post. Why is that?

This is a static website. Meaning when you go to a link, you get the exact file that is on the server. This makes it a bit hard to make a link that shoots you to a random page, without a server doing some thinking.

Due to my distain for JavaScript, the random links are generated at build time using a bit of HUGO.

{{ $pages := where .Site.RegularPages "Section" "blog" -}}
{{- $randomPage := index (shuffle $pages) 0 -}}
<a href="{{ $randomPage.Permalink }}">Random Post</a>

Once the site is built and deployed, it’s just a normal link. Though, the link will change when I push a new build.