2016-11-21 11:46:49 +00:00
|
|
|
import HTMLDOMPropertyConfig from 'react-dom/lib/HTMLDOMPropertyConfig'
|
2016-10-07 01:57:31 +00:00
|
|
|
|
|
|
|
const DEFAULT_TITLE = ''
|
|
|
|
|
|
|
|
export default class HeadManager {
|
2016-12-02 03:51:56 +00:00
|
|
|
constructor () {
|
2016-10-28 14:39:20 +00:00
|
|
|
this.requestId = null
|
|
|
|
}
|
|
|
|
|
2016-10-07 01:57:31 +00:00
|
|
|
updateHead (head) {
|
2016-10-28 14:39:20 +00:00
|
|
|
// perform batch update
|
|
|
|
window.cancelAnimationFrame(this.requestId)
|
|
|
|
this.requestId = window.requestAnimationFrame(() => {
|
|
|
|
this.requestId = null
|
|
|
|
this.doUpdateHead(head)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
doUpdateHead (head) {
|
2016-10-07 01:57:31 +00:00
|
|
|
const tags = {}
|
|
|
|
head.forEach((h) => {
|
|
|
|
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) => {
|
|
|
|
this.updateElements(type, tags[type] || [])
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
updateTitle (component) {
|
|
|
|
let title
|
|
|
|
if (component) {
|
|
|
|
const { children } = component.props
|
2016-10-17 00:00:17 +00:00
|
|
|
title = typeof children === 'string' ? children : children.join('')
|
2016-10-07 01:57:31 +00:00
|
|
|
} else {
|
|
|
|
title = DEFAULT_TITLE
|
|
|
|
}
|
|
|
|
if (title !== document.title) document.title = title
|
|
|
|
}
|
|
|
|
|
|
|
|
updateElements (type, components) {
|
|
|
|
const headEl = document.getElementsByTagName('head')[0]
|
|
|
|
const oldTags = Array.prototype.slice.call(headEl.querySelectorAll(type + '.next-head'))
|
|
|
|
const newTags = components.map(reactElementToDOM).filter((newTag) => {
|
|
|
|
for (let i = 0, len = oldTags.length; i < len; i++) {
|
|
|
|
const oldTag = oldTags[i]
|
|
|
|
if (oldTag.isEqualNode(newTag)) {
|
|
|
|
oldTags.splice(i, 1)
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
})
|
|
|
|
|
|
|
|
oldTags.forEach((t) => t.parentNode.removeChild(t))
|
|
|
|
newTags.forEach((t) => headEl.appendChild(t))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
function reactElementToDOM ({ type, props }) {
|
|
|
|
const el = document.createElement(type)
|
|
|
|
for (const p in props) {
|
|
|
|
if (!props.hasOwnProperty(p)) continue
|
2016-10-17 00:00:17 +00:00
|
|
|
if (p === 'children' || p === 'dangerouslySetInnerHTML') continue
|
2016-10-07 01:57:31 +00:00
|
|
|
|
|
|
|
const attr = HTMLDOMPropertyConfig.DOMAttributeNames[p] || p.toLowerCase()
|
|
|
|
el.setAttribute(attr, props[p])
|
|
|
|
}
|
|
|
|
|
|
|
|
const { children, dangerouslySetInnerHTML } = props
|
|
|
|
if (dangerouslySetInnerHTML) {
|
|
|
|
el.innerHTML = dangerouslySetInnerHTML.__html || ''
|
|
|
|
} else if (children) {
|
2016-10-17 00:00:17 +00:00
|
|
|
el.textContent = typeof children === 'string' ? children : children.join('')
|
2016-10-07 01:57:31 +00:00
|
|
|
}
|
|
|
|
return el
|
|
|
|
}
|