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
|
|
|
|
.map((c) => c.props.children)
|
|
|
|
.map((children) => React.Children.toArray(children))
|
|
|
|
.reduce((a, b) => a.concat(b), [])
|
2017-12-28 01:42:27 +00:00
|
|
|
.reduce((a, b) => {
|
|
|
|
if (React.Fragment && b.type === React.Fragment) {
|
|
|
|
return a.concat(React.Children.toArray(b.props.children))
|
|
|
|
}
|
|
|
|
return a.concat(b)
|
|
|
|
}, [])
|
2016-10-07 01:57:31 +00:00
|
|
|
.reverse()
|
2016-11-17 23:06:54 +00:00
|
|
|
.concat(...defaultHead())
|
2017-05-22 19:58:54 +00:00
|
|
|
.filter((c) => !!c)
|
2016-10-07 01:57:31 +00:00
|
|
|
.filter(unique())
|
|
|
|
.reverse()
|
|
|
|
.map((c) => {
|
2018-01-30 15:40:52 +00:00
|
|
|
const className = (c.props && c.props.className ? c.props.className + ' ' : '') + 'next-head'
|
2016-10-07 01:57:31 +00:00
|
|
|
return React.cloneElement(c, { className })
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
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) => {
|
2017-12-28 01:42:27 +00:00
|
|
|
if (h.key && h.key.startsWith('.$')) {
|
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)
|