Advertisement
Guest User

Untitled

a guest
Apr 18th, 2019
98
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import React from 'react';
  2. import ReactDOM from 'react-dom/server';
  3. import Helmet from 'react-helmet';
  4. import { flushChunkNames } from 'react-universal-component/server';
  5. import flushChunks from 'webpack-flush-chunks';
  6. import { Provider } from 'react-redux';
  7. import { StaticRouter } from 'react-router';
  8.  
  9. import createDocument from './document';
  10. import configureStore from '../shared/redux/store/configureStore';
  11. import App from '../shared/App';
  12.  
  13. export default ({ clientStats }) => async (req, res) => {
  14.     const preloadedState = {};
  15.     const store = configureStore(preloadedState);
  16.     const context = {};
  17.     const app = (
  18.         <Provider store={store}>
  19.             <StaticRouter location={req.url} context={context}>
  20.                 <App/>
  21.             </StaticRouter>
  22.         </Provider>
  23.     );
  24.  
  25.     const appString = ReactDOM.renderToString(app);
  26.     const helmet = Helmet.renderStatic();
  27.     const chunkNames = flushChunkNames();
  28.     const { js, styles } = flushChunks(clientStats, { chunkNames });
  29.     const document = createDocument({
  30.         appString,
  31.         js,
  32.         styles,
  33.         helmet,
  34.         preloadedState: JSON.stringify(preloadedState)
  35.     });
  36.  
  37.     if (context.url) {
  38.         res.writeHead(301, {
  39.             Location: context.url
  40.         });
  41.         res.end();
  42.     } else {
  43.  
  44.         res.set('Content-Type', 'text/html').end(document);
  45.     }
  46. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement