Created
March 25, 2018 17:11
-
-
Save alexnm/9623ad7cd9fba1ba0269a5554653e223 to your computer and use it in GitHub Desktop.
Full server.js example with react-helmet
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* ... */ | |
import Helmet from "react-helmet"; | |
/* ... */ | |
app.get( "/*", ( req, res ) => { | |
/* ... */ | |
const jsx = ( | |
<ReduxProvider store={ store }> | |
<StaticRouter context={ context } location={ req.url }> | |
<Layout /> | |
</StaticRouter> | |
</ReduxProvider> | |
); | |
const reactDom = renderToString( jsx ); | |
const reduxState = store.getState( ); | |
const helmetData = Helmet.renderStatic( ); | |
res.writeHead( 200, { "Content-Type": "text/html" } ); | |
res.end( htmlTemplate( reactDom, reduxState, helmetData ) ); | |
} ); | |
} ); | |
app.listen( 2048 ); | |
function htmlTemplate( reactDom, reduxState, helmetData ) { | |
return ` | |
<!DOCTYPE html> | |
<html> | |
<head> | |
<meta charset="utf-8"> | |
${ helmetData.title.toString( ) } | |
${ helmetData.meta.toString( ) } | |
<title>React SSR</title> | |
</head> | |
/* ... */ | |
`; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment