2016-10-07 01:57:31 +00:00
|
|
|
import React from 'react'
|
2017-04-10 18:35:26 +00:00
|
|
|
import PropTypes from 'prop-types'
|
2016-10-07 01:57:31 +00:00
|
|
|
import sideEffect from './side-effect'
|
|
|
|
|
|
|
|
class Head extends React.Component {
|
|
|
|
static contextTypes = {
|
2017-04-10 18:35:26 +00:00
|
|
|
headManager: PropTypes.object
|
2016-10-07 01:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
render () {
|
|
|
|
return null
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-11-17 23:06:54 +00:00
|
|
|
export function defaultHead () {
|
|
|
|
return [<meta charSet='utf-8' className='next-head' />]
|
|
|
|
}
|
|
|
|
|
2016-10-07 01:57:31 +00:00
|
|
|
function reduceComponents (components) {
|
|
|
|
return components
|
2018-03-27 18:11:03 +00:00
|
|
|
.map((c) => c.props.children)
|
|
|
|
.map((children) => React.Children.toArray(children))
|
|
|
|
.reduce((a, b) => a.concat(b), [])
|
|
|
|
.reduce((a, b) => {
|
|
|
|
if (React.Fragment && b.type === React.Fragment) {
|
|
|
|
return a.concat(React.Children.toArray(b.props.children))
|
|
|
|
}
|
|
|
|
return a.concat(b)
|
|
|
|
}, [])
|
|
|
|
.reverse()
|
|
|
|
.concat(...defaultHead())
|
|
|
|
.filter((c) => !!c)
|
|
|
|
.filter(unique())
|
|
|
|
.reverse()
|
|
|
|
.map((c) => {
|
|
|
|
const className = (c.props && c.props.className ? c.props.className + ' ' : '') + 'next-head'
|
|
|
|
return React.cloneElement(c, { className })
|
|
|
|
})
|
2016-10-07 01:57:31 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
function mapOnServer (head) {
|
|
|
|
return head
|
|
|
|
}
|
|
|
|
|
|
|
|
function onStateChange (head) {
|
|
|
|
if (this.context && this.context.headManager) {
|
|
|
|
this.context.headManager.updateHead(head)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-03 05:32:47 +00:00
|
|
|
const METATYPES = ['name', 'httpEquiv', 'charSet', 'itemProp', 'property']
|
2016-10-07 01:57:31 +00:00
|
|
|
|
|
|
|
// returns a function for filtering head child elements
|
|
|
|
// which shouldn't be duplicated, like <title/>.
|
|
|
|
|
|
|
|
function unique () {
|
2017-10-31 21:52:51 +00:00
|
|
|
const keys = new Set()
|
2016-10-07 01:57:31 +00:00
|
|
|
const tags = new Set()
|
|
|
|
const metaTypes = new Set()
|
|
|
|
const metaCategories = {}
|
|
|
|
|
|
|
|
return (h) => {
|
2018-02-19 14:47:50 +00:00
|
|
|
if (h.key && h.key.indexOf('.$') === 0) {
|
2017-10-31 21:52:51 +00:00
|
|
|
if (keys.has(h.key)) return false
|
|
|
|
keys.add(h.key)
|
|
|
|
}
|
2016-10-07 01:57:31 +00:00
|
|
|
switch (h.type) {
|
|
|
|
case 'title':
|
|
|
|
case 'base':
|
|
|
|
if (tags.has(h.type)) return false
|
|
|
|
tags.add(h.type)
|
|
|
|
break
|
|
|
|
case 'meta':
|
|
|
|
for (let i = 0, len = METATYPES.length; i < len; i++) {
|
|
|
|
const metatype = METATYPES[i]
|
|
|
|
if (!h.props.hasOwnProperty(metatype)) continue
|
|
|
|
|
2016-10-17 00:00:17 +00:00
|
|
|
if (metatype === 'charSet') {
|
2016-10-07 01:57:31 +00:00
|
|
|
if (metaTypes.has(metatype)) return false
|
|
|
|
metaTypes.add(metatype)
|
|
|
|
} else {
|
|
|
|
const category = h.props[metatype]
|
|
|
|
const categories = metaCategories[metatype] || new Set()
|
|
|
|
if (categories.has(category)) return false
|
|
|
|
categories.add(category)
|
|
|
|
metaCategories[metatype] = categories
|
|
|
|
}
|
|
|
|
}
|
|
|
|
break
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export default sideEffect(reduceComponents, onStateChange, mapOnServer)(Head)
|