SlideShare a Scribd company logo
1 of 25
Download to read offline
Redux Universal

by Sebastian Siemssen & Nik Graf
Redux Universal @nikgraf
Nik Graf
@nikgraf

nik@nikgraf.com
Creator of Belle
Working with StarterSquad
History
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
Redux Universal @nikgraf
?
Universal Rendering
Redux Universal @nikgraf
Benefits
• Search Engine Indexability
• Code Reusability & Maintainability
• Faster Perceived Load Time !?
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
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
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);
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>
);
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
);
Redux Universal @nikgraf
Redux on the Server II/III
const renderFullPage = (html, initialState) => {
return `
<!doctype html>
<html>
<body>
<div id="app">${html}</div>
<script>
window.__INITIAL_STATE__ =

${JSON.stringify(initialState)};
</script>
<script src="/static/bundle.js"></script>
</body>
</html>
`;
}
Redux Universal @nikgraf
Redux on the Server III/III
app.use((request, response) => {
const store = createStore(rootReducer);
const html = renderToString(
<Provider store={store}>
<App/>
</Provider>
);
const state = store.getState();
response.send(renderFullPage(html, state));
});
Redux Universal @nikgraf
What about async
actions?
Redux Universal @nikgraf
Async Action Example
import fetch from 'isomorphic-fetch';
export function fetchFriends() {
return dispatch => {
dispatch({ type: 'FETCH_FRIENDS' });
return fetch('http://localhost/api/friends/')
.then(response => response.json())
.then(json => {
dispatch({ type: 'RECEIVE_FRIENDS', payload: json });
});
};
}
Redux Universal @nikgraf
Issue with Async
Async Action
Render
Render
Async Action
Redux Universal @nikgraf
Before using redux-universal
import { createStore, applyMiddleware } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from '../reducers';
const createStoreWithMiddleware = applyMiddleware(
thunk
)(createStore);
export default function configureStore(initialState) {
return createStoreWithMiddleware(rootReducer, initialState);
}
Redux Universal @nikgraf
Using redux-universal
import { createStore } from 'redux';
import thunk from 'redux-thunk';
import rootReducer from '../reducers';
const applyMiddleware = __SERVER__ ?
require('redux-universal') :
require('redux').applyMiddleware;
const createStoreWithMiddleware = applyMiddleware(
thunk
)(createStore);
export default function configureStore(initialState) {
return createStoreWithMiddleware(rootReducer, initialState);
}
Redux Universal @nikgraf
Before using redux-universal
const html = renderToString(
<Provider store={store}>
<App/>
</Provider>
);
const state = store.getState();
res.send(renderFullPage(html, state));
Redux Universal @nikgraf
Using redux-universal
store.renderUniversal(
renderToString,
<Provider store={store}>
<App/>
</Provider>
)
.then(({ output }) => {
const state = store.getState();
response.send(renderFullPage(output, state));
})
.catch(({ output, error }) => {
console.warn(error.message);
});
Redux Universal @nikgraf
How it works
-> render
-> wait for promise
-> render
-> resolve
Redux Universal @nikgraf
How it works
-> render
-> wait for promiseA
-> render
-> wait for promiseB
-> render
-> resolve
Redux Universal @nikgraf
Fin


Checkout Redux Universal



https://github.com/reducks/redux-universal

More Related Content

What's hot

React.js in real world apps.
React.js in real world apps. React.js in real world apps.
React.js in real world apps. Emanuele DelBono
 
A Brief Introduction to React.js
A Brief Introduction to React.jsA Brief Introduction to React.js
A Brief Introduction to React.jsDoug Neiner
 
ProvJS: Six Months of ReactJS and Redux
ProvJS:  Six Months of ReactJS and ReduxProvJS:  Six Months of ReactJS and Redux
ProvJS: Six Months of ReactJS and ReduxThom Nichols
 
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥Remo Jansen
 
Better React state management with Redux
Better React state management with ReduxBetter React state management with Redux
Better React state management with ReduxMaurice De Beijer [MVP]
 
Academy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & ToolingAcademy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & ToolingBinary Studio
 
