How to get a random post link?

I’d like to have a link to a “Random Post” on the navigation.

Is there any solution to this with a bit of JS on the client side?

I can’t find anything either on the forums here or the Hugo functions.

Thank you!

Ok, thanks. Having another look to the shuffle function then!

I just read now that you are looking for a function client side for that. I would have a look into how search-engine-indexes are made in Hugo (look for algolia.json on the forum) and then just parse that json file somehow and select one item. But I very much prefer static stuff. Search engines “see” it and you won’t run into any troubles with browser clients.

I prefer static stuff too, but I can’t seem to find a static solution to this. I thought making it a client side function as a last resort. I have a done a similar thing in PHP for a static HTML site in the past.

Algolia I have looked it a bit already, but the thing is I wanted to not use any 3rd party services.

I just said algolia so you can see, how you can create a json file with all your items.

{{/* Generates a valid Algolia search index */}}
{{- $.Scratch.Add "index" slice -}}
{{- $section := $.Site.GetPage "section" .Section }}
{{- range .Site.AllPages -}}
  {{- if or (and (.IsDescendant $section) (and (not .Draft) (not .Params.private))) $section.IsHome -}}
    {{- $.Scratch.Add "index" (dict "objectID" (sha1 .Permalink) "date" .Date.UTC.Unix "description" .Description "expirydate" .ExpiryDate.UTC.Unix "fuzzywordcount" .FuzzyWordCount "keywords" .Keywords "kind" .Kind "lang" .Lang "lastmod" .Lastmod.UTC.Unix "permalink" .Permalink "publishdate" .PublishDate "readingtime" .ReadingTime "relpermalink" .RelPermalink "html" .Params.Description "title" .Title "type" .Type "url" .Permalink "weight" .Weight "wordcount" .WordCount "section" .Section "tags" .Params.Tags "categories" .Params.Categories "author" .Params.authors "content" .Params.Description "excerpt_html" .Params.Description "excerpt_text" .Params.Description "summary" .Summary)}}
  {{- end -}}
{{- end -}}
{{- $.Scratch.Get "index" | jsonify -}}

that creates a json file with all posts as json. what you do with that is your thing :wink: No algolia needed.

1 Like

Ahaa… sounds like a possible solution then.

Ok, I’m going to look into this, sounds like it’s doable with a bit of vanilla JS to get a random object from the JSON file.

Cheers! :nerd_face:

1 Like

Check the following samples:

The first one defines a custom post format (in lines 60 to 66) and the second one does the index-creation. Line 56 in the config.toml file at the top adds algolia to the home array, no need to add it to any other section.

If you run hugo look for a file public/algolia.json and it’s contents will be what you want to use from the frontend.

1 Like

Thank you so much, this is definitely great! Will work on this tonight and see if I get it running and it’s fast enough.

It’s for a pretty big site, 4552 posts :slight_smile: Takes 30 sec to build on Hugo!

dang… my 2800 post site takes 2 minutes :frowning: now I am triggered again :slight_smile:

Oh wow. What CPU? I’m using an i7 MacBook Pro. Also front-matter on each post is quite populated with about 20 fields. I haven’t tried on another machine.

seems to be a pagination issue… but we are veering off-topic :wink: get your random post fixed :stuck_out_tongue:

1 Like

Right. It’s random off-topic. Thanks for the help!

My self-contained solution from several years ago. It scaled to 18,000+ pages so well that I never bothered trying to do anything more sensible.


1 Like

Oh great, thanks, I see it’s for a recipes website.

I am also working on one, will check it out!