4. Redux Universal @nikgraf
Server side rendering
• (Client) Request the website's HTML
• (Server) Execute the Code
• (Server) Collect the data
• (Server) Render the page in the backend
• (Server) Serve the page with content
• (Client) -> Render the content
8. Redux Universal @nikgraf
Client side rendering
• (Client) Request the website's HTML
• (Server) Serve the page without content
• (Client) Request JavaScript code based on sources in the HTML
• (Server) Serve the JavaScript code
• (Client) Load & execute JavaScript
• (Client) -> Render a loading page
• (Client) Request data based on the executed code
• (Server) Collect and serve the data
• (Client) -> Render the content
9. Redux Universal @nikgraf
Isomorphic Universal rendering
• (Client) Request the website's HTML
• (Server) Execute the JavaScript Code
• (Server) Collect the data
• (Server) Render the page in the backend
• (Server) Serve the page with content
• (Client) -> Render the content
• (Client) Request JavaScript code based on sources in the HTML
• (Server) Serve the JavaScript code
• (Client) Load & execute JavaScript
10. Redux Universal @nikgraf
Render on the Server
(React)
import React, { Component } from 'react';
import { renderToString } from 'react-dom/server';
class MyComponent extends Component {
render() {
return <div>Hello World</div>;
}
}
const html = renderToString(MyComponent);
11. Redux Universal @nikgraf
Render on the Server
(React/Redux)
import { renderToString } from 'react-dom/server';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import rootReducer from '../reducers';
import App from '../components/App';
const store = createStore(rootReducer);
const html = renderToString(
<Provider store={store}>
<App/>
</Provider>
);
12. Redux Universal @nikgraf
Redux on the Server I/III
import ReactDOM from 'react-dom';
import { createStore } from 'redux';
import { Provider } from 'react-redux';
import rootReducer from '../reducers';
import App from '../components/App';
const initialState = window.__INITIAL_STATE__;
const store = createStore(rootReducer, initialState);
const rootElement = document.getElementById('app');
ReactDOM.render(
<Provider store={store}><App/></Provider>,
rootElement
);