|
|
|
@ -5,33 +5,29 @@ import { siteDataRef } from './siteData'
|
|
|
|
|
* @param {import('./pageData').PageDataRef} pageDataRef
|
|
|
|
|
*/
|
|
|
|
|
export function useUpdateHead(pageDataRef) {
|
|
|
|
|
const descriptionTag = createHeadElement(['meta', {
|
|
|
|
|
name: 'description',
|
|
|
|
|
content: siteDataRef.value.description
|
|
|
|
|
}])
|
|
|
|
|
document.head.appendChild(descriptionTag)
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @type {HTMLElement[]}
|
|
|
|
|
*/
|
|
|
|
|
const siteHeadTags = []
|
|
|
|
|
/**
|
|
|
|
|
* @type {HTMLElement[]}
|
|
|
|
|
*/
|
|
|
|
|
const pageHeadTags = []
|
|
|
|
|
const metaTags = Array.from(document.querySelectorAll('meta'))
|
|
|
|
|
|
|
|
|
|
let isFirstUpdate = true
|
|
|
|
|
/**
|
|
|
|
|
* @param {HTMLElement[]} tags
|
|
|
|
|
* @param {import('src').HeadConfig[]} newTags
|
|
|
|
|
*/
|
|
|
|
|
const updateHeadTags = (tags, newTags) => {
|
|
|
|
|
tags.forEach((el) => document.head.removeChild(el))
|
|
|
|
|
tags.length = 0
|
|
|
|
|
const updateHeadTags = (newTags) => {
|
|
|
|
|
if (!__DEV__ && isFirstUpdate) {
|
|
|
|
|
// in production, the initial meta tags are already pre-rendered so we
|
|
|
|
|
// skip the first update.
|
|
|
|
|
isFirstUpdate = false
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
metaTags.forEach((el) => document.head.removeChild(el))
|
|
|
|
|
metaTags.length = 0
|
|
|
|
|
if (newTags && newTags.length) {
|
|
|
|
|
newTags.forEach((headConfig) => {
|
|
|
|
|
const el = createHeadElement(headConfig)
|
|
|
|
|
document.head.appendChild(el)
|
|
|
|
|
tags.push(el)
|
|
|
|
|
metaTags.push(el)
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
@ -41,12 +37,14 @@ export function useUpdateHead(pageDataRef) {
|
|
|
|
|
const siteData = siteDataRef.value
|
|
|
|
|
const pageTitle = pageData && pageData.title
|
|
|
|
|
document.title = (pageTitle ? pageTitle + ` | ` : ``) + siteData.title
|
|
|
|
|
descriptionTag.setAttribute('content', siteData.description)
|
|
|
|
|
updateHeadTags(siteHeadTags, siteData.head)
|
|
|
|
|
updateHeadTags(
|
|
|
|
|
pageHeadTags,
|
|
|
|
|
pageData && pageData.frontmatter.head
|
|
|
|
|
)
|
|
|
|
|
updateHeadTags([
|
|
|
|
|
['meta', {
|
|
|
|
|
name: 'description',
|
|
|
|
|
content: siteData.description
|
|
|
|
|
}],
|
|
|
|
|
...siteData.head,
|
|
|
|
|
...(pageData && pageData.frontmatter.head || [])
|
|
|
|
|
])
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|