If you’ve ever tried to use Facebook social plugins‘ latest version in your website, you might have noticed how smooth and streamlined is now the embedding process.
Select a button type, include the SDK, copy-paste a snippet of code and… you’re done! You can also leave the data-href attribute blank, so the plugin will like/share the current page. Awesome!
At this point you might ask yourself, “Well, will this nice little button work with my super cool AJAX-powered app?”. The answer is not really.
The problem
Facebook social plugins won’t work properly when your application routing is based on Javascript’s history.pushState() API – this includes a large number of Rails applications that rely on Turbolinks for their navigation.
This is because the page metadata is parsed by Facebook SDK only when a full page load is triggered. So, if I navigate from example.com/page-one to example.com/page-two and try to click on my Facebook share button, it will prompt a share for /page-one.
The solution
How to solve this? It’s actually quite easy. A small snippet (here in CoffeeScript, but easily convertible in plain JavaScript) that will update our button and retrigger a parse will be enough:
updateFbButtonHref = (el) ->
$fbButton = $(el).find('.fb-share-button')
$fbButton.attr('data-href', window.location.href)
FB.XFBML.parse()
Now, we can bind this function to the “page:load” event (in the case of a Turbolinks application), or generally after every pushState():
$(document).on 'page:load', ->
updateFbButtonHref('.my-fb-button-container')
And we’re cool! Our button will now share the correct page.