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

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

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 Lid Walberg

October 18, 2023
Tweet

More Decks by Aurora Lid Walberg

Other Decks in Programming

Transcript

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

    Interactive Web Apps Aurora Walberg Scharff @aurorascharff
  2. Introduction > Aurora Walberg 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 overall performance × More development time × Hard to maintain Multi-Page Applications (MPAs)
  8. Single-Page Applications, cons × Slow initial load × SEO suffers

    × Needs JavaScript × Favors modern browsers × Less secure
  9. Server-side rendering (SSR) > Pre-render the initial HTML on the

    server > Fully-formed HTML when opening the page > JavaScript is fetched alongside it
  10. 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
  11. 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
  12. 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
  13. 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
  14. 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
  15. Streaming > Split a request into chunks > Send each

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

    Security ✓ Caching ✓ Bundle size + performance
  17. Benefits ✓ Data fetching + access to backend resources ✓

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

    Security ✓ Caching ✓ Bundle size + performance ✓ Streaming + suspense integration ✓ Developer experience
  19. |

  20. Server Actions > Define 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
  21. Requirements > Interactive dashboards > Complex forms > Articles through

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

    i18n × Limited testing resources Experience
  23. pros ✓ Performant app that fulfills requirements ✓ Maintainable app

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

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