Slide 1

Slide 1 text

G O I N G I S O M O R P H I C W I T H D J A N G O & R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1

Slide 2

Slide 2 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 2 Full stack developer @ SpazioDati Board game addicted Something between a beer lover and a beer snob MATTIA LARENTIS

Slide 3

Slide 3 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 3 Semantic stuff and Big data 30+ employees… hey, we’re hiring! Both in Trento and in Pisa SPAZIODATI

Slide 4

Slide 4 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 4 A marketing and sales intelligence tool Works on different countries Written in Django + React ATOKA

Slide 5

Slide 5 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 5 Talking about SSR Atoka SpazioDati 2012 2014 TODAY TOMORROW Conquer the world

Slide 6

Slide 6 text

W A I T , W H AT I S S E R V E R S I D E R E N D E R I N G ?

Slide 7

Slide 7 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 aɪ.soʊˈmɔɹ.fɪk In web development, an isomorphic application is one whose code (in this case, JavaScript) can run both in the server (in this case, Django) and the client. 7

Slide 8

Slide 8 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 Normally when using React, your browser will download a minimal HTML page, and the content will be filled in by JavaScript. 8

Slide 9

Slide 9 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 9 Page now viewable and interactable Browser executes React Browser downloads JS Server sends HTML to browser

Slide 10

Slide 10 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 With SSR, the initial content is generated on the server, so your browser can download a page with HTML content already in place. Updates to the content are still handled in the browser. 1 0

Slide 11

Slide 11 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 1 1 Page now interactable Browser executes React Now viewable The browser downloads JS Server sends ready to be rendered HTML response to browser

Slide 12

Slide 12 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 1 2 Rendering server-side helps search engine crawlers find your content. SEO Using the same components both in server and in client side allows you to not repeat yourself. DRY SSR will usually increase performance for your app, but not always. ? PERFORMANCE Using SSR will increase the complexity both of your code and your infrastructure. X COMPLEXITY The page will be rendered even if JavaScript is disabled on the browser. NO SCRIPT

Slide 13

Slide 13 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 SEO Search engine crawlers don’t support JavaScript (Google sometimes does). For apps built with React, this means they see a blank page. Fixing this problem is usually the most common reason for going SSR. 1 3

Slide 14

Slide 14 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 DRY “Don’t repeat yourself” is the key. Same code running on server and on browser. Use your favourite tech (React) without considering the environment (server / client) you are working in. 1 4

Slide 15

Slide 15 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 No script SSR allows you to write applications that gracefully degrade and can be used as thin clients. Processing runs on server and the application can be viewed in a browser without JavaScript enabled. 1 5

Slide 16

Slide 16 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 Improve perf. The browser can display your rendered components as soon as it downloaded HTML & CSS. If your JavaScript file is very large, this can be a big improvement. The web page won't be interactive until the JavaScript downloads and React boots up, but perceived performance is still improved. 1 6

Slide 17

Slide 17 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 Degrade perf. SSR means more work for your server, so your HTTP response will take a little longer to return. The size of your HTML will increase and will take longer to download. 1 7 X

Slide 18

Slide 18 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 More complex OP React components need node to run. Your infrastructure should keep a node instance up and running, that can access the same file system of your Django project. Give a warm welcome to another point of failure. 1 8 X

Slide 19

Slide 19 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 More complex code 1 9 X Even if code is shared among server and client, your components have to be adjusted due to different behaviour (Ajax requests). If you use react-router on the client side, you'll need to rewrite those same routers on your server.

Slide 20

Slide 20 text

H O W D O Y O U U S E S S R I N S P A Z I O D AT I R I G H T N O W ?

Slide 21

Slide 21 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 We need to build a PDF file containing the same information we render on a web page. We designed a proof-of-concept of this feature in order to use the same React components used on the frontend to build such files. It ain’t important to move the whole system to SSR! 2 1

Slide 22

Slide 22 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 2 2 We’re not alone.

Slide 23

Slide 23 text

H O W D O E S I T W O R K ?

Slide 24

Slide 24 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 HTTP request to Django app Django embeds React payload into HTML and returns response 2 4 HTTP request to Node server with data HTML payload of rendered React app Request data from DB Response with data

Slide 25

Slide 25 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 $ pip install react==4.1.1 2 5

Slide 26

Slide 26 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 REACT = { 'RENDER': True, 'RENDER_URL': 'http://127.0.0.1:9009/render', } 2 6

Slide 27

Slide 27 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 def index(request): # something super cool here! rendered = render_component( ‘path/to/component-to-render.jsx’, {data: data}, ) # something super cool here as well! return render( request, ‘index.html', {‘rendered’: rendered} ) 2 7

Slide 28

Slide 28 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 {# inject the server-side rendered component #}
{{ rendered | safe }}
{# add as global variable the store defined in the index view #} var store = {{ store | safe }}; {# add the bundle built by webpack #} 2 8

Slide 29

Slide 29 text

D E M O

Slide 30

Slide 30 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 http://localhost:8000 3 0

Slide 31

Slide 31 text

W H AT W E L E A R N E D T O D AY ?

Slide 32

Slide 32 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 3 2 First Migrating the code is not painless.

Slide 33

Slide 33 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 3 3 Second … but it’s not mandatory to move the whole code to SSR.

Slide 34

Slide 34 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 3 4 Third Your infrastructure will be more complex and it will contain a new point of failure (node).

Slide 35

Slide 35 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 3 5 Fourth … but frontenders will be allowed to write code in a single way using a single language.

Slide 36

Slide 36 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 3 6 Fifth Your code will not be so complex, after all.

Slide 37

Slide 37 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 3 7 Sixth … but SEO will be improved.

Slide 38

Slide 38 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 SRR is not the silver bullet for all the problems… …but it can solve some :) 3 8

Slide 39

Slide 39 text

No content

Slide 40

Slide 40 text

G O I N G I S O M O R P H I C W I T H D J A N G O A N D R E A C T L A R E N T I S M AT T I A - P Y C O N - 2 0 1 8 / 0 4 / 2 1 4 0 http://t.me/nostalgiaz http://github.com/nostalgiaz https://spaziodati.eu LARENTIS MATTIA