Why we chose Turbolinks instead of building an SPA
Tags: javascript, webdev, productivity, elixir
Published: 2018-10-12 15:16:00

---
title: Why we chose Turbolinks instead of building an SPA
published: true
description: We actively chose Turbolinks, installed it, and integrated it in to our application. I think that makes us pretty unique.
tags: javascript, webdev, productivity, elixir
---

> Note: _I [originally wrote this in 2016](https://changelog.com/posts/why-we-chose-turbolinks), but I re-read it today and it's still super relevant. We're still using Turbolinks with no plans of switching. Enjoy!_

[Changelog.com](https://github.com/thechangelog/changelog.com) _is not_ a Rails app, but _it is_ a [Turbolinks](https://github.com/turbolinks/turbolinks) app. Think about that for a moment. 

That means we aren't using Turbolinks because it's [Omakase](https://www.youtube.com/watch?v=E99FnoYqoII). We aren't using Turbolinks because we forgot to delete it from our Gemfile. **We actively chose Turbolinks**, installed it, and integrated it in to our application. I think that makes us pretty unique. 

The number one question we've received since [open sourcing the site](https://changelog.com/posts/changelog-is-open-source) is, **"why Turbolinks?"**<a href="#fn1" rel="footnote"><sup id="fnref1">1</sup></a>

Here's why we chose it (and maybe you should too).

### It comes down to one word: ~~Courage~~ Pragmatism

It's easy to get swept up by the New Hotness. I'm even more prone to this than most, since we [talk to creators](https://changelog.com/podcast) of the New Hotness _all about their New Hotness_ on a weekly basis. But there's a big difference between a) keeping up with open source,  and b) hitching your cart to every horse that struts by with shiny new shoes.

I find I'm most effective as a developer when I'm informed, yet cautious of trends. One trend I've long been skeptical of is the _De Facto Single Page App_. Don't get me wrong, SPAs are a legit (and often best) architecture for many apps<a href="#fn2" rel="footnote"><sup id="fnref2">2</sup></a>. However, when an application is built as an SPA when it doesn't need to be, <mark>bad things happen to good people</mark>.

In my experience, we're too often wooed in to asking ourselves "**_why shouldn't_** this application be an SPA?" instead of asking ourselves "**_why should_** this application be an SPA?" The application's needs should drive the architecture, not the developer's desires.

The tricky thing I was faced with when asking myself, "why should the new changelog.com be an SPA?" is that we have one (important) feature that just screams, "SPA!" 

A persistent audio player that stays in the footer and continues to play as you navigate pages.

<img src="https://changelog-assets.s3.amazonaws.com/changelog-player.png" alt="persistent audio player action shot" width="876" height="75" />

**But that's it**. There is <em>no other feature or need</em> that we have imagined that would necessitate an SPA. Did I really want to bifurcate the codebase, increase our JS payload, and &lt; gasp&gt;choose a front-end framework&lt;/gasp&gt; all so we could have a persistent audio player?

The technologist inside of me absolutely did want to. The pragmatist knew there was a simpler way.


### Things have changed in Turbolinks land

When Turbolinks [first shipped with Rails 4.0](https://geekmonkey.org/2012/09/introducing-turbolinks-for-rails-4-0/) back in 2012, it caused a lot of headaches for developers around the world. 

Some people thought it was a bad idea altogether, but that didn't resonate with me because GitHub was using [pjax](https://geekmonkey.org/2012/09/introducing-turbolinks-for-rails-4-0/) (Turbolinks' inspiration) with much success.

Other people gave it a chance until they ran in to edge cases, frustrating bugs, and a more complex mental model than advertised. I fell in to this camp.

In other words, <mark>Turbolinks didn't live up to its promise</mark>, so most folks wrote it off and moved on with their lives (some deriding any mention of its name). But the team behind the library continued to work at it, improving things along the way. <mark>It's four years later now, and Turbolinks has changed quite a bit</mark>. Here's a quick list of features and changes to get you up to speed:

* It underwent a complete rewrite for Turbolinks 5 (see [Turbolinks Classic](https://github.com/turbolinks/turbolinks-classic))
* It doesn't require server-side request detection or alternate rendering
* It doesn't depend on jQuery or any other library
* It includes a CSS-based loading progress bar
* It can reload when assets change
* It can persist elements across page loads
* You can install it with [npm/yarn](https://www.npmjs.com/package/turbolinks) and load it with webpack

Those last two bullet points provided exactly what we needed for our persistent player. If you haven't looked at Turbolinks since version 5 was released, I recommend you do.

### Using it with Phoenix took less time than reading its README

I falsely presumed that using Turbolinks outside of Rails would be tough. Here's what it took:

1. Add Turbolinks to a dependency in [our package.json](https://github.com/thechangelog/changelog.com/blob/master/package.json#L11)
2. Import Turbolinks at [the top of app.js](https://github.com/thechangelog/changelog.com/blob/master/web/static/app/app.js#L1)
3. Call `Turbolinks.start();` at [the bottom of app.js](https://github.com/thechangelog/changelog.com/blob/master/web/static/app/app.js#L101)
4. Add `data-turbolinks-permanent` to [the player div](https://github.com/thechangelog/changelog.com/blob/master/web/templates/layout/_player.html.eex#L1)

With that, I was done and it Just Worked. That being said, Turbolinks isn't completely _gotcha_ free. There are a few things to keep in mind when writing your application's JavaScript. Make sure to read and understand the [Building Your Turbolinks Application](https://github.com/turbolinks/turbolinks#building-your-turbolinks-application) section of the README and you should be just fine.

### As a wise senior developer once said: It Depends

I know, I know. Balanced reasoning about which tools and practices we should use doesn't make for popular writing, but it does make for good systems. I'm not here to hand wave and proclaim that you should ditch your current thing and use Turbolinks.

There are many reasons why our application is different than yours, some of which makes us a good fit for this solution. I'll list a few so you don't have to email us in anger:

* Ultimately, we're a content-first site. Very few content sites require rich enough interactions to necessitate an SPA.
* We don't have much JavaScript and very few (public facing) form submissions, so we haven't hit some of the more complex scenarios where Turbolinks may fall down.
* As our needs grow, we may come to a point where we need client-side routing. I doubt that highly, but it's a possibility.

In the end, we accomplished our goal with minimal engineering effort and launched our site to much acclaim. People love our persistent player and I love Turbolinks for helping us pull it off with such aplomb. <mark>If you've written Turbolinks off, now might be a good time to give it a second chance</mark>.

<hr/>

<ol class="footnotes">
<li id="fn1">
<p>The #2 question is, &quot;How is it so fast?&quot;&nbsp; Turbolinks helps with that, but it's mostly Elixir's fault.<a href="#fnref1" rel="footnote">&#8617;</a></p>
</li>
<li id="fn2">
<p>My first SPA was in 2010 when <a href="http://jerodsanto.net/2010/12/the-tech-behind-the-new-grooveshark/">I helped Grooveshark</a> (RIP) switch from Flash to a web app.<a href="#fnref2" rel="footnote">&#8617;</a></p></li>
</ol>