Upgrade to Pro — share decks privately, control downloads, hide ads and more …

React Server Components: A New Way to Build Fas...

React Server Components: A New Way to Build Fast and Interactive Web Apps

React Server Components are a new feature that lets you render React components on the server and stream them to the client. This enables you to build rich user interfaces with minimal client-side code, while still preserving interactivity and performance.

In this talk, you will learn how React Server Components work, how to use them in your web apps, and what benefits they offer over traditional approaches. You will also see some real-world examples of React Server Components in action and get some tips and best practices for adopting them in your projects.

Aurora Scharff

October 18, 2023
Tweet

More Decks by Aurora Scharff

Other Decks in Programming

Transcript

  1. React Server Components: A New Way to Build Fast and

    Interactive Web Apps Aurora Scharff @aurorascharff
  2. Introduction > Aurora Scharff > Consultant at Inmeta > Norway,

    Oslo > Full-stack, web-focused: active RSC developer
  3. Talking points > Rendering strategies > React Server Components >

    Next.js App Router > RSC in Next.js “the right way” > Demo > Real world experience > The future of RSC
  4. Static website rendering > Pages are pre-assembled and uploaded to

    a static host > Simple and efficient > Struggles with dynamic data and impractical to maintain
  5. Multi-page applications (MPAs) > HTML and data are dynamically merged

    on the server > Serves the merged result on page load > Page reloads per request
  6. pros √ Optimized for SEO √ Unlimited scalability √ More

    website insights Multi-page applications (MPAs)
  7. pros √ Optimized for SEO √ Unlimited scalability √ More

    website insights cons × Slower perceived performance × Limited interactivity × Hard to maintain Multi-page applications (MPAs)
  8. Server-side rendering (SSR) > Pre-render the initial HTML on the

    server > Fully-formed HTML when opening the page > JavaScript is fetched alongside it
  9. Static site generation (SSG) > Pages are pre-assembled and uploaded

    to a static host > Requires site redeployment whenever data changes > JavaScript is fetched alongside it
  10. Server-side rendering (SSR) > Included feature in JavaScript “meta-frameworks” >

    Build time (SSG) and/or request time (SSR) > Pages generated in the build can be cached
  11. Hydration Hydration is like watering the “dry” HTML with the

    “water” of interactivity and event handlers. - React core team member Dan Abramov https://github.com/reactwg/react-18/discussions/37
  12. The problem 1. You have to fetch everything before you

    can show anything 2. You have to load everything before you can hydrate anything 3. You have to hydrate everything before you can interact with anything
  13. Strategies to improve it > Partial hydration: only attach JS

    to certain components > Progressive hydration: lazy load JS to certain components > Selective hydration: priority load JS to certain components based on user interactivity
  14. Streaming > Split a request into chunks > Send each

    chunk as a stream to the client > Render each chunk as it is streamed in
  15. Benefits √ Data fetching + access to backend resources √

    Security √ Caching √ Bundle size + performance
  16. Benefits √ Data fetching + access to backend resources √

    Security √ Caching √ Bundle size + performance √ Streaming + suspense integration
  17. Benefits √ Data fetching + access to backend resources √

    Security √ Caching √ Bundle size + performance √ Streaming + suspense integration √ Developer experience
  18. |

  19. Server Functions > Mark a function/file with “use server” >

    Next.js creates a “hidden” API endpoint for you > Use the function in any component > Provides a type-safe RPC-experience
  20. Requirements > Interactive dashboards > Complex forms and tables >

    Articles through a CMS, static data > Need for SEO > Mobile friendly and accessible > Multiple language support > Customer’s own UI library
  21. cons × Wrappers for custom library × Trial and error

    i18n × Limited testing resources Experience
  22. pros √ Performant app that fulfills requirements √ Maintainable app

    code √ Enjoyable DX Experience cons × Wrappers for custom library × Trial and error i18n × Limited testing resources
  23. Resources > React docs > Vercel Next.js App Router docs

    > Vercel template apps and examples > Vercel “Learn Next.js” free course