1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 | 55x 55x 55x 55x 72x 72x 72x 72x 216x 153x 27x 126x 99x 72x 55x 50x 55x 20x 55x 29x 55x 1x 55x 35x 35x 30x 35x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 82x 82x 82x 80x 80x 20x 20x 60x 98x 98x 118x 118x 82x 41x 6x 6x 6x 3x 6x 55x 55x 55x 4x 4x 2x | import React, { Component } from 'react'; import PropTypes from 'prop-types'; import withSideEffect from 'react-side-effect'; import { clone, defaults } from './utils'; import { canUseDOM, insertDocumentMeta } from './dom'; function reducePropsTostate(propsList) { const props = {}; const dynamicProps = { title: undefined, description: undefined, canonical: undefined }; let extend = true; for (var i = propsList.length - 1; extend && i >= 0; i--) { extend = propsList[i].hasOwnProperty('extend'); const _props = propsList[i]; const _cloned = clone(propsList[i]); ['title', 'description', 'canonical'].forEach(key => { if (_props.hasOwnProperty(key)) { if (typeof _props[key] === 'function') { dynamicProps[key] = _props[key](dynamicProps[key]); } else if (dynamicProps[key] === undefined) { dynamicProps[key] = _props[key]; } } }); defaults(props, _cloned); } if (typeof dynamicProps.title === 'string') { props.title = dynamicProps.title; } if (typeof dynamicProps.description === 'string') { defaults(props, { meta: { name: { description: dynamicProps.description } } }); } if (typeof dynamicProps.canonical === 'string') { defaults(props, { link: { rel: { canonical: dynamicProps.canonical } } }); } if (props.auto && props.auto.ograph) { ograph(props); } return props; } function handleStateChangeOnClient(props) { Eif (canUseDOM) { if (typeof props.title === 'string') { document.title = props.title; } insertDocumentMeta(getTags(props)); } } function ograph(p) { Iif (!p.meta) { p.meta = {}; } Eif (!p.meta.property) { p.meta.property = {}; } const group = p.meta.property; Eif (group) { Eif (p.title && !group['og:title']) { group['og:title'] = p.title; } Eif (p.hasOwnProperty('description') && !group['og:description']) { group['og:description'] = p.description; } Eif (p.hasOwnProperty('canonical') && !group['og:url']) { group['og:url'] = p.canonical; } } return p; } function parseTags(tagName, props = {}) { const tags = []; const contentKey = tagName === 'link' ? 'href' : 'content'; Object.keys(props).forEach(groupKey => { const group = props[groupKey]; if (typeof group === 'string') { tags.push({ tagName, [groupKey]: group }); return; } Object.keys(group).forEach(key => { const values = Array.isArray(group[key]) ? group[key] : [group[key]]; values.forEach(value => { Eif (value !== null) { tags.push({ tagName, [groupKey]: key, [contentKey]: value }); } }); }); }); return tags; } function getTags(_props) { return [].concat( _props.base ? [{ tagName: 'base', href: _props.base }] : [], parseTags('meta', _props.meta), parseTags('link', _props.link) ); } export function render(meta = {}) { let i = 0; const tags = []; function renderTag(entry) { const { tagName, ...attr } = entry; if (tagName === 'meta') { return <meta {...attr} key={i++} data-rdm />; } if (tagName === 'link') { return <link {...attr} key={i++} data-rdm />; } return null; } if (meta.title) { tags.push(<title key={i++}>{meta.title}</title>); } return getTags(meta).reduce((acc, entry) => { acc.push(renderTag(entry)); return acc; }, tags); } class DocumentMeta extends Component { static propTypes = { title: PropTypes.oneOfType([PropTypes.string, PropTypes.func]), description: PropTypes.oneOfType([PropTypes.string, PropTypes.func]), canonical: PropTypes.oneOfType([PropTypes.string, PropTypes.func]), base: PropTypes.string, meta: PropTypes.objectOf( PropTypes.oneOfType([ PropTypes.string, PropTypes.objectOf( PropTypes.oneOfType([ PropTypes.string, PropTypes.arrayOf(PropTypes.string) ]) ) ]) ), link: PropTypes.objectOf( PropTypes.objectOf( PropTypes.oneOfType([ PropTypes.string, PropTypes.arrayOf(PropTypes.string) ]) ) ), auto: PropTypes.objectOf(PropTypes.bool) }; render() { const { children } = this.props; const count = React.Children.count(children); return count === 1 ? ( React.Children.only(children) ) : count ? ( <div>{this.props.children}</div> ) : null; } } const DocumentMetaWithSideEffect = withSideEffect( reducePropsTostate, handleStateChangeOnClient )(DocumentMeta); DocumentMetaWithSideEffect.renderAsReact = function rewindAsReact() { return render(DocumentMetaWithSideEffect.rewind()); }; export default DocumentMetaWithSideEffect; |