-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSeoGenerator.tsx
More file actions
318 lines (288 loc) · 8.78 KB
/
Copy pathSeoGenerator.tsx
File metadata and controls
318 lines (288 loc) · 8.78 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
import Head from 'next/head';
import { useRouter } from 'next/router';
import Script from 'next/script';
import type { FC } from 'react';
import type { TRouter } from '@local-types/global';
import { generateSchema } from '@lib/schema';
interface SeoGeneratorProps {
questionsSeo?: any;
strapiSEO?: any;
userFavIcon?: string;
localizedSlug?: any;
modifiedDate?: string;
createdDate?: string;
isLongevityPage?: boolean;
type?: string;
forceNoIndex?: boolean;
canonicalOverride?: string;
preloadImages?: string[];
ogTags?: {
ogDescription: string;
ogTitle: string;
ogType: string;
ogImageAlt?: string;
ogStaticTitle?: string;
ogImage?: {
data: {
attributes: {
url: string;
staticUrl?: string;
};
};
};
};
}
const SeoGenerator: FC<SeoGeneratorProps> = ({
questionsSeo,
strapiSEO = {},
userFavIcon,
ogTags,
createdDate,
modifiedDate,
localizedSlug,
isLongevityPage,
type,
forceNoIndex,
canonicalOverride,
preloadImages,
}) => {
const router = useRouter();
if (forceNoIndex && canonicalOverride) {
return (
<Head>
<meta name="robots" content="noindex, nofollow" />
<link rel="canonical" href={canonicalOverride} />
</Head>
);
}
const hasStrapiSEO =
!!strapiSEO.title ||
!!strapiSEO.description ||
!!strapiSEO.keywords ||
!!strapiSEO.pageTitle;
const seoData = { en: {}, ru: {} };
if (questionsSeo) {
seoData.en = {
...questionsSeo.en,
};
seoData.ru = {
...questionsSeo.ru,
};
}
const { locale, asPath } = router as TRouter;
let pathname = asPath;
const indexOfQuestionMark = asPath.indexOf('?');
const indexHashTag = asPath.indexOf('#');
if (indexOfQuestionMark !== -1) {
pathname = asPath.slice(0, indexOfQuestionMark);
}
if (indexHashTag !== -1) {
pathname = asPath.slice(0, indexHashTag);
}
const favIconPath = '/keepsimple_/assets/favicon.svg';
// HYTranslation TODO
// @ts-ignore
const staticSeo =
locale === 'hy' ? seoData['en'][pathname] : seoData[locale][pathname];
if (pathname.includes('next') || (!staticSeo && !hasStrapiSEO))
return (
<Head>
<meta
name="robots"
content={
process.env.NEXT_PUBLIC_INDEXING === 'off'
? 'noindex, nofollow'
: 'index, follow'
}
/>
<meta
name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=2, viewport-fit=cover"
/>
<title>keep-simple | Error Page</title>
<meta name="description" content={'404 page - page not found'} />
<meta name="keywords" content={'404 page - page not found'} />
</Head>
);
let description: string;
let keywords: string;
let pageTitle: string;
let title: string;
function stripHTML(input: string): string {
return input?.replace(/<[^>]*>/g, '') ?? '';
}
// @ts-ignore
if (staticSeo) {
({ title, description, keywords, pageTitle } = staticSeo);
}
if (hasStrapiSEO) {
({ title, description, keywords, pageTitle } = strapiSEO);
}
// HYTranslation TODO
const alternateLink = asPath === '/' ? '' : asPath;
const localePath = locale === 'en' ? '' : `/${locale}`;
function cleanURL(url: string) {
const exceptions = [
'/uxcp?name=&biases=&isTeamMember=false',
'#hr',
'?search=',
];
for (const exception of exceptions) {
if (url.includes(exception)) {
return url;
}
}
const symbols = ['?', '&', '=', '#', ';'];
let clean = url;
for (const symbol of symbols) {
if (clean.includes(symbol)) {
clean = clean.split(symbol)[0];
}
}
return clean;
}
const originalUrl =
process.env.NEXT_PUBLIC_DOMAIN + localePath + cleanURL(alternateLink);
const favIcon = `${process.env.NEXT_PUBLIC_DOMAIN}${favIconPath}`;
const schema = generateSchema(
title,
stripHTML(description),
originalUrl,
favIcon,
createdDate,
modifiedDate,
type,
);
return (
<>
<Head>
<meta charSet="UTF-8" />
<meta
name="viewport"
content="width=device-width, initial-scale=1, maximum-scale=2, viewport-fit=cover"
/>
<link
rel="preload"
href="/keepsimple_/fonts/Source-Serif-4/static/SourceSerif4-Regular.ttf"
as="font"
type="font/woff2"
crossOrigin="anonymous"
/>
{preloadImages?.map(src => (
<link key={src} rel="preload" as="image" href={src} />
))}
<meta httpEquiv="X-UA-Compatible" content="ie=edge" />
<meta name="theme-color" content="#1e2023" />
{pathname.includes('/user') ? (
<meta name="robots" content={'noindex, nofollow'} />
) : (
<meta
name="robots"
content={
process.env.NEXT_PUBLIC_INDEXING === 'off'
? 'noindex, nofollow'
: 'index, follow'
}
/>
)}
<link rel="shortcut icon" href={favIconPath} />
<title>{title}</title>
<link rel="canonical" href={originalUrl} key={'canonical'} />
<link
rel="alternate"
hrefLang="ru"
href={`${process.env.NEXT_PUBLIC_DOMAIN}/ru${localizedSlug ? `${localizedSlug.slugRu}` : alternateLink}`}
/>
<link
rel="alternate"
hrefLang="en"
href={`${process.env.NEXT_PUBLIC_DOMAIN}${localizedSlug ? `${localizedSlug.slugEn}` : alternateLink}`}
/>
<link
rel="alternate"
hrefLang="hy"
href={`${process.env.NEXT_PUBLIC_DOMAIN}/hy${localizedSlug ? `${localizedSlug.slugEn}` : alternateLink}`}
/>
<link
rel="alternate"
hrefLang="x-default"
href={`${process.env.NEXT_PUBLIC_DOMAIN}${localizedSlug ? `${localizedSlug.slugEn}` : alternateLink}`}
/>
<meta name="description" content={stripHTML(description)} />
<meta name="keywords" content={keywords} />
{/* GOOGLE */}
<meta itemProp="name" content={pageTitle} />
<meta itemProp="description" content={stripHTML(description)} />
<meta
itemProp="image"
content="https://keepsimple.io/assets/keep-simple.jpg"
/>
<meta
property="og:title"
content={ogTags?.ogTitle ? ogTags.ogTitle : ogTags?.ogStaticTitle}
/>
<meta property="og:description" content={ogTags?.ogDescription} />
<meta
property="og:image"
content={
ogTags?.ogImage?.data?.attributes?.url
? isLongevityPage
? ogTags?.ogImage?.data?.attributes?.url
: `${process.env.NEXT_PUBLIC_STRAPI}${ogTags?.ogImage?.data?.attributes?.url}`
: ogTags?.ogImage?.data?.attributes?.staticUrl
}
/>
<meta
property="og:image:alt"
content={
ogTags?.ogImageAlt
? ogTags?.ogImageAlt
: ogTags?.ogTitle || ogTags?.ogStaticTitle
}
/>
<meta property="og:url" content={originalUrl} />
<meta property="og:site_name" content="Keep Simple" />
<meta property="og:type" content={ogTags?.ogType} />
{/* AUTHRO */}
<meta property="article:author" content="Wolf Alexanyan" />
{/* TWITTER */}
<meta name="twitter:card" content="summary" />
<meta name="twitter:creator" content="@AlexanyanWolf" />
<meta name="twitter:title" content={pageTitle} />
<meta name="twitter:description" content={ogTags?.ogDescription} />
<meta
name="twitter:image"
content={
ogTags?.ogImage?.data?.attributes?.url
? isLongevityPage
? ogTags?.ogImage?.data?.attributes?.url
: `${process.env.NEXT_PUBLIC_STRAPI}${ogTags?.ogImage?.data?.attributes?.url}`
: ogTags?.ogImage?.data?.attributes?.staticUrl
}
/>
<meta name="twitter:url" content={originalUrl} />
<meta name="twitter:label1" content="Written by" />
<meta name="twitter:data1" content="Wolf Alexanyan" />
<script
type="application/ld+json"
dangerouslySetInnerHTML={{
__html: JSON.stringify(schema)
.replace(/</g, '\\u003c')
.replace(/>/g, '\\u003e')
.replace(/&/g, '\\u0026'),
}}
/>
</Head>
<Script
src="https://analytics.ahrefs.com/analytics.js"
data-key={process.env.NEXT_PUBLIC_AHREFS_ANALYTICS_KEY}
strategy="afterInteractive"
/>
<Script
src="https://seogeosolver.administration.ae/track.js"
strategy="afterInteractive"
/>
</>
);
};
export default SeoGenerator;