-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathSEO.tsx
More file actions
166 lines (141 loc) · 4.17 KB
/
SEO.tsx
File metadata and controls
166 lines (141 loc) · 4.17 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
import { useStaticQuery, graphql } from 'gatsby';
import React, { ReactElement } from 'react';
import { Helmet } from 'react-helmet';
import { extractImage } from 'src/util/extractImage';
import { LocationType } from 'src/types/Location';
import { PostType } from 'src/types/Post';
import { SiteMetadataType } from 'src/types/SiteMetadata';
import { SiteMetadataQueryType } from 'src/types/queries';
type PropsType = {
location: LocationType;
pageTitle: string;
post?: PostType;
};
function create(name?: string, value?: string): ReactElement | null {
if (!name || !value) {
return null;
}
return <meta key={name} property={name} content={value} />;
}
function generatePostSpecificTags(
post: PostType | undefined,
siteMetadata: SiteMetadataType,
url: string
): Array<ReactElement | null> {
if (!post) {
return [];
}
const data = post?.frontmatter;
if (!data) {
throw new Error(`Page had missing frontmatter: ${JSON.stringify(data)}`);
}
const rawPath = extractImage(post.html);
const postImage =
rawPath && !rawPath.includes('http') ? `${siteMetadata.domain}${rawPath}` : rawPath;
const twitterCardType = postImage ? 'summary_large_image' : 'summary';
const socialImage = postImage || siteMetadata.author.image;
const description = post.textPreview;
const ld = {
'@context': 'http://schema.org',
'@type': 'Article',
publisher: {
'@type': 'Organization',
name: siteMetadata.blogTitle,
logo: siteMetadata.author.image,
},
author: {
'@type': 'Person',
name: siteMetadata.author.name,
image: siteMetadata.author.image,
url: siteMetadata.author.url,
description: siteMetadata.author.blurb,
},
headline: data.title,
datePublished: data.date,
url,
description,
image: {
'@type': 'ImageObject',
url: socialImage,
},
mainEntityOfPage: url,
};
return [
create('og:image', socialImage),
create('og:type', 'article'),
create('og:title', data.title),
create('og:description', description),
create('twitter:image', socialImage),
create('twitter:card', twitterCardType),
create('twitter:title', data.title),
create('twitter:description', description),
<meta key="description" name="description" content={description} />,
create('article:published_time', data.date),
<script key="ld" type="application/ld+json">
{JSON.stringify(ld, null, ' ')}
</script>,
];
}
function generateMetaTags(
siteMetadata: SiteMetadataType,
post: PostType | undefined,
location: LocationType
): Array<ReactElement | null> {
const url = siteMetadata.domain + location.pathname;
const tags = [
<link key="canonical" rel="canonical" href={url} />,
<link
key="alternate"
rel="alternate"
type="application/rss+xml"
title={siteMetadata.blogTitle}
href={`${siteMetadata.domain}/rss.xml`}
/>,
create('og:site_name', siteMetadata.blogTitle),
create('og:url', url),
create('twitter:url', url),
create('twitter:site', siteMetadata.author.twitter),
];
const postSpecific = generatePostSpecificTags(post, siteMetadata, url);
if (!postSpecific.length) {
tags.push(
<meta key="description" name="description" content={siteMetadata.tagLine} />
);
tags.push(create('og:image', siteMetadata.author.image));
tags.push(create('twitter:image', siteMetadata.author.image));
}
return tags.concat(postSpecific);
}
function SEO({ pageTitle, post, location }: PropsType): ReactElement | null {
const data: SiteMetadataQueryType = useStaticQuery(
graphql`
query {
site {
siteMetadata {
author {
name
email
twitter
url
image
blurb
}
blogTitle
domain
favicon
tagLine
}
}
}
`
);
const { siteMetadata } = data.site;
return (
<Helmet>
<title>{`${pageTitle} | ${siteMetadata.blogTitle}`}</title>
<link rel="shortcut icon" href={siteMetadata.favicon} />
{generateMetaTags(siteMetadata, post, location)}
</Helmet>
);
}
export default SEO;