head.js 2.0 KB
Newer Older
N
nkzawa 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13
import React from 'react'
import sideEffect from './side-effect'

class Head extends React.Component {
  static contextTypes = {
    headManager: React.PropTypes.object
  }

  render () {
    return null
  }
}

Z
zpnk 已提交
14 15 16 17
export function defaultHead () {
  return [<meta charSet='utf-8' className='next-head' />]
}

N
nkzawa 已提交
18 19 20 21 22 23 24
function reduceComponents (components) {
  return components
  .map((c) => c.props.children)
  .filter((c) => !!c)
  .map((children) => React.Children.toArray(children))
  .reduce((a, b) => a.concat(b), [])
  .reverse()
Z
zpnk 已提交
25
  .concat(...defaultHead())
N
nkzawa 已提交
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
  .filter(unique())
  .reverse()
  .map((c) => {
    const className = (c.className ? c.className + ' ' : '') + 'next-head'
    return React.cloneElement(c, { className })
  })
}

function mapOnServer (head) {
  return head
}

function onStateChange (head) {
  if (this.context && this.context.headManager) {
    this.context.headManager.updateHead(head)
  }
}

const METATYPES = ['name', 'httpEquiv', 'charSet', 'itemProp']

// returns a function for filtering head child elements
// which shouldn't be duplicated, like <title/>.

function unique () {
  const tags = new Set()
  const metaTypes = new Set()
  const metaCategories = {}

  return (h) => {
    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

D
Dan Zajdband 已提交
66
          if (metatype === 'charSet') {
N
nkzawa 已提交
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
            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)