Passed
Pull Request — develop (#758)
by Kevin Van
10:05 queued 05:26
created

srcBU/components/seo.js   A

Complexity

Total Complexity 8
Complexity/F 8

Size

Lines of Code 184
Function Count 1

Duplication

Duplicated Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
wmc 8
eloc 135
mnd 7
bc 7
fnc 1
dl 0
loc 184
rs 10
bpm 7
cpm 8
noi 0
c 0
b 0
f 0

1 Function

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