-
-
Notifications
You must be signed in to change notification settings - Fork 604
/
head.ts
132 lines (118 loc) · 4.56 KB
/
head.ts
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
import { RouteLocationNormalized, RouteLocationNormalizedLoaded } from 'vue-router'
import type { HeadObjectPlain } from '@vueuse/head'
import type { Ref } from 'vue'
import { hasProtocol, joinURL, withTrailingSlash, withoutTrailingSlash } from 'ufo'
import { ParsedContent } from '../types'
import { useRoute, nextTick, useHead, unref, watch, useRuntimeConfig, useRequestEvent } from '#imports'
export const useContentHead = (
_content: ParsedContent | Ref<ParsedContent>,
to: RouteLocationNormalized | RouteLocationNormalizedLoaded = useRoute()
) => {
const content = unref(_content)
const config = useRuntimeConfig()
const refreshHead = (data: ParsedContent = content) => {
// Don't call this function if no route is yet available
if (!to.path || !data) { return }
// Default head to `data?.head`
const head: HeadObjectPlain = Object.assign({}, data?.head || {})
head.meta = [...(head.meta || [])]
head.link = [...(head.link || [])]
// Great basic informations from the data
const title = head.title || data?.title
if (title) {
head.title = title
if (process.server && !head.meta.some(m => m.property === 'og:title')) {
head.meta.push({
name: 'og:title',
content: title
})
}
}
const host = config.public.content.host
// if (process.server && !host) {
// const req = useRequestEvent().node?.req
// if (req && req.headers.host !== 'localhost') {
// const protocol = req.headers['x-forwarded-proto'] || req.connection.encrypted ? 'https' : 'http'
// host = `${protocol}://${req.headers.host}`
// }
// }
if (process.server && host) {
const _url = joinURL(host ?? '/', config.app.baseURL, to.fullPath)
const url = config.public.content.trailingSlash ? withTrailingSlash(_url) : withoutTrailingSlash(_url)
if (!head.meta.some(m => m.property === 'og:url')) {
head.meta.push({
name: 'og:url',
content: url
})
}
if (!head.link.some(m => m.rel === 'canonical')) {
head.link.push({
rel: 'canonical',
href: url
})
}
}
// Grab description from `head.description` or fallback to `data.description`
// @ts-ignore - We expect `head.description` from Nuxt configurations...
const description = head?.description || data?.description
// Shortcut for head.description
if (description && head.meta.filter(m => m.name === 'description').length === 0) {
head.meta.push({
name: 'description',
content: description
})
}
if (process.server && description && !head.meta.some(m => m.property === 'og:description')) {
head.meta.push({
name: 'og:description',
content: description
})
}
// Grab description from `head` or fallback to `data.description`
// @ts-ignore - We expect `head.image` from Nuxt configurations...
const image = head?.image || data?.image
// Shortcut for head.image to og:image in meta
if (process.server && image && head.meta.filter(m => m.property === 'og:image').length === 0) {
// Handles `image: '/image/src.jpg'`
if (typeof image === 'string') {
head.meta.push({
property: 'og:image',
// @ts-ignore - We expect `head.image` from Nuxt configurations...
content: host && !hasProtocol(image) ? new URL(joinURL(config.app.baseURL, image), host).href : image
})
}
// Handles: `image.src: '/image/src.jpg'` & `image.alt: 200`...
if (typeof image === 'object') {
// https://ogp.me/#structured
const imageKeys = [
'src',
'secure_url',
'type',
'width',
'height',
'alt'
]
// Look on available keys
for (const key of imageKeys) {
// `src` is a shorthand for the URL.
if (key === 'src' && image.src) {
const isAbsoluteURL = hasProtocol(image.src)
const imageURL = isAbsoluteURL ? image.src : joinURL(config.app.baseURL, image.src ?? '/')
head.meta.push({
property: 'og:image',
content: host && !isAbsoluteURL ? new URL(imageURL, host).href : imageURL
})
} else if (image[key]) {
head.meta.push({
property: `og:image:${key}`,
content: image[key]
})
}
}
}
}
// @ts-ignore
if (process.client) { nextTick(() => useHead(head)) } else { useHead(head) }
}
watch(() => unref(_content), refreshHead, { immediate: true })
}