page:before-change
— a Turbolinks-enabled link has been clicked
page:fetch
— starting to fetch a new target page
page:receive
— the page has been fetched from the server, but not yet parsed
page:change
— the page has been parsed and changed to the new version
page:update
— is triggered whenever page:change is PLUS on jQuery's ajaxSucess
page:load
— is fired at the end of the loading process.
This requires us to hunt down initialisation code in obfuscated, minified scripts in order to hook them into Turbolinks' page:load event.
Even doing that isn't enough.
The Turbolinks Compatibility project provides some examples of popular third party scripts made compatible with Turbolinks
Official code
<div id="fb-root"></div>
<script>
(function(d, s, id){
var js, fjs = d.getElementsByTagName(s)[0];
if (d.getElementById(id)) return;
js = d.createElement(s); js.id = id;
js.src = "//connect.facebook.net/en_US/all.js#xfbml=1";
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
</script>
Turbolinks compatible code (Coffeescript)
fb_root = null
fb_events_bound = false
$ ->
loadFacebookSDK()
bindFacebookEvents() unless fb_events_bound
bindFacebookEvents = ->
$(document)
.on('page:fetch', saveFacebookRoot)
.on('page:change', restoreFacebookRoot)
.on('page:load', ->
FB?.XFBML.parse()
)
fb_events_bound = true
Continued
saveFacebookRoot = ->
fb_root = $('#fb-root').detach()
restoreFacebookRoot = ->
if $('#fb-root').length > 0
$('#fb-root').replaceWith fb_root
else
$('body').append fb_root
loadFacebookSDK = ->
window.fbAsyncInit = initializeFacebookSDK
$.getScript("//connect.facebook.net/en_US/all.js#xfbml=1")
Continued
initializeFacebookSDK = ->
FB.init
appId : 'YOUR_APP_ID'
channelUrl: '//WWW.YOUR_DOMAIN.COM/channel.html'
status : true
cookie : true
xfbml : true
Using the example on Turbolinks Compatibility for the ShareThis social media library, Rabid found that URLs were not being detected by the library correctly. ShareThis always thought it was on the page last hard refreshed by the client.
Good news is, it's easy to get rid of. Remove the references to it from your Gemfile and application.js and you're home free.