rsnext/packages/next/client/head-manager.js

113 lines
2.9 KiB
JavaScript
Raw Normal View History

const DOMAttributeNames = {
acceptCharset: 'accept-charset',
className: 'class',
htmlFor: 'for',
httpEquiv: 'http-equiv',
}
2016-10-07 03:57:31 +02:00
export default class HeadManager {
constructor() {
2016-12-30 20:14:36 +01:00
this.updatePromise = null
}
updateHead = head => {
const promise = (this.updatePromise = Promise.resolve().then(() => {
2016-12-30 20:14:36 +01:00
if (promise !== this.updatePromise) return
this.updatePromise = null
this.doUpdateHead(head)
}))
}
doUpdateHead(head) {
2016-10-07 03:57:31 +02:00
const tags = {}
head.forEach(h => {
2016-10-07 03:57:31 +02:00
const components = tags[h.type] || []
components.push(h)
tags[h.type] = components
})
this.updateTitle(tags.title ? tags.title[0] : null)
const types = ['meta', 'base', 'link', 'style', 'script']
types.forEach(type => {
2016-10-07 03:57:31 +02:00
this.updateElements(type, tags[type] || [])
})
}
updateTitle(component) {
2018-12-31 19:06:03 +01:00
let title = ''
2016-10-07 03:57:31 +02:00
if (component) {
const { children } = component.props
title = typeof children === 'string' ? children : children.join('')
2016-10-07 03:57:31 +02:00
}
if (title !== document.title) document.title = title
}
updateElements(type, components) {
2016-10-07 03:57:31 +02:00
const headEl = document.getElementsByTagName('head')[0]
const headCountEl = headEl.querySelector('meta[name=next-head-count]')
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'
)
return
}
}
const headCount = Number(headCountEl.content)
const oldTags = []
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).filter(newTag => {
for (let k = 0, len = oldTags.length; k < len; k++) {
const oldTag = oldTags[k]
2016-10-07 03:57:31 +02:00
if (oldTag.isEqualNode(newTag)) {
oldTags.splice(k, 1)
2016-10-07 03:57:31 +02:00
return false
}
}
return true
})
oldTags.forEach(t => t.parentNode.removeChild(t))
newTags.forEach(t => headEl.insertBefore(t, headCountEl))
headCountEl.content = (
headCount -
oldTags.length +
newTags.length
).toString()
2016-10-07 03:57:31 +02:00
}
}
function reactElementToDOM({ type, props }) {
2016-10-07 03:57:31 +02:00
const el = document.createElement(type)
for (const p in props) {
if (!props.hasOwnProperty(p)) continue
if (p === 'children' || p === 'dangerouslySetInnerHTML') continue
2016-10-07 03:57:31 +02:00
// we don't render undefined props to the DOM
if (props[p] === undefined) continue
const attr = DOMAttributeNames[p] || p.toLowerCase()
2016-10-07 03:57:31 +02:00
el.setAttribute(attr, props[p])
}
const { children, dangerouslySetInnerHTML } = props
if (dangerouslySetInnerHTML) {
el.innerHTML = dangerouslySetInnerHTML.__html || ''
} else if (children) {
el.textContent = typeof children === 'string' ? children : children.join('')
2016-10-07 03:57:31 +02:00
}
return el
}