Created
May 31, 2019 18:45
-
-
Save rjgux/1a7ec4993c8e0a18b51322c9a8c6eed9 to your computer and use it in GitHub Desktop.
SEOmatic & CraftQL with react-helmet
This file contains hidden or 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
/*eslint no-useless-escape: 0 */ | |
import React from "react" | |
import Helmet from "react-helmet" | |
import ReactHtmlParser from "react-html-parser" | |
import { graphql } from "gatsby" | |
const SEO = props => { | |
const { data } = props | |
const splitJsonLdStringIntoArray = data.metaJsonLdContainer | |
.replace(/\<\/script\>/g, "") | |
.split(`<script type="application/ld+json">`) | |
return ( | |
<Helmet> | |
{/* Inject raw HTML into head */} | |
{ReactHtmlParser([data.metaTitleContainer, data.metaTagContainer])} | |
{splitJsonLdStringIntoArray.map((script, index) => ( | |
<script type="application/ld+json" key={`json-ld-${index}`}> | |
{script} | |
</script> | |
))} | |
</Helmet> | |
) | |
} | |
const IndexPage = ({ data }) => { | |
const { seomatic } = data.craft.entry[0] | |
return ( | |
<> | |
<SEO data={seomatic} /> | |
</> | |
) | |
} | |
export const IndexPageQuery = graphql` | |
{ | |
craft { | |
entry: entries(section: [home]) { | |
... on Craft_Home { | |
seomatic { | |
metaTagContainer | |
metaTitleContainer | |
metaJsonLdContainer | |
} | |
} | |
} | |
} | |
} | |
` | |
export default IndexPage |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment