Slide 1

Slide 1 text

@stefanjudis React in a worker, worker, worker, worker...

Slide 2

Slide 2 text

@stefanjudis www.stefanjudis.com Heyo, I'm Stefan!

Slide 3

Slide 3 text

Let's talk about CRA (create-react-app)

Slide 4

Slide 4 text

No content

Slide 5

Slide 5 text

A classical Single Page App

Slide 6

Slide 6 text

There are problems with typical single page apps...

Slide 7

Slide 7 text

React App No JavaScriptzzzz ...

Slide 8

Slide 8 text

React App No JavaScriptzzzz ...

Slide 9

Slide 9 text

React App No JavaScriptzzzz ...

Slide 10

Slide 10 text

React App No JavaScriptzzzz ...

Slide 11

Slide 11 text

React App No JavaScriptzzzz ...

Slide 12

Slide 12 text

React App No JavaScriptzzzz ...
No JavaScript zzzz ...

Slide 13

Slide 13 text

13 Million requests for JavaScript will time out. Ian Feather (Buzzfeed)

Slide 14

Slide 14 text

"We don't have any non- JavaScript users". No, all your users are non-JS while they're downloading your JS. Jake Archibald (Google)

Slide 15

Slide 15 text

React App No JavaScriptzzzz ...

Slide 16

Slide 16 text

React App No JavaScriptzzzz ...
The spinner of death

Slide 17

Slide 17 text

How can we do better?

Slide 18

Slide 18 text

proxx.app

Slide 19

Slide 19 text

nextjs.org www.gatsbyjs.org

Slide 20

Slide 20 text

nextjs.org www.gatsbyjs.org Run the same React code on the server and client

Slide 21

Slide 21 text

No content

Slide 22

Slide 22 text

No content

Slide 23

Slide 23 text

No content

Slide 24

Slide 24 text

No content

Slide 25

Slide 25 text

bundle.js

Slide 26

Slide 26 text

bundle.js

Slide 27

Slide 27 text

bundle.js Interactive

Slide 28

Slide 28 text

Better performance!

Slide 29

Slide 29 text

Better performance! Faster indexed by Google!

Slide 30

Slide 30 text

Better performance! Faster indexed by Google! It's the same code front to back!

Slide 31

Slide 31 text

But is it really the same code?

Slide 32

Slide 32 text

module.exports = global.fetch = global.fetch || (typeof process == 'undefined' ? require('unfetch').default || require('unfetch') : function(url, opts) { return require('node-fetch')(url.replace(/^\/\ //g, 'https: //'), opts); });

Slide 33

Slide 33 text

github.com/developit/unfetch/tree/master/packages/isomorphic-unfetch

Slide 34

Slide 34 text

What if we serve HTML from an environment with the same methods?

Slide 35

Slide 35 text

The Service Worker worker.js

Slide 36

Slide 36 text

if ('serviceWorker' in navigator) { navigator.serviceWorker .register('/worker.js') .then(function(registration) {}, function(err) {}); }

Slide 37

Slide 37 text

self.addEventListener('fetch', function(event) { event.respondWith( caches.match(event.request) .then(function(response) { // Cache hit - return response if (response) { return response; } return fetch(event.request); } ) ); });

Slide 38

Slide 38 text

Let's try an experiment?

Slide 39

Slide 39 text

Crowd Coding...

Slide 40

Slide 40 text

What did just happen?

Slide 41

Slide 41 text

No content

Slide 42

Slide 42 text

localhost:8080

Slide 43

Slide 43 text

localhost:8080

Slide 44

Slide 44 text

localhost:8080 worker.js

Slide 45

Slide 45 text

localhost:8080 worker.js

Slide 46

Slide 46 text

localhost:8080 worker.js Interactive

Slide 47

Slide 47 text

localhost:8080 worker.js Interactive

Slide 48

Slide 48 text

localhost:8080 worker.js Interactive

Slide 49

Slide 49 text

localhost:8080 worker.js Interactive

Slide 50

Slide 50 text

localhost:8080 worker.js Interactive

Slide 51

Slide 51 text

localhost:8080 worker.js Interactive

Slide 52

Slide 52 text

worker.js

Slide 53

Slide 53 text

worker.js

Slide 54

Slide 54 text

worker.js

Slide 55

Slide 55 text

worker.js

Slide 56

Slide 56 text

Could universal workers be a thing?

Slide 57

Slide 57 text

Let's create a cloud worker!

Slide 58

Slide 58 text

www.cloudflare.com/products/cloudflare-workers/

Slide 59

Slide 59 text

developers.cloudflare.com/workers/reference/

Slide 60

Slide 60 text

developers.cloudflare.com/workers/reference/

Slide 61

Slide 61 text

developers.cloudflare.com/workers/reference/

Slide 62

Slide 62 text

https://dash.cloudflare.com/9dcd7c...

Slide 63

Slide 63 text

worker.js

Slide 64

Slide 64 text

worker.js

Slide 65

Slide 65 text

worker.js

Slide 66

Slide 66 text

worker.js

Slide 67

Slide 67 text

worker.js

Slide 68

Slide 68 text

worker.js

Slide 69

Slide 69 text

worker.js

Slide 70

Slide 70 text

worker.js

Slide 71

Slide 71 text

worker.js worker.js

Slide 72

Slide 72 text

worker.js worker.js

Slide 73

Slide 73 text

worker.js worker.js

Slide 74

Slide 74 text

worker.js

Slide 75

Slide 75 text

worker.js

Slide 76

Slide 76 text

worker.js

Slide 77

Slide 77 text

worker.js

Slide 78

Slide 78 text

worker.js

Slide 79

Slide 79 text

Is this the solution to universal JavaScript?

Slide 80

Slide 80 text

Service worker development is still very tedious

Slide 81

Slide 81 text

Cache handling is tough

Slide 82

Slide 82 text

It's worth investigating!

Slide 83

Slide 83 text

Servers with browser APIs will change how we share code!

Slide 84

Slide 84 text

react-in-a-worker.netlify.com

Slide 85

Slide 85 text

www.react-in-a-worker.rocks

Slide 86

Slide 86 text

github.com/stefanjudis/react-in-a-worker

Slide 87

Slide 87 text

One last thing...

Slide 88

Slide 88 text

... let's serve HTML instead of spinners.

Slide 89

Slide 89 text

@stefanjudis [email protected] www.stefanjudis.com Thanks.