0

I would like to add the facebook Oembed page to my vue app. I did all the facebook process to get access to my oembed page properly. It works and it returns me an html with this structure :

<div id="fb-root"></div>
<script async="1" defer="1" crossorigin="anonymous" src="https://connect.facebook.net/fr_FR/sdk.js#xfbml=1&amp;version=v12.0" nonce="jvnA38OI"></script><div class="fb-page" data-href="urlToMyFacebookPage" data-small-header="" data-adapt-container-width="1" data-hide-cover="" data-show-facepile="1" data-show-posts="1" data-width="480" data-height="800"><blockquote cite="urlToMyFacebookPage" class="fb-xfbml-parse-ignore"><a href="urlToMyFacebookPage">My Facebook Page</a></blockquote></div>

So from this point everything seems ok. Then I want to show this html in my app but I cant find a way to make it load the actual facebook page. The best result I had was a link to the facebook page.

So here is what I've done in my app :

  • Axios requester for facebook API calls
  • Add a Facebook init.js file to load the facebook sdk before the app as the following code

init-facebook.js :

const facebookAppId = process.env.VUE_APP_FACEBOOK_APP_ID

// eslint-disable-next-line @typescript-eslint/explicit-module-boundary-types
export function initFacebookSdk() {
    return new Promise(resolve => {
        // wait for facebook sdk to initialize before starting the vue app
        window.fbAsyncInit = function () {
            FB.init({
                appId: facebookAppId,
                cookie: true,
                xfbml: true,
                version: 'v12.0'
            })

            FB.AppEvents.logPageView()
            console.log('facebook logged')
            resolve()
        };

        // load facebook sdk 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 = 'https://connect.facebook.net/en_US/sdk.js'
            fjs.parentNode.insertBefore(js, fjs)
        }(document, 'script', 'facebook-jssdk')) 
    })
}

main.ts :

initFacebookSdk().then(startApp)

function startApp() {
  createApp(App)
  .use(router)
  .mount('#app')
}

Then in the component I want to get the facebook page I called the facebook API from the requester, I do get the result in the first code block. I store the html in a reactive variable (with the vue Ref) Then I tried this :

  • Add the html variable directly in my component html -> Result : Shows the html as a string in my component
  • Add the html in a div with a v-html -> Result : Shows a link to the facebook page

I don't know what am I missing to show the page properly. Does anyone can help me with it ? Thanks !

4
  • The SDK goes through the current document once upon initialization, to find elements it needs to parse & convert into social plugins. Any such elements you add to the document later on, after the SDK was initialized, will not automatically be rendered - you need to call FB.XFBML.render to make that happen. Commented Feb 7, 2022 at 12:08
  • @CBroe ok thank you for that information I will try to look at this then ! But for now It seems that I can't use FB.something I have errors that tells me 'Cannot find name FB'... Commented Feb 7, 2022 at 12:12
  • @CBroe I made the FB.XFBML works (at least I think it is ok since it does not trigger any errors) but I don't have a render function. Instead I tried the parse function but my problem still persist... Commented Feb 7, 2022 at 12:39
  • Yes, sorry, it needs to be parse, not render. Is something like stackoverflow.com/a/61462872/1427878 not working for you? Commented Feb 7, 2022 at 13:15

0

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.