Skip to content

Instantly share code, notes, and snippets.

@albertpak
Created April 28, 2021 18:04
Show Gist options
  • Save albertpak/dff14385102fe76513b9d7bfc9b03412 to your computer and use it in GitHub Desktop.
Save albertpak/dff14385102fe76513b9d7bfc9b03412 to your computer and use it in GitHub Desktop.
Add FB Chat messenger to NextJS
// In case anyone else needs this, here's what I done to add FB Messenger code to my NextJS app - As mentioned before, you need to drop the plugin code into `_document.js` and modify `script` tag
import Document, { Html, Head, Main, NextScript } from 'next/document';
export default class MyDocument extends Document {
render() {
return (
<Html>
<Head>
{/* code that you want to add to the header */}
</Head>
<body>
<Main />
<NextScript />
</body>
<div id="fb-root"></div>
<script
dangerouslySetInnerHTML={{
__html: `
window.fbAsyncInit = function() {
FB.init({
xfbml : true,
version : 'v10.0'
});
};
(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/xfbml.customerchat.js';
fjs.parentNode.insertBefore(js, fjs);
}(document, 'script', 'facebook-jssdk'));
`,
}}
/>
<div className="fb-customerchat"
attribution="page_inbox"
page_id="YOUR_FB_PAGE_ID">
</div>
</Html>
)
}
}
@VoDuyHung
Copy link

VoDuyHung commented Jun 9, 2021

Thank you, and I have an question, why did you add script out of body tag ?

@hassanUI786
Copy link

not working

@monpham
Copy link

monpham commented Nov 12, 2021

fails

@thachsteven
Copy link

can not

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment