"Random Post" button, issues with cache

Hey everyone,

For a blog I’m working on at the moment I want to implement a Random Post button in the header. I tried using something like below to get a random post:

$page->children()->visible()->shuffle()->first()

It works fine, until I turn on Kirby’s cache. I guess this is probably expected behaviour, but now the output of the above is cached, and the user no longer sees a random page, but the same one over and over again.

Maybe I’m looking at this all wrong, but is there a way I can achieve this without disabling the cache?

Any help is greatly appreciated, and apologies upfront if this is a noob question :slight_smile:
Simon

Would disabling the cache just on that page be an option for you? That’d be a quick fix!

How about adding a parameter to the URL? What exactly does your button do?

Unfortunately not, as it’s on the site-wide header

The button is just a link to the page output by the shuffled collection. What kind of parameter do you mean? Like a random integer to break the cache?

Edit: Think similar to Wikipedia’s “Random Page” link

You could get a random post uid in the parameter and display it. I think that’s what @texnixe meant

So it is just a normal html link tag with a href attribute? Then you could just add the uid of the page as a parameter.

Thiousi was faster, but he got me right.

I don’t think the param idea is going to work as the resulting HTML of the button will be cached again, no matter if using a param or not.

You could use a route like /random and link to that in the header. The route would then select the random post and redirect to it. This way you can cache all pages and the result would still be random.

Here you can find an example route: Creating a /random url

Thanks for all the help!

I couldn’t get the parameter solutions working, unfortunately, but I’ve made some progress using the route solution provided by @lukasbestle and @texnixe.

It’s all working except for one thing: The site is 404’ing when used in conjunction with the routes in the guide here to omit the home/ folder from URLs. Any ideas why?

Thanks again :slight_smile:

You would probably have to register the /random route first so that it is checked before the other route.

That did it! Thank you!

A weird thing though, it works perfectly on my local machine, but is completely broken on the live server. Do you have any ideas why that would happen? I can’t work it out at all.

Edit: the shuffle button at the top right of the page :slight_smile:

Do you mean that the result is always https://cynicalmollusc.com/home/adventures-of-zeke/human-world?

That could be one of:

  • CloudFlare caches the result
  • Kirby caches the result
  • There is only one matching page

Not 100% sure about this one and I can’t test it because I’m not at home but you can try adding the /random Route to a cache.ignore rule in the config file.

Something like
c::set('cache.ignore', array( 'random'));

Okay, so I came back to this with a fresh mind, and discovered it was an absolutely stupid problem that I shouldn’t have had so much trouble with.

My htaccess file had cache expiry rules for various different file types, and a default of 4 hours, which was picking up HTML and PHP files.

I can’t believe I made that rookie mistake haha.

Thanks for all the help everyone :smiley:

3 Likes