kharkiv js 2015 - creating isomorphic applications in react (en)

42
Pain and Joy of creating isomorphic applications in ReactJS Viktor Turskyi Kharkiv JS 2015

Upload: viktor-turskyi

Post on 06-Jan-2017

353 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Pain and Joy of creating isomorphic applications in

ReactJSViktor Turskyi

Kharkiv JS 2015

Page 2: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Isomorphism o_O

The interest of isomorphisms lies in the fact that two isomorphic objects cannot be distinguished by using only the properties used to define morphisms; thus isomorphic objects may be considered the same as long as one considers only these properties and their consequences.

(Wikipedia)

Page 3: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Isomorphic application (simpler definition)

Just SPA that can be run on server

Page 4: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Demo (app in real life)

wall.itsquiz.com

Page 5: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Client

Server

Shared code REST API

Page 6: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

RESTAPI

Shared code

Page 7: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

RESTAPI

View

Routing

Fetching I18N

Models

Page 8: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

What for? (about joy)

Search engine optimization

Loading speed

Page 9: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Colors of isomorphic

1) Isomorphic view2) Isomorphic styles3) Isomorphic routing4) Isomorphic data fetching5) Isomorphic configuration 6) Isomorphic ES67) Isomorphic localization

Page 10: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Joy #1 - React

Page 11: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

View - client

Page 12: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

View - server

Page 13: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Pain #1 - cannot import styles in nodejs

Page 14: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Solution - inline styles (styles - just JSON)

Page 15: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Issues with inline styles

1) pseudo attributes :hover, :active, :focus2) media queries3) autoprefixing4) styles merging

Page 16: Kharkiv JS  2015 - Creating isomorphic applications in React (en)
Page 17: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Server and client renders different markup

Page 18: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Pain #2 - client side autoprefixer

Page 19: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Solution that just works

Page 20: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Choosing UI components library

Page 21: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Pain #3 - “Material UI” uses client side autoprefixer

Page 22: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

We’ve switched to Material Design Lite

react-mdl (Material Design Lite) works great!!! )

What next?

Page 23: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Hooray, the problem with the styles is solved!!!

Page 24: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Not this time!!!

Page 25: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Pain #4 - assets loading order

Page 26: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

How to do it right?

Load CSS at the beginning

Load Javascript at the end

Page 27: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Correct loading order

Page 28: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Solution - Text Extract Plugin for Webpack

Page 29: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Joy #2 - React Router

Page 30: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Routing (shared code)

Page 31: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Routing (client)

Page 32: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Routing (server)

Page 33: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Joy #3 - Redux

Page 34: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

One store per application

Page 35: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Work with REST API

Page 36: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Data fetching steps

Client:

1. React component rendering2. Show spinner3. Fetch data4. Update the page (rerender React component with fetched data)

Server:

1. Preload all required data for a page2. Render the page to string3. Send HTML to client

Page 37: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

REST API - shared code (isomorphic-fetch)

Page 38: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

What is left outside of the talk

1) Isomorphic configuration 2) Isomorphic localization3) Isomorphic ES6

Page 39: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Statistics

Universal code - 1778 SLOC (93%)

Client specific code - 33 SLOC (2%)

Server specific code - 95 SLOC (5%)

Page 40: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

links

Redux http://rackt.org/redux/index.html

React Router https://github.com/rackt/react-router

Tutorial: Handcrafting an Isomorphic Redux Application (With Love)

https://medium.com/@bananaoomarang/handcrafting-an-isomorphic-redux-application-with-love-40ada4468af4

Page 41: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

The application source code

https://github.com/WebbyLab/itsquiz-wall/

Page 42: Kharkiv JS  2015 - Creating isomorphic applications in React (en)

Viktor Turskyi (koorchik)

[email protected]://twitter.com/koorchik

https://github.com/koorchik

http://webbylab.com