turbolinks-compatibility
turbolinks-compatibility copied to clipboard
New Sharethis implementation and Turbolinks' incompatibility
The official implementation of Sharethis, described in URL below, is deprecated. http://reed.github.io/turbolinks-compatibility/sharethis.html
The new one is something like this:
<script type="text/javascript" src="//platform-api.sharethis.com/js/sharethis.js#property=[YOUR-PROPERTY-CODE]&product=sticky-share-buttons"></script>
How can I set this new one compatible with turbolinks?
The new sharethis
implementation does not have an official JS API. I was able to reinitialize the share buttons on new pages, by calling the library's init
method after turbolinks
loads the page:
document.addEventListener("turbolinks:load", function () {
// Note: This is undocumented API for the sharethis widget. Reload the widget when we
// visit a new page after a turbolinks load event.
if (__sharethis__ && __sharethis__.config) {
__sharethis__.init(__sharethis__.config);
}
})
thanks for the code
Well, the butons show up and that great BUT...
The current url is not properly updated. If I click Twitter (or any other) button, the Twitter page will open but the embeded URL is the one stored when the code has loaded (usually the home page) not the current page location.
UPDATE:
Just update the sharethis.href BEFORE calling sharethis.init
So:
document.addEventListener("turbolinks:load", function () {
// Note: This is undocumented API for the sharethis widget. Reload the widget when we
// visit a new page after a turbolinks load event.
if (__sharethis__ && __sharethis__.config) {
__sharethis__.href = document.location.href
__sharethis__.init(__sharethis__.config);
}
})