As a thought experiment, how would the JavaScript ecosystem replicate this redirect to a random post feature?
https://chriscoyier.net/2023/11/29/a-random-route-on-a-wordpress-site/
And just for fun, let’s brainstorm things that *aren’t* single-host features like Netlify redirects.
19 replies
-
-
@zachleat Is a host-specific SSR adapter for X framework cheating? If so all I’ve got for ideas are somewhat-platform-specific edge workers or an Express server that sits next to the app to make queries and respond like PHP would.
(Edited to fix spelling.)
@mattrambles ha! yeahhhh… adapters are not quite the same portability story
@zachleat a client-side script that grabs a random page from a pagefind index?
-
@khalidabuhakmeh @noleli yeah, I really like that added twist too!
@khalidabuhakmeh @zachleat oh man, you’re totally right! How could I have forgotten‽
@noleli @khalidabuhakmeh my brain also has a robots.txt
-
@zachleat Collect all the URLs on the site in a big Javascript array, plop that on the page, then pick one randomly and window.open() it? It would cause two full page loads in a row, but you could put a fun spinner on the “random" page to make that feel less bad.
@zachleat Thinking about it, a client-side solution would also be a reasonable choice on a Wordpress site? Lots of hosts cache all queries for performance reasons, in which case `’orderby' => 'rand’` would go to the same page repeatedly until the cache gets purged. If you disabled caching specifically for /random it could still slow down the site overall since every visit to the page would result in an expensive database query.
@zachleat My first thought was to generate an array of every post in the site at build time, and insert it into a /random/index.html page that runs some JS to pick a random value to redirect to. It’s not pretty, but it should run pretty much everywhere. You could spice it up by remembering posts the visitor has read and filtering those from the array.
@zachleat Another option could be to configure your nginx/apache/... server to handle the /random route (if you control that). Probably somewhat like: https://stackoverflow.com/a/62162839