const withHelmet = (App: any, props: any) => { const sheet = new ServerStyleSheet() const body = renderToString(sheet.collectStyles(<App {...props} />)) const styles = sheet.getStyleTags() const helmet = Helmet.renderStatic() const title = helmet.title.toString() const meta = helmet.meta.toString() return template({ title, styles, body, meta }) }
const Html = ({ content, css, store }) => { const helmet = Helmet.renderStatic() const state = `window.__INITIAL_STATE__ = ${serialize(store.getState())}` return ( <html> <head> {helmet.title.toComponent()} {helmet.meta.toComponent()} <style> {'*, *:after, *:before { box-sizing: border-box; }'} {'html, body, main { height: 100%;position: relative;}'} {'body {margin: 0; overflow: hidden; font-family: Roboto, sans-serif; background-color: #FAFAFA}'} </style> <style id="jss-server-side">{css}</style> {helmet.link.toComponent()} </head> <body> <div id="root" dangerouslySetInnerHTML={{ __html: content }} /> <script dangerouslySetInnerHTML={{ __html: state }} /> <script src="bundle.js"></script> </body> </html> ) }
export default function renderHtmlPage(store, html = '') { const head = Helmet.rewind(); const assets = global.webpackIsomorphicTools.assets(); let page = `<!DOCTYPE html><html ${head.htmlAttributes.toString()}>`; page += '<head><meta charset="utf-8"><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">'; page += '<meta http-equiv="X-UA-Compatible" content="IE=edge"><link rel="shortcut icon" type="image/x-icon" href="/favicon.ico">'; page += head.base.toString() + head.title.toString() + head.meta.toString() + head.link.toString(); page += Object.keys(assets.styles).map(style => `<link href="${assets.styles[style]}" rel="stylesheet" type="text/css">` ).join(''); page += `</head><body><div id="app"><div>${html}</div></div>`; if (store) { page += `<script type="text/javascript">window.__INITIAL_STATE__=${serialize(store.getState())}</script>`; } page += Object.keys(assets.javascript).reverse().map(script => `<script src="${assets.javascript[script]}"></script>` ).join(''); page += head.script.toString(); page += '</body></html>'; return page; }
render() { const { component, store } = this.props const head = Helmet.rewind() const htmlAttributes = head.htmlAttributes.toComponent() return ( <html lang={htmlAttributes.lang}> <head> <meta charSet="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1" /> <link rel="shortcut icon" href="/favicon.ico" /> {head.title.toComponent()} {head.meta.toComponent()} {head.link.toComponent()} {head.script.toComponent()} {this.styles} </head> <body> <div id="root" dangerouslySetInnerHTML={{ __html: renderToString(component) }} /> <script dangerouslySetInnerHTML={{ __html: `window.__PRELOADED_STATE__=${serialize(store.getState())};` }} /> {this.scripts} </body> </html> ) }
const titleTag = helmetData.title.toString(); const metaTag = helmetData.meta.toString(); const linkTags = helmetData.link.toString();
<head> ${head.base.toString()} ${head.title.toString()} ${head.meta.toString()} ${head.link.toString()}
const getHTML = (markup, initialState, styleTags) => { const helmet = Helmet.renderStatic(); return ( ` <!DOCTYPE html> <html lang="en"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1"> ${helmet.title.toString()} ${helmet.meta.toString()} ${helmet.link.toString()} <script src="/client.js" defer></script> <script> window.__INITIAL_STATE__ = ${serialize(initialState)} </script> ${styleTags} </head> <body> <div id="app">${markup}</div> </body> </html> ` ); }
html: helmet.htmlAttributes.toString(), head: helmet.title.toString() + helmet.meta.toString() + helmet.link.toString(),
render () { const { assets, content, state } = this.props const head = Helmet.renderStatic() return ( <html lang='en-us'> <head> {head.base.toComponent()} {head.title.toComponent()} {head.meta.toComponent()} {head.link.toComponent()} {head.script.toComponent()} <link rel='shortcut icon' href='/favicon.ico' /> <meta name='viewport' content='width=device-width, initial-scale=1' /> {process.env.NODE_ENV !== 'development' && <link href={`/static/assets${assets['main.css']}`} rel='stylesheet' /> } </head> <body> <div id='root' dangerouslySetInnerHTML={{ __html: content }} /> <script dangerouslySetInnerHTML={{ __html: `window.__data=${state}` }} charSet='UTF-8' /> <script src={process.env.NODE_ENV === 'development' ? `${assets['main.js']}` : `/static/assets${assets['main.js']}`} defer /> </body> </html> ) }
render() { const { component, assetPath, store, type, location } = this.props const content = component ? ReactDOM.renderToString(component) : '' const state = JSON.stringify(store.getState()) const head = Helmet.rewind() return ( <html> <head> { head.title.toComponent() } { head.meta.toComponent() } <meta name="viewport" content="width=device-width, initial-scale=1.0" /> <link href="https://fonts.googleapis.com/css?family=Josefin+Sans:400,400i,600,600i|Lora:700,700i" rel="stylesheet" /> <script dangerouslySetInnerHTML={{ __html: `window.store=${state};` }} charSet="UTF-8" /> </head> <body> <Style rules={ styles } /> <div id="root" dangerouslySetInnerHTML={{ __html: content }} /> <script src="/assets/js/tinymce/tinymce.min.js" /> <script src={`/assets/${type}/bundle.js`} charSet="UTF-8" /> </body> </html> ) }
executePostCss(css) .then(css => { const helmet = Helmet.renderStatic(); head.push(helmet.title.toString()); head.push(helmet.meta.toString()); head.push(helmet.link.toString());
<meta httpEquiv="Content-Type" value="text/html; charset='utf-8'" /> {head.base.toComponent()} {head.title.toComponent()} {head.meta.toComponent()} {head.link.toComponent()}
app.get('*', (req, res) => { const context = {}; const content = ReactDOMServer.renderToString( <StaticRouter location={req.url} context={context}> <App /> </StaticRouter>, ); const helmet = Helmet.renderStatic(); const html = ` <html> <head> ${helmet.title.toString()} ${helmet.meta.toString()} <script src="client_bundle.js" defer></script> <script>window.__INITIAL_DATA__ = undefined;</script> </head> <body> <div id="root">${content}</div> </body> </html> `; res.send(html); });
render () { const {component, store, data} = this.props; const content = component ? ReactDOM.renderToString(component) : ''; const head = Helmet.rewind(); return ( <html lang='en-us'> <head> {head.base.toComponent()} {head.title.toComponent()} {head.meta.toComponent()} {head.link.toComponent()} {head.script.toComponent()} <link rel='shortcut icon' href='/favicon.ico' /> <meta name='viewport' content='width=device-width, initial-scale=1' /> <style type='text/css'>{data.css.join('')}</style> </head> <body> <div id='content' dangerouslySetInnerHTML={{__html: content}} /> <script dangerouslySetInnerHTML={{__html: `window.__data=${serialize(store.getState())};`}} charSet='UTF-8' /> <script src='http://localhost:9001/dist/bundle.js' /> </body> </html> ); }
<head> ${head.base.toString()} ${head.title.toString()} ${head.meta.toString()} ${head.link.toString()}