Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: de-duplicate head tags while merging #976

Merged
merged 1 commit into from
Jul 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 9 additions & 41 deletions src/client/app/composables/head.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { watchEffect, Ref } from 'vue'
import { HeadConfig, SiteData, createTitle } from '../../shared'
import { HeadConfig, SiteData, createTitle, mergeHead } from '../../shared'
import { Route } from '../router'

export function useUpdateHead(route: Route, siteDataByRouteRef: Ref<SiteData>) {
Expand All @@ -14,50 +14,20 @@ export function useUpdateHead(route: Route, siteDataByRouteRef: Ref<SiteData>) {
return
}

const newEls: HTMLElement[] = []
const commonLength = Math.min(managedHeadTags.length, newTags.length)
for (let i = 0; i < commonLength; i++) {
let el = managedHeadTags[i]
const [tag, attrs, innerHTML = ''] = newTags[i]
if (el.tagName.toLocaleLowerCase() === tag) {
for (const key in attrs) {
if (el.getAttribute(key) !== attrs[key]) {
el.setAttribute(key, attrs[key])
}
}
for (let i = 0; i < el.attributes.length; i++) {
const name = el.attributes[i].name
if (!(name in attrs)) {
el.removeAttribute(name)
}
}
if (el.innerHTML !== innerHTML) {
el.innerHTML = innerHTML
}
} else {
document.head.removeChild(el)
el = createHeadElement(newTags[i])
document.head.append(el)
}
newEls.push(el)
}

managedHeadTags
.slice(commonLength)
.forEach((el) => document.head.removeChild(el))
newTags.slice(commonLength).forEach((headConfig) => {
managedHeadTags.forEach((el) => document.head.removeChild(el))
managedHeadTags = []
newTags.forEach((headConfig) => {
const el = createHeadElement(headConfig)
document.head.appendChild(el)
newEls.push(el)
managedHeadTags.push(el)
})
managedHeadTags = newEls
}

watchEffect(() => {
const pageData = route.data
const siteData = siteDataByRouteRef.value
const pageDescription = pageData && pageData.description
const frontmatterHead = pageData && pageData.frontmatter.head
const frontmatterHead = (pageData && pageData.frontmatter.head) || []

// update title and description
document.title = createTitle(siteData, pageData)
Expand All @@ -66,11 +36,9 @@ export function useUpdateHead(route: Route, siteDataByRouteRef: Ref<SiteData>) {
.querySelector(`meta[name=description]`)!
.setAttribute('content', pageDescription || siteData.description)

updateHeadTags([
// site head can only change during dev
brc-dd marked this conversation as resolved.
Show resolved Hide resolved
...(import.meta.env.DEV ? siteData.head : []),
...(frontmatterHead ? filterOutHeadDescription(frontmatterHead) : [])
])
updateHeadTags(
mergeHead(siteData.head, filterOutHeadDescription(frontmatterHead))
)
})
}

Expand Down
16 changes: 11 additions & 5 deletions src/node/build/render.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ import { pathToFileURL } from 'url'
import escape from 'escape-html'
import { normalizePath, transformWithEsbuild } from 'vite'
import { RollupOutput, OutputChunk, OutputAsset } from 'rollup'
import { HeadConfig, PageData, createTitle, notFoundPageData } from '../shared'
import {
HeadConfig,
PageData,
createTitle,
notFoundPageData,
mergeHead
} from '../shared'
import { slash } from '../utils/slash'
import { SiteConfig, resolveSiteDataByRoute } from '../config'

Expand Down Expand Up @@ -115,10 +121,10 @@ export async function renderPage(
const title: string = createTitle(siteData, pageData)
const description: string = pageData.description || siteData.description

const head = [
...siteData.head,
...filterOutHeadDescription(pageData.frontmatter.head)
]
const head = mergeHead(
siteData.head,
filterOutHeadDescription(pageData.frontmatter.head)
)

let inlinedScript = ''
if (config.mpa && result) {
Expand Down
2 changes: 1 addition & 1 deletion src/node/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -285,7 +285,7 @@ function resolveSiteDataHead(userConfig?: UserConfig): HeadConfig[] {
if (userConfig?.appearance ?? true) {
head.push([
'script',
{},
{ id: 'check-dark-light' },
`
;(() => {
const saved = localStorage.getItem('${APPEARANCE_KEY}')
Expand Down
20 changes: 19 additions & 1 deletion src/shared/shared.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
import { SiteData, PageData, LocaleConfig } from '../../types/shared'
import {
SiteData,
PageData,
LocaleConfig,
HeadConfig
} from '../../types/shared'

export type {
SiteData,
Expand Down Expand Up @@ -136,3 +141,16 @@ function cleanRoute(siteData: SiteData, route: string): string {

return route.slice(baseWithoutSuffix.length)
}

function hasTag(head: HeadConfig[], tag: HeadConfig) {
const [tagType, tagAttrs] = tag
const keyAttr = Object.entries(tagAttrs)[0] // First key
if (keyAttr == null) return false
return head.some(
([type, attrs]) => type === tagType && attrs[keyAttr[0]] === keyAttr[1]
)
}

export function mergeHead(prev: HeadConfig[], curr: HeadConfig[]) {
return [...prev.filter((tagAttrs) => !hasTag(curr, tagAttrs)), ...curr]
}