React state managmenet with Redux
React state managmenet with ReduxReact state managmenet with Redux
React state managmenet with ReduxVedran Blaženka
 
Better web apps with React and Redux
Better web apps with React and ReduxBetter web apps with React and Redux
Better web apps with React and ReduxAli Sa'o
 
Rethinking Best Practices
Rethinking Best PracticesRethinking Best Practices
Rethinking Best Practicesfloydophone
 
Let's discover React and Redux with TypeScript
Let's discover React and Redux with TypeScriptLet's discover React and Redux with TypeScript
Let's discover React and Redux with TypeScriptMathieu Savy
 
React JS and Redux
React JS and ReduxReact JS and Redux
React JS and ReduxGlib Kechyn
 
React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)
React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)
React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)Binary Studio
 
React Lifecycle and Reconciliation
React Lifecycle and ReconciliationReact Lifecycle and Reconciliation
React Lifecycle and ReconciliationZhihao Li
 
React for Dummies
React for DummiesReact for Dummies
React for DummiesMitch Chen
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux IntroductionNikolaus Graf
 

What's hot (20)

React.js in real world apps.
React.js in real world apps. React.js in real world apps.
React.js in real world apps.
 
React / Redux Architectures
React / Redux ArchitecturesReact / Redux Architectures
React / Redux Architectures
 
A Brief Introduction to React.js
A Brief Introduction to React.jsA Brief Introduction to React.js
A Brief Introduction to React.js
 
ProvJS: Six Months of ReactJS and Redux
ProvJS:  Six Months of ReactJS and ReduxProvJS:  Six Months of ReactJS and Redux
ProvJS: Six Months of ReactJS and Redux
 
React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥React + Redux + TypeScript === ♥
React + Redux + TypeScript === ♥
 
Better React state management with Redux
Better React state management with ReduxBetter React state management with Redux
Better React state management with Redux
 
Academy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & ToolingAcademy PRO: React JS. Redux & Tooling
Academy PRO: React JS. Redux & Tooling
 
React state managmenet with Redux
React state managmenet with ReduxReact state managmenet with Redux
React state managmenet with Redux
 
React & Redux
React & ReduxReact & Redux
React & Redux
 
Better web apps with React and Redux
Better web apps with React and ReduxBetter web apps with React and Redux
Better web apps with React and Redux
 
Rethinking Best Practices
Rethinking Best PracticesRethinking Best Practices
Rethinking Best Practices
 
React js
React jsReact js
React js
 
Let's discover React and Redux with TypeScript
Let's discover React and Redux with TypeScriptLet's discover React and Redux with TypeScript
Let's discover React and Redux with TypeScript
 
React & redux
React & reduxReact & redux
React & redux
 
React JS and Redux
React JS and ReduxReact JS and Redux
React JS and Redux
 
React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)
React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)
React vs Angular: ups & downs (speaker Oleksandr Kovalov, Binary Studio)
 
React Lifecycle and Reconciliation
React Lifecycle and ReconciliationReact Lifecycle and Reconciliation
React Lifecycle and Reconciliation
 
React for Dummies
React for DummiesReact for Dummies
React for Dummies
 
React and redux
React and reduxReact and redux
React and redux
 
React + Redux Introduction
React + Redux IntroductionReact + Redux Introduction
React + Redux Introduction
 

Similar to Redux Universal

Building Universal Web Apps with React ForwardJS 2017
Building Universal Web Apps with React ForwardJS 2017Building Universal Web Apps with React ForwardJS 2017
Building Universal Web Apps with React ForwardJS 2017Elyse Kolker Gordon
 
Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...
Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...
Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...OdessaJS Conf
 
Workshop 27: Isomorphic web apps with ReactJS
Workshop 27: Isomorphic web apps with ReactJSWorkshop 27: Isomorphic web apps with ReactJS
Workshop 27: Isomorphic web apps with ReactJSVisual Engineering
 
Server side rendering with React and Symfony
Server side rendering with React and SymfonyServer side rendering with React and Symfony
Server side rendering with React and SymfonyIgnacio Martín
 
