JQuery plugin initialization on browser back button for Turbolinks Rails 5 JQuery plugin initialization on browser back button for Turbolinks Rails 5 ruby-on-rails ruby-on-rails

JQuery plugin initialization on browser back button for Turbolinks Rails 5


So the best answer I've come up with for dealing with 'Restoration' visits (as I have since learned browser back and forward button visits are called in the world of turbolinks) that involve jquery plugins that don't have an 'undo' method is to simply opt the page out of cache-ing. I implemented this by throwing:

<% if content_for?(:head) %>  <%= yield(:head) %><% end %>

in the <head> section of my application.html.erb file and then at the top of the page I don't want turbolinks to include in it's cache, I put:

<% content_for :head do %>  <meta name="turbolinks-cache-control" content="no-cache"><% end %>

That way turbolinks fetches the page from the network and not the cache as would be the normal behavior for a 'restoration' visit. Just had to read the documentation very carefully and figure out how to implement it in rails which wasn't too bad.