$30 off During Our Annual Pro Sale. View Details »

React in a worker, worker, worker...

React in a worker, worker, worker...

stefan judis

May 23, 2019
Tweet

More Decks by stefan judis

Other Decks in Technology

Transcript

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

    View Slide

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

    View Slide

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

    View Slide

  4. View Slide

  5. A classical Single Page App

    View Slide

  6. There are problems with
    typical single page apps...

    View Slide









  7. React App


    No JavaScriptzzzz ...






    View Slide









  8. React App


    No JavaScriptzzzz ...






    View Slide









  9. React App


    No JavaScriptzzzz ...






    View Slide









  10. React App


    No JavaScriptzzzz ...






    View Slide









  11. React App


    No JavaScriptzzzz ...






    View Slide









  12. React App


    No JavaScriptzzzz ...






    No
    JavaScript
    zzzz ...

    View Slide

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

    View Slide

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

    View Slide









  15. React App


    No JavaScriptzzzz ...






    View Slide









  16. React App


    No JavaScriptzzzz ...






    The spinner of death

    View Slide

  17. How can we do better?

    View Slide

  18. proxx.app

    View Slide

  19. nextjs.org
    www.gatsbyjs.org

    View Slide

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

    View Slide

  21. View Slide

  22. View Slide

  23. View Slide

  24. View Slide

  25. bundle.js

    View Slide

  26. bundle.js

    View Slide

  27. bundle.js
    Interactive

    View Slide

  28. Better performance!

    View Slide

  29. Better performance!
    Faster indexed by Google!

    View Slide

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

    View Slide

  31. But is it really
    the same code?

    View Slide

  32. 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);
    });

    View Slide

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

    View Slide

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

    View Slide

  35. The Service Worker
    worker.js

    View Slide

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

    View Slide

  37. 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);
    }
    )
    );
    });

    View Slide

  38. Let's try an
    experiment?

    View Slide

  39. Crowd Coding...

    View Slide

  40. What did just happen?

    View Slide

  41. View Slide

  42. localhost:8080

    View Slide

  43. localhost:8080

    View Slide

  44. localhost:8080
    worker.js

    View Slide

  45. localhost:8080
    worker.js

    View Slide

  46. localhost:8080
    worker.js
    Interactive

    View Slide

  47. localhost:8080
    worker.js
    Interactive

    View Slide

  48. localhost:8080
    worker.js
    Interactive

    View Slide

  49. localhost:8080
    worker.js
    Interactive

    View Slide

  50. localhost:8080
    worker.js
    Interactive

    View Slide

  51. localhost:8080
    worker.js
    Interactive

    View Slide

  52. worker.js

    View Slide

  53. worker.js

    View Slide

  54. worker.js

    View Slide

  55. worker.js

    View Slide

  56. Could universal
    workers be a thing?

    View Slide

  57. Let's create
    a cloud worker!

    View Slide

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

    View Slide

  59. developers.cloudflare.com/workers/reference/

    View Slide

  60. developers.cloudflare.com/workers/reference/

    View Slide

  61. developers.cloudflare.com/workers/reference/

    View Slide

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

    View Slide

  63. worker.js

    View Slide

  64. worker.js

    View Slide

  65. worker.js

    View Slide

  66. worker.js

    View Slide

  67. worker.js

    View Slide

  68. worker.js

    View Slide

  69. worker.js

    View Slide

  70. worker.js

    View Slide

  71. worker.js
    worker.js

    View Slide

  72. worker.js
    worker.js

    View Slide

  73. worker.js
    worker.js

    View Slide

  74. worker.js

    View Slide

  75. worker.js

    View Slide

  76. worker.js

    View Slide

  77. worker.js

    View Slide

  78. worker.js

    View Slide

  79. Is this the solution
    to universal JavaScript?

    View Slide

  80. Service worker development
    is still very tedious

    View Slide

  81. Cache handling
    is tough

    View Slide

  82. It's worth
    investigating!

    View Slide

  83. Servers with
    browser APIs will change
    how we share code!

    View Slide

  84. react-in-a-worker.netlify.com

    View Slide

  85. www.react-in-a-worker.rocks

    View Slide

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

    View Slide

  87. One last thing...

    View Slide

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

    View Slide

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

    View Slide