{"size": 1534, "ext": "js", "max_stars_repo_path": "server/render-server.js", "max_stars_repo_name": "asadsahi/reactnode-fullstack", "max_stars_repo_stars_event_min_datetime": "2018-04-10T12:54:35.000Z", "max_stars_repo_stars_event_max_datetime": "2021-04-25T23:39:03.000Z", "max_issues_repo_path": "server/render-server.js", "max_issues_repo_name": "asadsahi/reactnode-fullstack", "max_issues_repo_issues_event_min_datetime": null, "max_issues_repo_issues_event_max_datetime": null, "max_forks_repo_path": "server/render-server.js", "max_forks_repo_name": "asadsahi/reactnode-fullstack", "max_forks_repo_forks_event_min_datetime": "2019-07-18T15:03:40.000Z", "max_forks_repo_forks_event_max_datetime": "2019-07-18T15:03:40.000Z"}
{"max_stars_count": 6, "max_issues_count": null, "max_forks_count": 1, "avg_line_length": 31.9583333333, "max_line_length": 114, "alphanum_fraction": 0.6186440678}
const React = require('react');
const { renderToString } = require('react-dom/server');
const { StaticRouter } = require('react-router-dom');

const api = require('./features/app/app.controller');
const App = require('../src/containers/App');

const { AppProvider, AppConsumer } = require('../src/contexts/AppContext');

module.exports = async (req, res, file) => {
  try {
    const initialData = await api.content(req);

    const context = {};
    if (context.url) {
      // Somewhere a `<Redirect>` was rendered
      return res.redirect(301, context.url);
    }

    const initialMarkup = renderToString(
      React.createElement(
        AppProvider,
        { value: { appData: initialData } },
        React.createElement(AppConsumer, null, ({ appData }) =>
          React.createElement(StaticRouter, { location: req.url, context }, React.createElement(App, { appData }))
        )
      )
    );

    // we're good, send the response
    const RenderedApp = file
      .replace(
        '{{PRELOADEDSTATE}}',
        `<script>
      window.__PRELOADEDSTATE__ = ${JSON.stringify(initialData).replace(/</g, '\\u003c')}
      window.ssrEnabled = ${true}
      </script>`
      )
      .replace('{{SSR}}', `<div id="app">${initialMarkup}</div>`)
      .replace(/{{app_title}}/g, initialData.content.app_title)
      .replace(/{{app_description}}/g, initialData.content.app_description);

    return res.send(RenderedApp);
  } catch (err) {
    return res.status(500).send(`${err.toString()}\n${err.stack.toString()}`);
  }
};
