import { createStore } from 'redux'
import reducers from '../../app/store/reducers';
import * as dispatchTypes from '../../app/store/dispatchTypes';
import App from '../app/components/App.jsx';
app.get('/home', (req, res) => {
// делаем редакс стор
const Store = createStore(reducers, {});
// вкидываем в него что хотим (нужно)
Store.dispatch({
type : "USER",
payload : req.user
})
// получаем все данные из бд или откуда хотим
var ContextData ={
page : {
title : 'Qwerty'
}
}
// рендерим
var template = ReactDOMServer.renderToString(
<Provider store={Store}>
<StaticRouter
location={req.url}
context={ContextData}>
<App/>
</StaticRouter>
</Provider>
);
// отдаем шаблонизатору
return res.render('content', {
preloadState : Store.getState(),
content : template
});
});
const composeEnhancers =
typeof window === 'object' &&
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__ ?
window.__REDUX_DEVTOOLS_EXTENSION_COMPOSE__({
// Specify extension’s options like name, actionsBlacklist, actionsCreators, serialize...
}) : compose;
git clone url folder
cd folder
npm install
// правим конфиги, накатываем левый софт
npm run build // собираем prod версию css/js (минификация все дела, феншуй)
pm2 start process.config.js // запустили
module.exports = {
apps : [
{
name: "feat",
script: "./server/start.js",
interpreter : "babel-node",
instance_var : "INSTANCE_ID",
env : {
"NODE_ENV" : "production",
"PORT" : 8000
}
}
]
}
git fetch --tags
git checkout v1.2222.043.beta
npm run build // пересобираешь клиент скрипты
pm2 restart feat
npm run dev
"dev": "cross-env NODE_ENV=development babel-node server/start.js",
npm run watch
"watch": "cross-env NODE_ENV=development webpack --watch --progress --hide-modules --config ./app/webpack.config.js",
import { createStore } from 'redux'
import reducers from '../../app/store/reducers';
import * as dispatchTypes from '../../app/store/dispatchTypes';
export default (req, res, next) => {
req._reduxStore = createStore(reducers, {});
req._reduxStore.dispatch({
type : dispatchTypes.APP_URL,
payload : config.get("app.url")
})
req._reduxStore.dispatch({
type : dispatchTypes.USERS_RECEIVE_UR,
payload : req.user // пример
})
return next();
}
import { Page } from '../database/models';
import ReactSSRender from '../utils/reactSSrender';
const show = (req, res, next) => {
Page.find(1)
.then(page => {
// отдаем на рендер шаблонизатору
res.status(200).render('content', {
preloadState : req._reduxStore.getState(),
content : ReactSSRender(req._reduxStore, req.url, {
// context
page
});
});
}).catch(err => {
logger(LOG_ERR).write(err);
// как-нибудь обрабатываем
});
};
body
#appMount
block content
script.
window.__PRELOADED_STATE__ = !{JSON.stringify(preloadedState || {}).replace(/</g, '\\u003c')}
// ...
render() {
return (
<Layout>
<Switch>
<Route exact path="/" component={RouteContainers.Home} />
{/* Page */}
<Route path="/pages" component={RouteContainers.Page} />
</Switch>
</Layout>
);
}
// ...