This post is part of a series of 31 Rails 4 articles being released each day in December 2012.
pushState + AJAX) is a jQuery plugin created by Chris Wanstrath, which allows you to update a specific section of a page with an AJAX request without having to do a full HTTP request. By using
Turbolinks also uses pushState and does the exact same thing as PJAX, except that it does not require a custom partial response from the server. Turbolinks will perform an HTTP Request just as the browser would, but then replaces the
<body> currently loaded in the DOM with the response from the server. It is a more automatic solution, that removes the need to manage which sections of your page will be replaced.
If a Turbolinks enabled web application is accessed from a browser that doesn't support
pushState, the web application will degrade gracefully and do a complete HTTP request. Browsers that support
pushState and all related APIs are:
- Safari 6.0+
- Chrome 5.0+
- Firefox 4.0+
Turbolinks can also be used with Rails 3 applications today. To enable it, add the
turbolinks gem to your
# Gemfile gem 'turbolinks'
All internal links within a page are automatically Turbolinks enabled. If you would like certain containers and links to not be enabled, add
data-no-turbolink to the element.
<div data-no-turbolink> ... </div>
ready event on a document will only be triggered on the initial page load. Subsequent Turbolink requests will not fire any
ready events, thus potentially not binding events your application requires.
There are four events which Turbolinks does fire:
page:fetch: Page is beginning to fetch a response from the server.
page:load: Page is being fetched from the server.
page:restore: Page is being loaded from client-side cache.
page:change: Page has has changed to fetched version.
Moving forward, you will have to bind events to both
ready(for degradation purposes) and
page:load. Or you can use jQuery Turbolinks, a jQuery plugin that is a drop-in fix for binding event issues with Turbolinks.
Opting Out Completely
If Turbolinks is not for you, you can remove it from your Rails 4 application by doing the following:
gem 'turbolinks'from your