2020-12-01 19:10:16 +01:00
|
|
|
export const DOMAttributeNames: Record<string, string> = {
|
2016-12-15 07:05:20 +01:00
|
|
|
acceptCharset: 'accept-charset',
|
|
|
|
className: 'class',
|
|
|
|
htmlFor: 'for',
|
2019-11-11 04:24:53 +01:00
|
|
|
httpEquiv: 'http-equiv',
|
2021-01-04 20:57:52 +01:00
|
|
|
noModule: 'noModule',
|
2016-12-15 07:05:20 +01:00
|
|
|
}
|
|
|
|
|
2020-08-13 05:54:48 +02:00
|
|
|
function reactElementToDOM({ type, props }: JSX.Element): HTMLElement {
|
2021-01-04 20:57:52 +01:00
|
|
|
const el: HTMLElement = document.createElement(type)
|
2016-10-07 03:57:31 +02:00
|
|
|
for (const p in props) {
|
|
|
|
if (!props.hasOwnProperty(p)) continue
|
2016-10-17 02:00:17 +02:00
|
|
|
if (p === 'children' || p === 'dangerouslySetInnerHTML') continue
|
2016-10-07 03:57:31 +02:00
|
|
|
|
2020-02-03 20:55:14 +01:00
|
|
|
// we don't render undefined props to the DOM
|
|
|
|
if (props[p] === undefined) continue
|
|
|
|
|
2016-12-15 07:05:20 +01:00
|
|
|
const attr = DOMAttributeNames[p] || p.toLowerCase()
|
2021-01-04 20:57:52 +01:00
|
|
|
if (
|
|
|
|
type === 'script' &&
|
|
|
|
(attr === 'async' || attr === 'defer' || attr === 'noModule')
|
|
|
|
) {
|
|
|
|
;(el as HTMLScriptElement)[attr] = !!props[p]
|
|
|
|
} else {
|
|
|
|
el.setAttribute(attr, props[p])
|
|
|
|
}
|
2016-10-07 03:57:31 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const { children, dangerouslySetInnerHTML } = props
|
|
|
|
if (dangerouslySetInnerHTML) {
|
|
|
|
el.innerHTML = dangerouslySetInnerHTML.__html || ''
|
|
|
|
} else if (children) {
|
2020-09-29 01:12:07 +02:00
|
|
|
el.textContent =
|
|
|
|
typeof children === 'string'
|
|
|
|
? children
|
|
|
|
: Array.isArray(children)
|
|
|
|
? children.join('')
|
|
|
|
: ''
|
2016-10-07 03:57:31 +02:00
|
|
|
}
|
|
|
|
return el
|
|
|
|
}
|
2020-02-26 13:56:13 +01:00
|
|
|
|
2021-01-05 16:11:37 +01:00
|
|
|
function updateElements(type: string, components: JSX.Element[]): void {
|
2020-02-26 13:56:13 +01:00
|
|
|
const headEl = document.getElementsByTagName('head')[0]
|
2020-11-10 22:35:47 +01:00
|
|
|
const headCountEl: HTMLMetaElement = headEl.querySelector(
|
|
|
|
'meta[name=next-head-count]'
|
|
|
|
) as HTMLMetaElement
|
|
|
|
if (process.env.NODE_ENV !== 'production') {
|
|
|
|
if (!headCountEl) {
|
|
|
|
console.error(
|
|
|
|
'Warning: next-head-count is missing. https://err.sh/next.js/next-head-count-missing'
|
|
|
|
)
|
2020-02-26 13:56:13 +01:00
|
|
|
return
|
|
|
|
}
|
2020-11-10 22:35:47 +01:00
|
|
|
}
|
2020-02-26 13:56:13 +01:00
|
|
|
|
2020-11-10 22:35:47 +01:00
|
|
|
const headCount = Number(headCountEl.content)
|
|
|
|
const oldTags: Element[] = []
|
2020-09-09 03:41:04 +02:00
|
|
|
|
2020-11-10 22:35:47 +01:00
|
|
|
for (
|
|
|
|
let i = 0, j = headCountEl.previousElementSibling;
|
|
|
|
i < headCount;
|
|
|
|
i++, j = j!.previousElementSibling
|
|
|
|
) {
|
|
|
|
if (j!.tagName.toLowerCase() === type) {
|
|
|
|
oldTags.push(j!)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
const newTags = (components.map(reactElementToDOM) as HTMLElement[]).filter(
|
|
|
|
(newTag) => {
|
|
|
|
for (let k = 0, len = oldTags.length; k < len; k++) {
|
|
|
|
const oldTag = oldTags[k]
|
|
|
|
if (oldTag.isEqualNode(newTag)) {
|
|
|
|
oldTags.splice(k, 1)
|
|
|
|
return false
|
|
|
|
}
|
2020-02-26 13:56:13 +01:00
|
|
|
}
|
2020-11-10 22:35:47 +01:00
|
|
|
return true
|
2020-02-26 13:56:13 +01:00
|
|
|
}
|
2020-11-10 22:35:47 +01:00
|
|
|
)
|
2020-02-26 13:56:13 +01:00
|
|
|
|
2020-11-10 22:35:47 +01:00
|
|
|
oldTags.forEach((t) => t.parentNode!.removeChild(t))
|
|
|
|
newTags.forEach((t) => headEl.insertBefore(t, headCountEl))
|
|
|
|
headCountEl.content = (headCount - oldTags.length + newTags.length).toString()
|
2020-02-26 13:56:13 +01:00
|
|
|
}
|
|
|
|
|
2021-01-05 16:11:37 +01:00
|
|
|
export default function initHeadManager(): {
|
|
|
|
mountedInstances: Set<unknown>
|
|
|
|
updateHead: (head: JSX.Element[]) => void
|
|
|
|
} {
|
2020-08-13 05:54:48 +02:00
|
|
|
let updatePromise: Promise<void> | null = null
|
2020-02-26 13:56:13 +01:00
|
|
|
|
2020-06-12 00:09:06 +02:00
|
|
|
return {
|
|
|
|
mountedInstances: new Set(),
|
2020-08-13 05:54:48 +02:00
|
|
|
updateHead: (head: JSX.Element[]) => {
|
2020-06-12 00:09:06 +02:00
|
|
|
const promise = (updatePromise = Promise.resolve().then(() => {
|
|
|
|
if (promise !== updatePromise) return
|
2020-02-26 13:56:13 +01:00
|
|
|
|
2020-06-12 00:09:06 +02:00
|
|
|
updatePromise = null
|
2020-11-10 22:35:47 +01:00
|
|
|
const tags: Record<string, JSX.Element[]> = {}
|
|
|
|
|
|
|
|
head.forEach((h) => {
|
|
|
|
const components = tags[h.type] || []
|
|
|
|
components.push(h)
|
|
|
|
tags[h.type] = components
|
|
|
|
})
|
|
|
|
|
|
|
|
const titleComponent = tags.title ? tags.title[0] : null
|
|
|
|
let title = ''
|
|
|
|
if (titleComponent) {
|
|
|
|
const { children } = titleComponent.props
|
|
|
|
title =
|
|
|
|
typeof children === 'string'
|
|
|
|
? children
|
|
|
|
: Array.isArray(children)
|
|
|
|
? children.join('')
|
|
|
|
: ''
|
|
|
|
}
|
|
|
|
if (title !== document.title) document.title = title
|
|
|
|
;['meta', 'base', 'link', 'style', 'script'].forEach((type) => {
|
|
|
|
updateElements(type, tags[type] || [])
|
|
|
|
})
|
2020-06-12 00:09:06 +02:00
|
|
|
}))
|
|
|
|
},
|
2020-02-26 13:56:13 +01:00
|
|
|
}
|
|
|
|
}
|