#390 Turbolinks
Turbolinks can make your Rails app feel faster by using JavaScript to replace the page content when clicking a link. It will be default in new Rails 4.0 applications, but here I show how to use it in Rails 3 and mention some of the gotchas.
- Download:
- source code
- mp4
- m4v
- webm
- ogv
In order to make your code more concise, combining the "ready" and "page:load" listeners into one should work too:
How does this compare to PJAX?
Have a read on my article Introducing Turbolinks for Rails 4.0 which a wrote a few weeks ago. It should answer your questions.
I too was wondering about the differences. Thanks for sharing!
Thanks, I was also asking myself the same question!
I've been trying out turbolinks and pjax several times and almost every time I disabled it after a while. It does feel faster and in fact is faster, but you'll have to come up with work arounds for some small problems every time. And you'll have this constant worry: will this still work for my IE8 users? Frankly, this approach just feels a bit icky to me, mostly because I feel less in control of what is going on.
A co-worker of mine created a gem autoexec_bat which helps you modularize your javascript files. It helps you trigger the correct javascript based on e.g. controller action or component.
It really made the transition to turbolinks easy for us as we only had to change one file :)
Thanks for this episode! Turbolinks is nice. It's like ajax loading on steroids. Probably would need lots of finessing to make it seamlessly work on a large site but I like the direction here anyway.
In most cases turbolinks is actually slower than manually loading content with ajax, as it loads the whole body, which is not necessary in many cases for many sites.
I made a gem ajaxify_rails (https://github.com/ncri/ajaxify_rails), which only ajaxifies the main content area. We use it in a large site, in fact it has been extracted from it. It has a fallback for IE, unless pjax and turbolinks. Happy to hear feedback.
This looks really great. I will test it. Its a lot less magic than turbolinks.
If you have really large site (maybe some SaaS system), then you probably like my wiselinks gem.
You may find it similar to PJAX, but it is as easy to use as Turbolinks (I mean, easy to start) and as configurable as PJAX.
+1
Even without a large site, wiselinks is the way to go.
1000 page hits and not even a second improvement with Turbolinks? For most people it seems like the extra hassle of refactoring code is not worth it. Heed Mr. Knuth's words in this instance, unless you absolutely need that extra 5%.
Look again. It went from 140 seconds to 80 seconds.
There is a gem twitter-bootstrap-turbo to make twitter bootstrap javascript plugins work on turbolink.
As of Bootstrap 2.2.1 the plugins listen to document instead of body, so modification shouldn't be necessary. You'll still need make other $(document).ready stuff happen on page:load as well though.
Still getting issues with this, dropdown's aren't triggered after page change
Can someone explain more completely how this is different from PJAX? I hear everyone say "turbolinks is such a bad idea", although they offer nothing constructive. Is PJAX not a bad idea? I mean github uses it so it can't be that bad... If PJAX is fine, could they not implement it in the same way so it isn't "so bad".
After playing a while with Turbolinks I had to disable it. Too many small problems with other JavaScript code. it also does not fit to more complex scenarios. I wonder why Rails core team decided to add Turbolinks to incoming Rails 4. For more complex site, all those additional side effects are not worth. I think, that full blown Javascript MVC framework (e.g. AngularJS) can be a better way. Not only faster (only chosen parts of the page are refreshed) but also simpler (no jQuery spaghetti hell, thanks to two-way data binding)
Designing a site with turbo links in mind is a lot easier than making turbo links work on an existing site. In fact, it's a great tool to have in the rails stack by default. If I had to guess the rails team put it there because it genuinely makes every site better, and with little cost when designing a new app
Once you start using more third party plugins, more advanced UI, own workflow and bindings, turbolinks may be a potential blocker. It can be useful, but only for simple sites, sites without too many legacy javascript code and plugins. So it is not better for every site, for sure.
Just using
$(document.body).on
makes everything work as expected (if you need to add javascript files from an AJaX query)
Thanks, this solved all issues I was having with bootstrap plugins :)
thanks a lot, this saved my life today
I just added PJAX to a (fairly big) application, and I'm wondering if I shouldn't rollback and add TurboLinks instead, since it's gonna be default on Rails 4.
Any toughts on that? Would you people still add PJAX to new projects?
I am in charge of a relatively large app, which for the better part does not use ajax loading of content, I can see turbolinks being perfect for that. As other commentators have mentioned if you have already gone down the PJAX or Ajax Content loading route it might be more hassle than its worth.
It would be interesting to see some data on how much Tubolinks speeds up a real site.
Turbolinks Test seems to run locally, where the request time is about 20ms (on your computer). But in real use it is about 0.5 to 1 sec.
It was easy (1-2 days) for me to migrate my Rails 3.2 desktop app to use PJAX via the rack-pjax gem. I picked that gem because you specify the container. I chose to just PJAX the content between my header and footer tags.
However, I have a feeling that most of the performance win comes from not reprocessing the head tag so I would expect that just fragmenting the entire body is good enuff. When I get the time, I'll migrate from PJAX to Turbolinks.
Has anyone had any experience integrating either PJAX or Turbolinks with jQuery Mobile. JQM does has it's own pushState manager and converts all links into AJAX requests. It also does DOM caching of those pages so there are several issues to consider.
While I was thinking about it, I migrated to Turbolinks. Since I use bootstrap, I needed the https://github.com/kossnocorp/jquery.turbolinks gem to deal with the fact that bootstrap binds events to the body tag (this will be fixed in future releases to use the html tag).
Also, I use Turbolinks for my forms (like search) that use GET
If, like me, you also use Ryan's awesome
nested_form
gem, then you will be very interested in Issue #145 where the compatibilities with Turbolinks are getting resolvedFor those that are interested "modularizing" their CoffeeScript for easy loading in Turbolinks and Pjax environments I'd appreciate comments on https://github.com/erichmenge/handlers-js#readme. It works out of the box with Turbolinks and is a pattern I've used with great success.
Erich, that looks pretty nice. Perhaps your pattern is a good way to DRY up code between two PushState frameworks (Turbolinks on Desktop / jQuery Mobile on Mobile)
I couldn't get the following to work:
I had to use:
This was on jQuery 2.1.4 with Firefox 17.0.1 and Chrome 24.0.1312.52-beta on Linux.
I just wrote a blog post about what to watch out for with Rails 4.0 Turbolinks: Web Dev: Gotchas With Rails 4.0 Turbolinks
Turbolinks is like an electric car...it's so smooth, you don't even know it's working. And from a UI perspective, that can be bad.
The git page in one of it's examples has this:
I'm not sure if I like this. It's like adding a fake engine noise that is louder than a regular engine.
I'm curious if anyone has any good solutions.
If you're having problems with some jQueries you can try the jQuery-Turbolinks gem: https://github.com/kossnocorp/jquery.turbolinks/
can someone please explain how can i load other apis(e.g. google map api) along with turbolink
what about content_for :head included scripts ?
Hello
thanks for the details
A perfect post anyone would want to see.