Skip to content

Instantly share code, notes, and snippets.

@alexnm
Created March 25, 2018 17:11
Show Gist options
  • Save alexnm/9623ad7cd9fba1ba0269a5554653e223 to your computer and use it in GitHub Desktop.
Save alexnm/9623ad7cd9fba1ba0269a5554653e223 to your computer and use it in GitHub Desktop.
Full server.js example with react-helmet
/* ... */
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