├── .gitignore ├── LICENSE ├── README.md ├── index.js └── package.json /.gitignore: -------------------------------------------------------------------------------- 1 | node_modules -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2014 Shawn Lim 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | React Doc Meta 2 | ==================== 3 | 4 | Render meta tags on the server & client for ReactJS. You can render meta tags with any kind of attributes / properties. 5 | 6 | Built with [React Side Effect](https://github.com/gaearon/react-side-effect). 7 | 8 | ==================== 9 | 10 | ## Installation 11 | 12 | ``` 13 | npm install --save react-doc-meta 14 | ``` 15 | 16 | Dependencies: React >= 0.12.0 17 | 18 | ## Example 19 | 20 | ```javascript 21 | var App = React.createClass({ 22 | render: function () { 23 | var tags = [ 24 | {name: "description", content: "lorem ipsum dolor"}, 25 | {itemProp: "name", content: "The Name or Title Here"}, 26 | {itemProp: "description", content: "This is the page description"}, 27 | {itemProp: "image", content: "http://www.example.com/image.jpg"}, 28 | {name: "twitter:card", content: "product"}, 29 | {name: "twitter:site", content: "@publisher_handle"}, 30 | {name: "twitter:title", content: "Page Title"}, 31 | {name: "twitter:description", content: "Page description less than 200 characters"}, 32 | {name: "twitter:creator", content: "@author_handle"}, 33 | {name: "twitter:image", content: "http://www.example.com/image.html"}, 34 | {name: "twitter:data1", content: "$3"}, 35 | {name: "twitter:label1", content: "Price"}, 36 | {name: "twitter:data2", content: "Black"}, 37 | {name: "twitter:label2", content: "Color"}, 38 | {property: "og:title", content: "Title Here"}, 39 | {property: "og:type", content: "article"}, 40 | {property: "og:url", content: "http://www.example.com/"}, 41 | {property: "og:image", content: "http://example.com/image.jpg"}, 42 | {property: "og:description", content: "Description Here"}, 43 | {property: "og:site_name", content: "Site Name, i.e. Moz"}, 44 | {property: "og:price:amount", content: "15.00"}, 45 | {property: "og:price:currency", content: "USD"}, 46 | {weirdfield: "something", content: "really really cool", hello:"world", meh: "hahaha"} 47 | ] 48 | 49 | // DocMeta will construct meta tags with properties & values mirroring the above key-value pairs 50 | return ( 51 | 52 | 53 | 54 | ); 55 | } 56 | }); 57 | ``` 58 | 59 | On the browser the above will produce: 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 | Works for nested components too! 88 | 89 | Also, you don't need to wrap components with ```...```, writing `````` should work just fine. 90 | 91 | 92 | ```javascript 93 | 94 | class JoinPage extends Component { 95 | static propTypes = { 96 | status: PropTypes.string, 97 | user: PropTypes.object 98 | } 99 | 100 | render() { 101 | var tags = [ 102 | {name: "description", content: "test"} 103 | ] 104 | 105 | // only this meta should render in the DOM 106 | var tags2 = [ 107 | {name: "description", content: "test 2"} 108 | ] 109 | 110 | return ( 111 |
112 | 113 | 114 | 115 |
116 | ); 117 | } 118 | } 119 | 120 | class JoinForm extends Component { 121 | 122 | constructor(props) { 123 | super(props); 124 | this.state = { 125 | }; 126 | } 127 | 128 | render(){ 129 | var tags = [ 130 | {name: "description", content: "a nested doc meta"} 131 | ] 132 | 133 | 134 | return ( 135 |
136 | 137 |

Join

138 | Test 139 |
140 | ) 141 | } 142 | } 143 | 144 | ``` 145 | 146 | On the browser the above will produce 147 | 148 | ``` 149 | 150 | ``` 151 | 152 | 153 | ## Server Usage 154 | 155 | If you use it on server, call `DocMeta.rewind()` **after rendering components to string** to retrieve the array of meta tags given to the innermost `DocMeta`. You can then embed this meta tags into HTML page template like this using es6 spread props: 156 | 157 | ```javascript 158 | 159 | //... the rest of the html-document.jsx... 160 | 161 | render() { 162 | const { state, markup, script, css, lang } = this.props; 163 | let metaTags = DocMeta.rewind(); 164 | 165 | return ( 166 | 167 | 168 | { 169 | metaTags.map((tag, index) => 170 | ) 171 | } 172 | 173 | 174 | 175 |
176 | 177 |