AppSyncをReactで使ってみた
AppSyncをReactで使ってみたAppSyncをReactで使ってみた
AppSyncをReactで使ってみたTakahiro Kobaru
 
Zero to One : How to Integrate a Graphical Editor in a Cloud IDE (27.10.2021)
Zero to One : How to Integrate a Graphical Editor in a Cloud IDE (27.10.2021)Zero to One : How to Integrate a Graphical Editor in a Cloud IDE (27.10.2021)
Zero to One : How to Integrate a Graphical Editor in a Cloud IDE (27.10.2021)Obeo
 
Using JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot appsUsing JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot appsYakov Fain
 
Building and deploying React applications
Building and deploying React applicationsBuilding and deploying React applications
Building and deploying React applicationsAstrails
 
JS Fest 2018. Илья Иванов. Введение в React-Native
JS Fest 2018. Илья Иванов. Введение в React-NativeJS Fest 2018. Илья Иванов. Введение в React-Native
JS Fest 2018. Илья Иванов. Введение в React-NativeJSFestUA
 
Maciej Treder "Server-side rendering with Angular—be faster and more SEO, CDN...
Maciej Treder "Server-side rendering with Angular—be faster and more SEO, CDN...Maciej Treder "Server-side rendering with Angular—be faster and more SEO, CDN...
Maciej Treder "Server-side rendering with Angular—be faster and more SEO, CDN...Fwdays
 
reactjs-quiz..docs.pdf
reactjs-quiz..docs.pdfreactjs-quiz..docs.pdf
reactjs-quiz..docs.pdfAyanSarkar78
 
Solving anything in VCL
Solving anything in VCLSolving anything in VCL
Solving anything in VCLFastly
 
Retrofit library for android
Retrofit library for androidRetrofit library for android
Retrofit library for androidInnovationM
 
Retrofit Library In Android
Retrofit Library In AndroidRetrofit Library In Android
Retrofit Library In AndroidInnovationM
 

Similar to Redux Universal (20)

React custom renderers
React custom renderersReact custom renderers
React custom renderers
 
Building Universal Web Apps with React ForwardJS 2017
Building Universal Web Apps with React ForwardJS 2017Building Universal Web Apps with React ForwardJS 2017
Building Universal Web Apps with React ForwardJS 2017
 
React loadable
React loadableReact loadable
React loadable
 
Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...
Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...
Maciej Treder ''Angular Universal - a medicine for the Angular + SEO/CDN issu...
 
Presentation Tier optimizations
Presentation Tier optimizationsPresentation Tier optimizations
Presentation Tier optimizations
 
Workshop 27: Isomorphic web apps with ReactJS
Workshop 27: Isomorphic web apps with ReactJSWorkshop 27: Isomorphic web apps with ReactJS
Workshop 27: Isomorphic web apps with ReactJS
 
Server side rendering with React and Symfony
Server side rendering with React and SymfonyServer side rendering with React and Symfony
Server side rendering with React and Symfony
 
AppSyncをReactで使ってみた
AppSyncをReactで使ってみたAppSyncをReactで使ってみた
AppSyncをReactで使ってみた
 
Zero to One : How to Integrate a Graphical Editor in a Cloud IDE (27.10.2021)
Zero to One : How to Integrate a Graphical Editor in a Cloud IDE (27.10.2021)Zero to One : How to Integrate a Graphical Editor in a Cloud IDE (27.10.2021)
Zero to One : How to Integrate a Graphical Editor in a Cloud IDE (27.10.2021)
 
Using JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot appsUsing JHipster for generating Angular/Spring Boot apps
Using JHipster for generating Angular/Spring Boot apps
 
Building and deploying React applications
Building and deploying React applicationsBuilding and deploying React applications
Building and deploying React applications
 
React js
React jsReact js
React js
 
JS Fest 2018. Илья Иванов. Введение в React-Native
JS Fest 2018. Илья Иванов. Введение в React-NativeJS Fest 2018. Илья Иванов. Введение в React-Native
JS Fest 2018. Илья Иванов. Введение в React-Native
 
