In my nuxt.js ("nuxt": "^2.15.7") web app I am using Facebook share button as described in this full code example.
When application loads for the first time, then facebook share button is rendered correctly. Unfortunately, when I navigate to another route and/or navigate back, then facebook share button disappears. Why this happens and how to fix it?
I have tried to implement following solutions including offered by the @Cbroe and @kissu:
- Facebook share button dissapear after updatePanel
- How to add a 3rd party script code into Nuxt
- Facebook social plug-in not showing up when added dynamically
Unfortunately, above offered solutions doesn't solved my problem.
What is interesting, that Vue developer tools in Chrome browser indicates, that component <FbShareButton>
is present, but it doesn't show up on the page.
There is my initial code:
I have created nuxt plugin loadFacebookSdk.js to load Facebook SDK into the app, there is the code:
Vue.prototype.$loadFacebookSDK = async (d, s, id) => {
var js = d.getElementsByTagName(s)[0]
var fjs = d.getElementsByTagName(s)[0]
if (d.getElementById(id)) {
return
}
js = d.createElement(s)
js.id = id
js.src = "https://connect.facebook.net/en_US/sdk.js#xfbml=1&version=v3.0"
fjs.parentNode.insertBefore(js, fjs)
}
Then registered above plugin in the nuxt.config.js as following:
plugins: [
{ src: "~/plugins/loadFacebookSdk.js", mode: 'client' }
]
And finally created FbShareButton.vue component to load facebook SDK and render facebook share button:
<template>
<div>
<div id="fb-root"></div>
<div class="fb-share-button"
data-href="https://developers.facebook.com/docs/plugins/"
data-layout="button"
data-size="small"
>
</div>
</div>
</template>
<script>
export default {
async created () {
if (process.client) {
await this.$loadFacebookSDK(document, 'script', 'facebook-jssdk')
}
}
}
</script>