I’m afraid I have to start this with a whole backstory, as the journey here is the point, not so much the theme.
A fella wrote to me a while back outlining a situation he was in. His company has a bunch of WordPress sites for public radio, many of which are essentially homes for podcasts. There is one specific bit of functionality he thought would be ideal for them all: to have a “pinned” audio player. Like you could play a podcast, then continue navigating around the site without that podcast stopping.
This is somewhat tricky to pull off in WordPress, because WordPress does full page reloads like any other regular website not doing anything special with link handling or history manipulation. When a page reloads, any audio on the page stops playing. That’s just how the web works.
So how would you pull it off on a WordPress site? Well, you could make it a headless WordPress site and rebuild the entire front-end as a Single Page App. Sounds fun to me, but I’d be hesitant to make that call just for this one thing.
What else could you do? You could find a way to make the page never reload. I remember doing this on a little static site 10 years ago, but that wasn’t a full blown WordPress site and I didn’t even bother updating the URL back then.
What if you did this…
I’ll do this in jQuery quick for ya:
That’s not far off from being literally functional. You’d wanna watch for a
popstate event to deal with the back button, but that’s only a few more lines.
In this hypothetical world, you’d lay out the site like:
So all that
<main> content gets swapped out, the URL changes, but your
Turns out there is more to think about here though. Are any inline scripts on the content going to run? What about updating the
<title> too? There are enough edge concerns you probably will get annoyed dealing with it.
data-turbo-permanent attribute on an HTML element, it will persist it across that reload. So I did that for the audio player here.
Here’s the thing though.
I just don’t have time to finish this project properly. It was fun to have a play, but my interest in it has kinda petered out. So I’ll leave it alone for now:
It almost works, minus one glaring bug that the audio stops playing on the first navigation, then works after that. I’m sure it’s fixable, but I just don’t have much skin in this game. I figure I’ll just bow out and leave this code around for someone to pick up if it’s useful for them.
Another thing at play here is that Turbo is from Basecamp, and Basecamp has rather imploded recently making it not feel great to be using their software. Exacerbated by the fact that Sam Stephenson wrote 75% of Turbo and has said he won’t be touching it (or other related projects) unless the software is moved to its own foundation. Turbo was already in a shaky place since it seemed buggy compared to Turbolinks, and now is on very gnarly ground.
This content was originally published here.