Maciej Treder "Server-side rendering with Angular—be faster and more SEO, CDN...
Maciej Treder "Server-side rendering with Angular—be faster and more SEO, CDN...Maciej Treder "Server-side rendering with Angular—be faster and more SEO, CDN...
Maciej Treder "Server-side rendering with Angular—be faster and more SEO, CDN...
 
reactjs-quiz..docs.pdf
reactjs-quiz..docs.pdfreactjs-quiz..docs.pdf
reactjs-quiz..docs.pdf
 
Node.js server-side rendering
Node.js server-side renderingNode.js server-side rendering
Node.js server-side rendering
 
Solving anything in VCL
Solving anything in VCLSolving anything in VCL
Solving anything in VCL
 
React native by example by Vadim Ruban
React native by example by Vadim RubanReact native by example by Vadim Ruban
React native by example by Vadim Ruban
 
Retrofit library for android
Retrofit library for androidRetrofit library for android
Retrofit library for android
 
Retrofit Library In Android
Retrofit Library In AndroidRetrofit Library In Android
Retrofit Library In Android
 

More from Nikolaus Graf

Type Systems & Props Design - Exploring PropTypes, TypeScript, Flow & Reason
Type Systems & Props Design - Exploring PropTypes, TypeScript, Flow & ReasonType Systems & Props Design - Exploring PropTypes, TypeScript, Flow & Reason
Type Systems & Props Design - Exploring PropTypes, TypeScript, Flow & ReasonNikolaus Graf
 
Get started with Reason
Get started with ReasonGet started with Reason
Get started with ReasonNikolaus Graf
 
Introduction to Serverless
Introduction to ServerlessIntroduction to Serverless
Introduction to ServerlessNikolaus Graf
 
Serverless Framework Intro
Serverless Framework IntroServerless Framework Intro
Serverless Framework IntroNikolaus Graf
 
Rich text editing with Draft.js
Rich text editing with Draft.jsRich text editing with Draft.js
Rich text editing with Draft.jsNikolaus Graf
 
AngularDart Introduction
AngularDart IntroductionAngularDart Introduction
AngularDart IntroductionNikolaus Graf
 

More from Nikolaus Graf (9)

Type Systems & Props Design - Exploring PropTypes, TypeScript, Flow & Reason
Type Systems & Props Design - Exploring PropTypes, TypeScript, Flow & ReasonType Systems & Props Design - Exploring PropTypes, TypeScript, Flow & Reason
Type Systems & Props Design - Exploring PropTypes, TypeScript, Flow & Reason
 
Reason and GraphQL
Reason and GraphQLReason and GraphQL
Reason and GraphQL
 
Get started with Reason
Get started with ReasonGet started with Reason
Get started with Reason
 
Introduction to Serverless
Introduction to ServerlessIntroduction to Serverless
Introduction to Serverless
 
Serverless Framework Intro
Serverless Framework IntroServerless Framework Intro
Serverless Framework Intro
 
Rich text editing with Draft.js
Rich text editing with Draft.jsRich text editing with Draft.js
Rich text editing with Draft.js
 
React on es6+
React on es6+React on es6+
React on es6+
 
AngularDart Introduction
AngularDart IntroductionAngularDart Introduction
AngularDart Introduction
 
Web Components
Web ComponentsWeb Components
Web Components
 

Recently uploaded

Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxMalak Abu Hammad
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationMichael W. Hawkins
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetEnjoy Anytime
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitecturePixlogix Infotech
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxnull - The Open Security Community
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptxLBM Solutions
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxOnBoard
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 

Recently uploaded (20)

Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
The Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptxThe Codex of Business Writing Software for Real-World Solutions 2.pptx
The Codex of Business Writing Software for Real-World Solutions 2.pptx
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
GenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day PresentationGenCyber Cyber Security Day Presentation
GenCyber Cyber Security Day Presentation
 
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your BudgetHyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
Hyderabad Call Girls Khairatabad ✨ 7001305949 ✨ Cheap Price Your Budget
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
Understanding the Laravel MVC Architecture
Understanding the Laravel MVC ArchitectureUnderstanding the Laravel MVC Architecture
Understanding the Laravel MVC Architecture
 
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptxMaking_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
Making_way_through_DLL_hollowing_inspite_of_CFG_by_Debjeet Banerjee.pptx
 
