-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.tsx
151 lines (144 loc) · 3.88 KB
/
index.tsx
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
import React, { memo } from 'react';
import NextHead from 'next/head';
type SEOProps = {
title?: string;
canonical?: string;
/**
* Set `robots` only when you don't want search engines to index the page.
*/
robots?: 'noindex' | 'nofollow' | 'noindex, nofollow' | 'index, follow';
description?: string;
/**
* Truncate after maxDescriptionCharacters. Default value is 150.
* (Google generally truncates descritpion to ~155–160 characters https://moz.com/learn/seo/meta-description )
*/
maxDescriptionCharacters?: number;
twitter?: {
/**
* Twitter card type
* https://developer.twitter.com/en/docs/twitter-for-websites/cards/overview/abouts-cards
*/
card?: 'summary' | 'summary_large_image' | 'player' | 'app';
/**
* Twitter username starting with @
*/
site?: string;
};
/**
* Additional meta tags.
* Setting same value for `key` property between `<DefaultSeo />` and `<PageSeo />` will override the default meta tag (Otherwise both of them will be rendered)
*/
customMetaTags?: Record<string, string>[];
/**
* Additional link tags.
* Setting same value for `key` property between `<DefaultSeo />` and `<PageSeo />` will override the default meta tag (Otherwise both of them will be rendered)
*/
customLinkTags?: Record<string, string>[];
og?: {
title?: string;
description?: string;
url?: string;
type?: 'article' | 'book' | 'website' | 'profile';
/**
* URL for og:image
*/
image?: string;
siteName?: string;
};
};
const SEO: React.VFC<SEOProps> = memo(
({
title,
description,
canonical,
robots,
maxDescriptionCharacters = 150,
twitter = {},
og = {},
customMetaTags = [],
customLinkTags = []
}) => {
const tags = [];
if (title) {
tags.push(<title key="title">{title}</title>);
}
if (robots) {
tags.push(<meta key="robots" name="robots" content={robots} />);
}
if (description) {
tags.push(
<meta
key="description"
name="description"
content={description.substr(0, maxDescriptionCharacters)}
/>
);
}
if (canonical) {
tags.push(<link key="canonical" rel="canonical" href={canonical} />);
}
if (twitter.card) {
tags.push(
<meta key="twitter:card" name="twitter:card" content={twitter.card} />
);
}
if (twitter.site) {
tags.push(
<meta key="twitter:site" name="twitter:site" content={twitter.site} />
);
}
if (og.url || canonical) {
tags.push(
<meta key="og:url" property="og:url" content={og.url || canonical} />
);
}
if (og.title || title) {
tags.push(
<meta key="og:title" property="og:title" content={og.title || title} />
);
}
if (og.image) {
tags.push(<meta key="og:image" property="og:image" content={og.image} />);
}
if (og.description || description) {
tags.push(
<meta
key="og:description"
property="og:description"
content={(og.description || description)?.substr(
0,
maxDescriptionCharacters
)}
/>
);
}
if (og.type) {
tags.push(<meta key="og:type" property="og:type" content={og.type} />);
}
if (og.siteName) {
tags.push(
<meta
key="og:site_name"
property="og:site_name"
content={og.siteName}
/>
);
}
if (customMetaTags.length > 0) {
tags.push(
customMetaTags.map(({ key, ...tagProps }, i) => (
<meta key={`meta-${key || i}`} {...tagProps} />
))
);
}
if (customLinkTags.length > 0) {
tags.push(
customLinkTags.map(({ key, ...tagProps }, i) => (
<link key={`link-${key || i}`} {...tagProps} />
))
);
}
return <NextHead>{tags}</NextHead>;
}
);
export default SEO;