Script Tag for 3rd Party
Answered
Highlander posted this in #help-forum
HighlanderOP
I am using Next.js 14 and would like to add a 3rd party script tag to add a chat bot to my website. Here is the script tag that the 3rd party says I should add:
I get errors when trying to add it to my RootLayout. Any guidance would be appreciated.
<script type="text/javascript">
(function(d, t) {
var v = d.createElement(t), s = d.getElementsByTagName(t)[0];
v.onload = function() {
window.voiceflow.chat.load({
verify: { projectID: '655e9a9073f76b00076ed256' },
url: 'https://general-runtime.voiceflow.com',
versionID: 'production'
});
}
v.src = "https://cdn.voiceflow.com/widget/bundle.mjs"; v.type = "text/javascript"; s.parentNode.insertBefore(v, s);
})(document, 'script');
</script>I get errors when trying to add it to my RootLayout. Any guidance would be appreciated.
7 Replies
<script
type="text/javascript"
dangerouslySetInnerHTML={{
__html: `
(function(d, t) {
var v = d.createElement(t), s = d.getElementsByTagName(t)[0];
v.onload = function() {
window.voiceflow.chat.load({
verify: { projectID: '655e9a9073f76b00076ed256' },
url: 'https://general-runtime.voiceflow.com',
versionID: 'production'
});
}
v.src = "https://cdn.voiceflow.com/widget/bundle.mjs"; v.type = "text/javascript"; s.parentNode.insertBefore(v, s);
})(document, 'script');
`,
}}
></script>HighlanderOP
works but then get this error @Ray Error: Hydration failed because the initial UI does not match what was rendered on the server.
Warning: Expected server HTML to contain a matching <script> in <html>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error
Warning: Expected server HTML to contain a matching <script> in <html>.
See more info here: https://nextjs.org/docs/messages/react-hydration-error
use
import Script from "next/script";Answer
const id = useId();
....
return (
...
<Script
id={id}
type="text/javascript"
dangerouslySetInnerHTML={{
__html: `
(function(d, t) {
var v = d.createElement(t), s = d.getElementsByTagName(t)[0];
v.onload = function() {
window.voiceflow.chat.load({
verify: { projectID: '655e9a9073f76b00076ed256' },
url: 'https://general-runtime.voiceflow.com',
versionID: 'production'
});
}
v.src = "https://cdn.voiceflow.com/widget/bundle.mjs"; v.type = "text/javascript"; s.parentNode.insertBefore(v, s);
})(document, 'script');
`,
}}
></script>
)HighlanderOP
is that in the root layout under html tag?
inside body
HighlanderOP
thank you so much got it working!