-
-
Notifications
You must be signed in to change notification settings - Fork 344
Expand file tree
/
Copy pathseo.ts
More file actions
85 lines (72 loc) · 2.05 KB
/
seo.ts
File metadata and controls
85 lines (72 loc) · 2.05 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
import { env } from '~/utils/env'
const DEFAULT_SITE_URL = 'https://tanstack.com'
const NON_INDEXABLE_PATH_PREFIXES = ['/account', '/admin', '/login'] as const
function trimTrailingSlash(value: string) {
return value.replace(/\/$/, '')
}
function normalizePath(path: string) {
if (!path || path === '/') {
return '/'
}
const normalizedPath = path.startsWith('/') ? path : `/${path}`
return normalizedPath.replace(/\/$/, '')
}
export function getCanonicalPath(path: string) {
const normalizedPath = normalizePath(path)
if (
NON_INDEXABLE_PATH_PREFIXES.some(
(prefix) =>
normalizedPath === prefix || normalizedPath.startsWith(`${prefix}/`),
)
) {
return null
}
return normalizedPath
}
export function shouldIndexPath(path: string) {
return getCanonicalPath(path) !== null
}
export function canonicalUrl(path: string) {
const origin = trimTrailingSlash(
env.URL ||
(import.meta.env.SSR ? env.SITE_URL : undefined) ||
DEFAULT_SITE_URL,
)
return `${origin}${normalizePath(path)}`
}
type SeoOptions = {
title: string
description?: string
image?: string
keywords?: string
noindex?: boolean
}
export const seo = ({
title,
description,
keywords,
image,
noindex,
}: SeoOptions) => {
const tags = [
{ title },
{ name: 'description', content: description },
{ name: 'keywords', content: keywords },
{ name: 'twitter:title', content: title },
{ name: 'twitter:description', content: description },
{ name: 'twitter:creator', content: '@tannerlinsley' },
{ name: 'twitter:site', content: '@tannerlinsley' },
{ property: 'og:type', content: 'website' },
{ property: 'og:title', content: title },
{ property: 'og:description', content: description },
...(image
? [
{ name: 'twitter:image', content: image },
{ name: 'twitter:card', content: 'summary_large_image' },
{ property: 'og:image', content: image },
]
: []),
...(noindex ? [{ name: 'robots', content: 'noindex, nofollow' }] : []),
]
return tags
}