FB init function gives wrong version error

后端 未结 20 1378
故里飘歌
故里飘歌 2020-12-08 18:18

I\'m using the Facebook JS sdk, and I have created a new App today. Everything is configured properly. Using init function like:

window.fbAsyncInit = functio         


        
20条回答
  •  悲&欢浪女
    2020-12-08 19:02

    I had the same problem, and I think I found the root cause!

    Root cause

    In my case, we were injecting FB SDK dynamically to our customer's website. However, some of our customers were already added FB SDK via other plugins. Those plugins have different app id and version.

    So depending on latency some plugins call init before/after ours

    Solution

    If you're the owner of the site where you're injecting the SDK, make sure no other plugins are injecting FB SDK and calling init different version and app id

    If you don't own the site, then at least try to inject the SDK before anyone else and prefer not async

    I also reported the same to FB. They told not to call init separately, pass init params directly in the rule. I've attached the code that I use:

    if (!document.getElementById("fb-root")) {
          // create div required for fb
          const fbDiv = document.createElement("div");
          fbDiv.id = "fb-root";
          document.body.appendChild(fbDiv);
          // Run any script after sdk is loaded
          window.fbAsyncInit = () => {
            //
          };
          // inject sdk.js
          (function(d, script) {
            script = d.createElement("script");
            script.type = "text/javascript";
            script.async = true;
            script.src =
              "https://connect.facebook.net/en_GB/sdk.js#xfbml=1&version=v3.2&appId=" +
              process.env.REACT_APP_FB_APP_ID +
              "&autoLogAppEvents=1";
            d.getElementsByTagName("head")[0].appendChild(script);
          })(document);
        }
    
    

提交回复
热议问题