I want to use react's server-side-render to only get the initial state for the user. subsequent to that , i'd prefer to use websockets/ajax/client side routing to handle the application.
This is what i have so far
function handleRender(req,res){
getInitialState(id_token)
.then(function(initialState){
const store = createStore(rootReducer,initialState)
console.log('store.getState() ',store.getState())
const html = renderToString(
<Provider store={store}>
<App/>
</Provider>
)
res.send(renderFullPage(html,initialState))
})
.catch(function(err){
console.log('error')
})
}
const renderFullPage = (html,initialState) => {
return `
<!doctype html>
<html>
<head>
<title>Redux Universal</title>
</head>
<body>
<div id="app"><div>${html}</div></div>
<script>
window.__INITIAL_STATE__= ${JSON.stringify(initialState)}
</script>
<script src="/bundle.js"></script>
</body>
</html>`
}
my client side index.js is pretty much standard
const initialState = window.__INITIAL_STATE__
export const store = createStore(rootReducer,initialState)
ReactDOM.render(
<Provider store={store}>
<Router history={browserHistory}>
<Route path="/" component={App} />
<Route path ="/project" component={Project} />
</Router>
</Provider>
,document.getElementById('app')
)
The above works well. when i navigate to /project on the client side. The Project component renders.
However , when i refresh the page at /project - i get a quick render of the / page before the client side router moves to /project.
Also , whenever i switch between routes , i hit the server again which i dont want.
i get that this should happen because refreshing hits the server again causing it to send back
<Provider store={store}>
<App/>
</Provider>
which renders the app component after which react-router renders the /project component again.
is there a way i can just get window.__INITIAL_STATE__ and initial HTML from the server without it having to send the html on every refresh/route change.
I do not want to hit the server on every route change which is what universal react does i believe.
The most important thing for me is to get the initial state for the user on page load. I prefer to have the app as an spa (only hit server for initial render/state and api requests)
handleRendergetting called?app.use(handleRender)