Hi guys! Sometimes i need to add some js scripts inside <body> but i have totally no idea how i could do that! Adding it to head it's easy with Helmet, but what about body?
Thanks!
Have a look at the docs for customizing html.js. It should do what you're asking.
@markmichon thanks!
So do i have to just add this
;<div
key={`body`}
id="___gatsby"
dangerouslySetInnerHTML={{ __html: this.props.body }}
/>
and replace _this.props.body_ with <script>myCode</script> ?
Or just add <script> inside body as i would normally do?
Take a look at the file, as mentioned in the doc link. Looks something like this:
import React from "react"
let stylesStr
if (process.env.NODE_ENV === `production`) {
try {
stylesStr = require(`!raw-loader!../public/styles.css`)
} catch (e) {
console.log(e)
}
}
module.exports = class HTML extends React.Component {
render() {
let css
if (process.env.NODE_ENV === `production`) {
css = (
<style
id="gatsby-inlined-css"
dangerouslySetInnerHTML={{ __html: stylesStr }}
/>
)
}
return (
<html {...this.props.htmlAttributes}>
<head>
<meta charSet="utf-8" />
<meta httpEquiv="x-ua-compatible" content="ie=edge" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, shrink-to-fit=no"
/>
{this.props.headComponents}
{css}
</head>
<body {...this.props.bodyAttributes}>
{this.props.preBodyComponents}
<div
key={`body`}
id="___gatsby"
dangerouslySetInnerHTML={{ __html: this.props.body }}
/>
{this.props.postBodyComponents}
</body>
</html>
)
}
}
Insert script where you'd normally insert it in the html structure. I suspect right before the body closes is what you're looking for.
Thanks a lot!!!
Most helpful comment
Take a look at the file, as mentioned in the doc link. Looks something like this:
Insert script where you'd normally insert it in the html structure. I suspect right before the body closes is what you're looking for.