Created
March 18, 2019 08:21
-
-
Save koistya/5411b8a26e1a55e41c0196d8dc6dce1b to your computer and use it in GitHub Desktop.
Lazy loader for Facebook JavaScript SDK / Customer Chat SDK https://medium.com/p/5b7c21343048/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import loadScript from 'load-script'; | |
let initialized = false; | |
let queue = []; | |
export function fb(callback) { | |
if if (initialized) { | |
callback(window.FB); | |
} else { | |
queue.push(callback); | |
if (!window.fbAsyncInit) { | |
window.fbAsyncInit = () => { | |
window.FB.init({ | |
appId: window.config.facebook.appId, | |
autoLogAppEvents: true, | |
status: true, | |
cookie: true, | |
xfbml: false, | |
version: 'v3.2', | |
}); | |
initialized = true; | |
queue.forEach(cb => cb(window.FB)); | |
queue = null; | |
}; | |
const script = window.localStorage.getItem('fb:debug') === 'true' | |
? 'xfbml.customerchat/debug.js' | |
: 'xfbml.customerchat.js'; | |
loadScript(`https://connect.facebook.net/en_US/sdk/${script}`, { async: true }); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment