Skip to content

Instantly share code, notes, and snippets.

@koistya
Created March 18, 2019 08:21
Show Gist options
  • Save koistya/5411b8a26e1a55e41c0196d8dc6dce1b to your computer and use it in GitHub Desktop.
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/
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