git clone https://github.com/BoryaMogila/koa_react_redux.git; npm install; npm run-script run-with-build;
npm install --save react-helmet
import { Component } from 'react' import Helmet from "react-helmet" class SomeComponent extends Component { render(){ return ( <div> <Helmet htmlAttributes={{"lang": "en", "amp": undefined}} // amp takes no value title="My Title" titleTemplate="MySite.com - %s" defaultTitle="My Default Title" base={{"target": "_blank", "href": "http://mysite.com/"}} meta={[ {"name": "description", "content": "Helmet application"}, {"property": "og:type", "content": "article"} ]} link={[ {"rel": "canonical", "href": "http://mysite.com/example"}, {"rel": "apple-touch-icon", "href": "http://mysite.com/img/apple-touch-icon-57x57.png"}, {"rel": "apple-touch-icon", "sizes": "72x72", "href": "http://mysite.com/img/apple-touch-icon-72x72.png"} ]} script={[ {"src": "http://include.com/pathtojs.js", "type": "text/javascript"}, {"type": "application/ld+json", innerHTML: `{ "@context": "http://schema.org" }`} ]} // </div> ); }
class SomeComponent extends Component { render(){ return ( <div> <Helmet title="My Title" meta={[ {"name": "description", "content": "Helmet application"} ]} link={[ {"rel": "apple-touch-icon", "href": "http://mysite.com/img/apple-touch-icon-57x57.png"}, {"rel": "apple-touch-icon", "sizes": "72x72", "href": "http://mysite.com/img/apple-touch-icon-72x72.png"} ]} base={{"href": "http://mysite.com/"}} /> <AnotherComponent /> </div> ) } } class AnotherComponent extends Component { render(){ return ( <div> <Helmet title="Nested Title" meta={[ {"name": "description", "content": "Nested component"} ]} link={[ {"rel": "apple-touch-icon", "href": "http://mysite.com/img/apple-touch-icon-180x180.png"} ]} base={{"href": "http://mysite.com/blog"}} /> </div> ) } }
<head> <title>Nested Title</title> <meta name="description" content="Nested component"> <link rel="apple-touch-icon" href="http://mysite.com/img/apple-touch-icon-180x180.png"> <base href="http://mysite.com/blog"> </head>
<Helmet defaultTitle="My Site" titleTemplate="My Site - %s" /> <Helmet title="Nested Title" />
<head> <title>My Site - Nested Title</title> </head>
<Helmet script={[{ "type": "application/ld+json", "innerHTML": `{ "@context": "http://schema.org", "@type": "NewsArticle" }` }]} />
<head> <script type="application/ld+json"> { "@context": "http://schema.org", "@type": "NewsArticle" } </script> </head>
<Helmet style={[{ "cssText": ` body { background-color: green; } ` }]} />
<head> <style> body { background-color: green; } </style> </head>
let markup = ReactDOM.renderToString(<Handler />); let head = Helmet.rewind(); const html = ` <!doctype html> <html ${head.htmlAttributes.toString()}> <head> ${head.title.toString()} ${head.meta.toString()} ${head.link.toString()} </head> <body> <div id="content"> ${markup} </div> </body> </html>` // ctx.body = html;
let markup = ReactDOM.renderToString(<Handler />); let head = Helmet.rewind(); function HTML () { const attrs = head.htmlAttributes.toComponent(); return ( <html {...attrs}> <head> {head.title.toComponent()} {head.meta.toComponent()} {head.link.toComponent()} </head> <body> <div id="content"> // React stuff here </div> </body> </html> ); } // ctx.body = ReactDOM.renderToString(<HTML />);
Source: https://habr.com/ru/post/311964/
All Articles