I'm trying to implement infinite scroll. But with Turbolink gem I have to refresh the page or it doesn't work.
Which are negative side disabling turbolink? There are any reduction of performance?
I'm trying to implement infinite scroll. But with Turbolink gem I have to refresh the page or it doesn't work.
Which are negative side disabling turbolink? There are any reduction of performance?
Removing turbolinks
has advantages and disadvantages.
One advantage is that it makes page loads faster for modern browsers. Instead of letting the browser recompile the JavaScript and CSS between each page change, it keeps the current page instance alive and replaces only the body (or parts of) and the title in the head.
However, there are many things that can go wrong when you're working with turbolinks
. The more Javascript you add to your app, the more potential there is for conflict and bugs. The first thing I do (and many other Rails developers that I know) when setting up a new Rails app is:
gem 'turbolinks'
from your Gemfile
turbolinks
from app/assets/javascripts/applicaiton.js
"data-no-turbolink"
to the <body>
tag in app/views/application.html.erb
If you want to go down the route of conditionally disabling turbolinks
on a page-by-page basis you can check out this post: Rails 4: disable Turbolinks in a specific page
From rails guide
Turbolinks attaches a click handler to all on the page. If your browser supports PushState, Turbolinks will make an Ajax request for the page, parse the response, and replace the entire body of the page with the body of the response. It will then use PushState to change the URL to the correct one, preserving refresh semantics and giving you pretty URLs.
So what will happen without the turbolink is that you will be loading the whole page everytime including the head of the document which will again load the resources such as the js and the stylesheets.
The fact that you might not be handling the Turbolinks ready event correctly in your javascript may be driving this question. In my opinion, if you can use Turbolinks you should as there are significant speed improvements as you are not loading and parsing the whole page again, just the body over AJAX.
I am not sure which version of Rails you are using, but assuming you are using the most recent Rails 5.1 (that includes Turbolinks 5) you need to make sure that you are wrapping your custom js with the following:
$( document ).on('turbolinks:load', function() {
// your custom js code for infinite scroll
});
Hope this helps.