Key Features Of Token Development (1).pptx
Key  Features Of Token  Development (1).pptxKey  Features Of Token  Development (1).pptx
Key Features Of Token Development (1).pptx
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Pigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food ManufacturingPigging Solutions in Pet Food Manufacturing
Pigging Solutions in Pet Food Manufacturing
 
The transition to renewables in India.pdf
The transition to renewables in India.pdfThe transition to renewables in India.pdf
The transition to renewables in India.pdf
 
Pigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping ElbowsPigging Solutions Piggable Sweeping Elbows
Pigging Solutions Piggable Sweeping Elbows
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
Maximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptxMaximizing Board Effectiveness 2024 Webinar.pptx
Maximizing Board Effectiveness 2024 Webinar.pptx
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 

Redux Universal

  • 1. Redux Universal
 by Sebastian Siemssen & Nik Graf
  • 2. Redux Universal @nikgraf Nik Graf @nikgraf
 nik@nikgraf.com Creator of Belle Working with StarterSquad
  • 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
  • 7. Redux Universal @nikgraf Benefits • Search Engine Indexability • Code Reusability & Maintainability • Faster Perceived Load Time !?
  • 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 );
  • 13. Redux Universal @nikgraf Redux on the Server II/III const renderFullPage = (html, initialState) => { return ` <!doctype html> <html> <body> <div id="app">${html}</div> <script> window.__INITIAL_STATE__ =
 ${JSON.stringify(initialState)}; </script> <script src="/static/bundle.js"></script> </body> </html> `; }
  • 14. Redux Universal @nikgraf Redux on the Server III/III app.use((request, response) => { const store = createStore(rootReducer); const html = renderToString( <Provider store={store}> <App/> </Provider> ); const state = store.getState(); response.send(renderFullPage(html, state)); });
  • 15. Redux Universal @nikgraf What about async actions?
  • 16. Redux Universal @nikgraf Async Action Example import fetch from 'isomorphic-fetch'; export function fetchFriends() { return dispatch => { dispatch({ type: 'FETCH_FRIENDS' }); return fetch('http://localhost/api/friends/') .then(response => response.json()) .then(json => { dispatch({ type: 'RECEIVE_FRIENDS', payload: json }); }); }; }
  • 17. Redux Universal @nikgraf Issue with Async Async Action Render Render Async Action
  • 18. Redux Universal @nikgraf Before using redux-universal import { createStore, applyMiddleware } from 'redux'; import thunk from 'redux-thunk'; import rootReducer from '../reducers'; const createStoreWithMiddleware = applyMiddleware( thunk )(createStore); export default function configureStore(initialState) { return createStoreWithMiddleware(rootReducer, initialState); }
  • 19. Redux Universal @nikgraf Using redux-universal import { createStore } from 'redux'; import thunk from 'redux-thunk'; import rootReducer from '../reducers'; const applyMiddleware = __SERVER__ ? require('redux-universal') : require('redux').applyMiddleware; const createStoreWithMiddleware = applyMiddleware( thunk )(createStore); export default function configureStore(initialState) { return createStoreWithMiddleware(rootReducer, initialState); }
  • 20. Redux Universal @nikgraf Before using redux-universal const html = renderToString( <Provider store={store}> <App/> </Provider> ); const state = store.getState(); res.send(renderFullPage(html, state));
  • 21. Redux Universal @nikgraf Using redux-universal store.renderUniversal( renderToString, <Provider store={store}> <App/> </Provider> ) .then(({ output }) => { const state = store.getState(); response.send(renderFullPage(output, state)); }) .catch(({ output, error }) => { console.warn(error.message); });
  • 22. Redux Universal @nikgraf How it works -> render -> wait for promise -> render -> resolve
  • 23. Redux Universal @nikgraf How it works -> render -> wait for promiseA -> render -> wait for promiseB -> render -> resolve
  • 24.
  • 25. Redux Universal @nikgraf Fin 
 Checkout Redux Universal
 
 https://github.com/reducks/redux-universal