disable turbolinks

Rails 5: How to remove Turbolinks 5

Hello World. Want to disable Turbolinks? While it does great things, not all Rails apps need it. Do you have weird bugs with bugs with Javascript? It might be because of it. So, here is how to remove Turbolinks. 

Why disable Turbolinks?

If you don’t need Turbolinks in your Rails app, you’ll want to avoid the bugs with Javascript, jQuery and any Javascript libraries due to Turbolinks not refreshing the page. Even trying to get Google Analytics and Turbolinks to work together has no simple solution. Even checking if a checkbox is checked or not might break, which could cause annoying problems.

Why ? Because when you click on a link, Turbolinks will send an AJAX request for the content and replace the body with it. And Javascript never gets to load properly.

A gem like jquery-turbolinks used to solve most of the issues. But months after the release of Rails 5 / Turbolinks 5, it’s still not ready to work with it so well.

So, you want to learn how to remove Turbolinks? Let’s go. We will first see how to do it on a specific page, and then on the whole Rails app.

Disable Turbolinks on a specific page

The tag for disabling Turbolinks has changed in Turbolinks 5, let’s see how it works now.

As per the documentation, you can disable it on a page by including data-turbolinks="false" in a div on the page. All the links will be disabled. But you can re-enable Turbolinks on a link by setting data-turbolinks to true. Your links must be within that div:

<div data-turbolinks="false">
  <a href="/">Turbolinks disabled</a>
  <a href="/" data-turbolinks="true">Turbolinks enabled</a>
  <a href="/">Disabled again</a>
</div>

You can also disable it directly on the link, without the need for a div: <a href="/" data-turbolinks="false">Disabled</a>

How to disable Turbolinks entirely in 3 steps

  1. In your Gemfile, remove gem 'turbolinks'
  2. Find app/assets/javascripts/application.js and remove the line //= require turbolinks
  3. In app/views/layouts/application.html.erb, you’ll see these two lines:
<%= stylesheet_link_tag 'application', media: 'all', 'data-turbolinks-track': 'reload' %>
<%= javascript_include_tag 'application', 'data-turbolinks-track': 'reload' %>

You need to remove, on each line: media: 'all', 'data-turbolinks-track': 'reload' and 'data-turbolinks-track': 'reload'.

It’s similar on Rails 4: 'data-turbolinks-track' => true.

Don’t forget to run bundle and restart your server.

If you were using the gem 'jquery-turbolinks' before upgrading to Rails 5, delete the corresponding line in your Gemfile and //= require jquery.turbolinks in application.js.

How to skip Turbolinks for a new app

Rails will install Turbolinks by default when you run Rails new myapp, because in most cases it won’t be a problem. But you can easily opt out from Turbolinks by using the --skip-turbolinks flag.
That could avoid you some hard to debug issues. Especially when you are developing your app and don’t need that kind of optimization yet.

Rails 5: How to remove Turbolinks 5
5 (100%) 4 votes

1 reply

Trackbacks & Pingbacks

  1. […] not compatible with Turbolinks 5 and it won’t be easy to make it so. See my article on how to remove Turbolinks. A more convenient solution might be to make use of the great jQuery Validation plugin in our Rails […]

Leave a Reply

Want to join the discussion?
Feel free to contribute!

Leave a Reply

Your email address will not be published. Required fields are marked *