|
| 1 | +import React from 'react' |
| 2 | +import PropTypes from 'prop-types' |
| 3 | +import { useStaticQuery, graphql } from 'gatsby' |
| 4 | + |
| 5 | +// This function now acts as a Head component that Gatsby will use to populate <head> |
| 6 | +export function Head({ |
| 7 | + lang = 'en', |
| 8 | + meta = [], |
| 9 | + keywords = '', |
| 10 | + description = '', |
| 11 | + title, |
| 12 | +}) { |
| 13 | + const { site, placeholderImage } = useStaticQuery(graphql` |
| 14 | + query DefaultSEOQuery { |
| 15 | + site { |
| 16 | + siteMetadata { |
| 17 | + title |
| 18 | + description |
| 19 | + author |
| 20 | + } |
| 21 | + } |
| 22 | + placeholderImage: file(relativePath: { eq: "avatar.jpg" }) { |
| 23 | + childImageSharp { |
| 24 | + gatsbyImageData(width: 300, layout: FIXED) |
| 25 | + original { |
| 26 | + src |
| 27 | + } |
| 28 | + } |
| 29 | + } |
| 30 | + } |
| 31 | + `) |
| 32 | + |
| 33 | + const metaDescription = description || site.siteMetadata.description |
| 34 | + |
| 35 | + const imageSrc = placeholderImage.childImageSharp.original.src |
| 36 | + const allMeta = [ |
| 37 | + { |
| 38 | + name: `description`, |
| 39 | + content: metaDescription, |
| 40 | + }, |
| 41 | + { |
| 42 | + property: `og:title`, |
| 43 | + content: title, |
| 44 | + }, |
| 45 | + { |
| 46 | + property: `og:description`, |
| 47 | + content: metaDescription, |
| 48 | + }, |
| 49 | + { |
| 50 | + property: `og:type`, |
| 51 | + content: `website`, |
| 52 | + }, |
| 53 | + { |
| 54 | + property: `og:image`, |
| 55 | + content: imageSrc, |
| 56 | + }, |
| 57 | + { |
| 58 | + name: `twitter:card`, |
| 59 | + content: `summary`, |
| 60 | + }, |
| 61 | + { |
| 62 | + name: 'twitter:site', |
| 63 | + content: site.siteMetadata.author, |
| 64 | + }, |
| 65 | + { |
| 66 | + name: `twitter:creator`, |
| 67 | + content: site.siteMetadata.author, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: `twitter:title`, |
| 71 | + content: title, |
| 72 | + }, |
| 73 | + { |
| 74 | + name: `twitter:description`, |
| 75 | + content: metaDescription, |
| 76 | + }, |
| 77 | + { |
| 78 | + name: `twitter:image`, |
| 79 | + content: imageSrc, |
| 80 | + }, |
| 81 | + { |
| 82 | + name: `twitter:domain`, |
| 83 | + content: `mattboldt.com`, |
| 84 | + }, |
| 85 | + { |
| 86 | + name: `keywords`, |
| 87 | + content: keywords, |
| 88 | + }, |
| 89 | + ].concat(meta) |
| 90 | + |
| 91 | + // When using Gatsby's Head API, we don't need fragments or html tag |
| 92 | + // Gatsby will automatically place these elements in the document head |
| 93 | + return ( |
| 94 | + <> |
| 95 | + <title>{title} | {site.siteMetadata.title}</title> |
| 96 | + {allMeta.map((metaItem, i) => ( |
| 97 | + <meta key={i} {...metaItem} /> |
| 98 | + ))} |
| 99 | + <link rel="shortcut icon" href="/favicon.ico" /> |
| 100 | + </> |
| 101 | + ) |
| 102 | +} |
| 103 | + |
| 104 | +Head.propTypes = { |
| 105 | + description: PropTypes.string, |
| 106 | + lang: PropTypes.string, |
| 107 | + meta: PropTypes.array, |
| 108 | + keywords: PropTypes.string, |
| 109 | + title: PropTypes.string.isRequired, |
| 110 | +} |
0 commit comments