import React from 'react' import PropTypes from 'prop-types' import sideEffect from './side-effect' class Head extends React.Component { static contextTypes = { headManager: PropTypes.object } render () { return null } } const NEXT_HEAD_IDENTIFIER = 'next-head' export function defaultHead (className = NEXT_HEAD_IDENTIFIER) { return [ ] } function reduceComponents (components) { return components .map((component) => React.Children.toArray(component.props.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(Boolean) .filter(unique()) .reverse() .map((c, i) => { const className = (c.props && c.props.className ? c.props.className + ' ' : '') + NEXT_HEAD_IDENTIFIER const key = c.key || i return React.cloneElement(c, { key, 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', 'property'] const ALLOWED_DUPLICATES = ['article:tag', 'og:image', 'og:image:alt', 'og:image:width', 'og:image:height', 'og:image:type', 'og:image:secure_url', 'og:image:url'] /* returns a function for filtering head child elements which shouldn't be duplicated, like