<commit_before>import React from 'react';
import ReactDOM from 'react-dom/server';
import Helmet from 'react-helmet';

import { match, RouterContext } from 'react-router'

import { applyMiddleware, createStore } from 'redux';
import { Provider } from 'react-redux';

import reducers from './reducers';
import promiseMiddleware from './middlewares/promiseMiddleware';
import routes from './routes';
import prefetchComponentData from './utils/prefetchComponentData';

export default function(req, res) {

  const store = applyMiddleware(promiseMiddleware)(createStore)(reducers);

  match({ routes, location: req.url }, (error, redirectLocation, renderProps) => {
    
    if (error) {
      res.status(500).send(error.message)
    } else if (redirectLocation) {
      res.redirect(302, redirectLocation.pathname + redirectLocation.search)
    } else if (renderProps) {
      prefetchComponentData(store.dispatch, renderProps.components, renderProps.params)
        .then(renderHTML)
        .then((html) => res.status(200).send(html))
        .catch(err => res.end(err.message));
    } else {
      res.status(404).send('Not found')
    }

    function renderHTML() {

        const initialState = store.getState();
        let head = Helmet.rewind();

        const renderedComponent = ReactDOM.renderToString(
            <Provider store={store}>
              <RouterContext {...renderProps} />
            </Provider>
        );

        const HTML = `
            <!DOCTYPE html>
            <html lang="en">
              <head>
                <meta charset="UTF-8">
                <meta name="viewport" content="width=device-width, initial-scale=1">
                ${head.title.toString()}
                ${head.meta.toString()}
                ${head.link.toString()}
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
              </head>
              <body>
                <div id="app">${renderedComponent}</div>
                <script type="application/javascript">
                   window.__INITIAL_STATE__ = ${JSON.stringify(initialState)};
                </script>
                <script src="/assets/bundle.js"></script>
              </body>
            </html>    
        `;

        return HTML;<commit_msg>fix bug about changing title by moving Helmet.rewind() under renderToString()<commit_after>import React from 'react';
import ReactDOM from 'react-dom/server';
import Helmet from 'react-helmet';

import { match, RouterContext } from 'react-router'

import { applyMiddleware, createStore } from 'redux';
import { Provider } from 'react-redux';

import reducers from './reducers';
import promiseMiddleware from './middlewares/promiseMiddleware';
import routes from './routes';
import prefetchComponentData from './utils/prefetchComponentData';

export default function(req, res) {

  const store = applyMiddleware(promiseMiddleware)(createStore)(reducers);

  match({ routes, location: req.url }, (error, redirectLocation, renderProps) => {
    
    if (error) {
      res.status(500).send(error.message)
    } else if (redirectLocation) {
      res.redirect(302, redirectLocation.pathname + redirectLocation.search)
    } else if (renderProps) {
      prefetchComponentData(store.dispatch, renderProps.components, renderProps.params)
        .then(renderHTML)
        .then((html) => res.status(200).send(html))
        .catch(err => res.end(err.message));
    } else {
      res.status(404).send('Not found')
    }

    function renderHTML() {

        const initialState = store.getState();

        const renderedComponent = ReactDOM.renderToString(
            <Provider store={store}>
              <RouterContext {...renderProps} />
            </Provider>
        );

        let head = Helmet.rewind();

        const HTML = `
            <!DOCTYPE html>
            <html lang="en">
              <head>
                <meta charset="UTF-8">
                <meta name="viewport" content="width=device-width, initial-scale=1">
                ${head.title.toString()}
                ${head.meta.toString()}
                ${head.link.toString()}
                <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/css/bootstrap.min.css">
              </head>
              <body>
                <div id="app">${renderedComponent}</div>
                <script type="application/javascript">
                   window.__INITIAL_STATE__ = ${JSON.stringify(initialState)};
                </script>
                <script src="/assets/bundle.js"></script>
              </body>
            </html>    
        `;

        return HTML;
{'fraction_non_alphanumeric': 0.1341750481489407, 'fraction_numerical': 0.007275839931521507, 'mean_word_length': 2.3290598290598292, 'pattern_counts': {'":': 0, '<': 41, '<?xml version=': 0, '>': 47, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3063383', 'n_tokens_mistral': 1298, 'n_tokens_neox': 1241, 'n_words': 266}