turbolinks-compatibility icon indicating copy to clipboard operation
turbolinks-compatibility copied to clipboard

New Sharethis implementation and Turbolinks' incompatibility

Open bcunha42 opened this issue 7 years ago • 3 comments

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?

bcunha42 avatar May 19 '17 23:05 bcunha42

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);
        }
    })

amenon avatar Oct 15 '17 14:10 amenon

thanks for the code

mubbashar avatar Dec 06 '17 12:12 mubbashar

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);
        }
    })

ycrepeau avatar Feb 13 '18 21:02 ycrepeau