Passed
Push — feature/social-media-cards ( 7227d0...98fdac )
by Kevin Van
02:54
created

seo.js ➔ SEO   A

Complexity

Conditions 3

Size

Total Lines 50
Code Lines 35

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
eloc 35
dl 0
loc 50
rs 9.0399
c 0
b 0
f 0
cc 3
1
import React from "react"
2
import PropTypes from "prop-types"
3
import Helmet from "react-helmet"
4
import { StaticQuery, graphql } from "gatsby"
5
6
import defaultOgImage from "../images/preseason.jpg"
7
8
// function SEO({ description, lang, meta, keywords, title }) {
9
function SEO({
10
  lang,
11
  title,
12
  description,
13
  meta,
14
  keywords,
15
  path,
16
  image: metaImage,
17
}) {
18
  return (
19
    <StaticQuery
20
      query={detailsQuery}
21
      render={({ site }) => {
22
        const metaDescription = description || site.siteMetadata.description
23
        const canonicalUrl = path ? `${site.siteMetadata.siteUrl}${path}` : null
24
25
        return (
26
          <Helmet
27
            htmlAttributes={{
28
              lang,
29
            }}
30
            title={title}
31
            titleTemplate={`%s | ${site.siteMetadata.title}`}
32
            link={
33
              canonicalUrl
34
                ? [
35
                    {
36
                      rel: "canonical",
37
                      href: canonicalUrl,
38
                    },
39
                  ]
40
                : []
41
            }
42
            meta={[
43
              {
44
                name: `description`,
45
                content: metaDescription,
46
              },
47
              {
48
                property: `fb:app_id`,
49
                content: site.siteMetadata.fbAppId,
50
              },
51
            ]
52
              .concat(getOgMeta(title, metaDescription, canonicalUrl))
53
              .concat(getOgImage(site, metaImage))
54
              .concat(getTwitterMeta(site, title, metaDescription, metaImage))
55
              .concat(getKeywords(keywords))
56
              .concat(meta)}
57
          />
58
        )
59
      }}
60
    />
61
  )
62
}
63
const getOgMeta = (title, metaDescription, canonicalUrl) => {
64
  const ogMeta = [
65
    {
66
      property: `og:title`,
67
      content: title,
68
    },
69
    {
70
      property: `og:description`,
71
      content: metaDescription,
72
    },
73
    {
74
      property: `og:type`,
75
      content: `website`,
76
    },
77
  ].concat(getOgUrl(canonicalUrl))
78
  return ogMeta
79
}
80
81
const getTwitterMeta = (site, title, metaDescription, metaImage) => {
82
  const twitterMeta = [
83
    {
84
      name: `twitter:creator`,
85
      content: site.siteMetadata.author,
86
    },
87
    {
88
      name: `twitter:title`,
89
      content: title,
90
    },
91
    {
92
      name: `twitter:description`,
93
      content: metaDescription,
94
    },
95
  ].concat(getTwitterCard(site, metaImage))
96
  return twitterMeta
97
}
98
99
const getOgImage = ({ siteMetadata }, metaImage) => {
100
  const image =
101
    metaImage && metaImage.src
102
      ? `${siteMetadata.siteUrl}${metaImage.src}`
103
      : null
104
  return metaImage
105
    ? [
106
        {
107
          property: "og:image",
108
          content: image,
109
        },
110
        {
111
          property: "og:image:width",
112
          content: metaImage.width,
113
        },
114
        {
115
          property: "og:image:height",
116
          content: metaImage.height,
117
        },
118
      ]
119
    : []
120
}
121
122
const getTwitterCard = (metaImage) =>
123
  metaImage
124
    ? {
125
        name: "twitter:card",
126
        content: "summary_large_image",
127
      }
128
    : {
129
        name: "twitter:card",
130
        content: "summary",
131
      }
132
133
const getOgUrl = (canonicalUrl) =>
134
  canonicalUrl
135
    ? {
136
        property: `og:url`,
137
        content: canonicalUrl,
138
      }
139
    : []
140
141
const getKeywords = (keywords) =>
142
  keywords.length > 0
143
    ? {
144
        name: `keywords`,
145
        content: keywords.join(`, `),
146
      }
147
    : []
148
149
SEO.defaultProps = {
150
  lang: `nl-BE`,
151
  meta: [],
152
  keywords: [],
153
  path: "/",
154
  image: {
155
    src: defaultOgImage,
156
    width: 2000,
157
    height: 1000,
158
  },
159
}
160
161
SEO.propTypes = {
162
  subtitle: PropTypes.string,
163
  description: PropTypes.string,
164
  lang: PropTypes.string,
165
  meta: PropTypes.array,
166
  keywords: PropTypes.arrayOf(PropTypes.string),
167
  title: PropTypes.string.isRequired,
168
  path: PropTypes.string,
169
  image: PropTypes.shape({
170
    src: PropTypes.string.isRequired,
171
    height: PropTypes.number.isRequired,
172
    width: PropTypes.number.isRequired,
173
  }),
174
}
175
176
export default SEO
177
178
const detailsQuery = graphql`
179
  query DefaultSEOQuery {
180
    site {
181
      siteMetadata {
182
        title
183
        subTitle
184
        description
185
        author
186
        siteUrl
187
        fbAppId
188
      }
189
    }
190
  }
191
`
192