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

React Server Components og React 19 i Next.js A...

Aurora Scharff
September 14, 2024
16

React Server Components og React 19 i Next.js App Router

Aurora Scharff

September 14, 2024
Tweet

Transcript

  1. I denne workshoppen skal vi bygge en kontaktliste- CRUD-app med

    Next.js App Router, React Server Components, Tailwind, Prisma og Server Actions. Gjennom utviklingsprosessen vil vi lære alle relevante konsepter steg for steg. Videre skal vi bruke React 19 for å forbedre brukeropplevelsen og forenkle koden. Ved slutten av workshoppen vil vi bli kjent med typiske utviklingsmønstre, viktige biblioteker og testmetoder.
  2. HVEM ER JEG? Aurora Scharff > Utvikler MSDev > Bachelor

    UiO Robotikk og intelligente systemer > Tidligere erfaring fra 2 år i fintech-startup > Vokst opp på seilbåt
  3. HVEM ER JEG? Aurora Scharff > Begynte hos Inmeta januar

    2023 > Jobber for Miljødirektoratet > Frontend-utvikler for modernisering av avfallsdeklarering.no
  4. HVEM ER JEG? Aurora Scharff > Snakker på konferanser (NDC

    ++) om webutvikling > Involvert i utviklingsmiljøet for React hos Meta, representert dem på egen konferanse React Conf > Invitert til Next.js Conf i San Francisco
  5. Dag 1 plan > Rendering on the web (slides) >

    Next.js App Router (slides) > Server components > Client components > LUNCH > Web performance (slides) > Transitions > Suspense > Server Actions > TASK
  6. Static website rendering > Pages are pre-assembled and uploaded to

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

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

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

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

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

    chunk as a stream to the client > Render each chunk as it is streamed in
  17. Next.js App Router An opinionated React meta-framework by Vercel, offering:

    > Server-Side Rendering (SSR), Static Site Generation (SSG), ISR (Incremental Site Regeneration) > Automatic code splitting > File-system based routing and nested layouts > API routes > Caching, Image optimization, SEO, ++ > React Server Components
  18. > Needs server* > Increased complexity > Harder to deploy

    > Learning curve > Opinionated Do I need a meta-framework?
  19. Core Web Vitals > Largest Contentful Paint (LCP) > Cumulative

    Layout Shift (CLS) > First Input Delay (FID) > Interaction to Next Paint (INP) Other web vitals: TTFB, FCP, TBT, TTI, Speed Index https://crystallize.com/learn/best-practices/frontend-performance/other-web-vitals
  20. TASK Task 1: Handle loading state for contacts/{id}/edit-page Task 2:

    Handle loading states for and implement Task 3: Make the favorite-button work const response = confirm('Please confirm you want to delete this record.'); if (!response) {
  21. Dag 2 plan > React 19 (slides) > React Compiler

    (slides) > Actions > useActionState(), useOptimistic(), useFormStatus() > LUNCH > CSS in Server Components & Deployment (slides) > Data management, use(), optimizations > Testing > Libraries and development patterns > TASK
  22. RECAP > Rendering on the web (slides) > Next.js App

    Router (slides) > Server components > Client components > LUNCH > Web performance (slides) > Transitions > Suspense > Server Actions > TASK
  23. Dag 2 plan > React 19 (slides) > React Compiler

    (slides) > Actions > useActionState(), useOptimistic(), useFormStatus() > LUNCH > CSS in Server Components & Deployment (slides) > Data management, use(), optimizations > Testing > Libraries and development patterns > TASK
  24. “Progressive enhancement is a strategy that puts emphasis on web

    content first, allowing everyone to access the basic functionality whilst users with additional browser features or faster Internet access receive an enhanced version instead.” https://en.wikipedia.org/wiki/Progressive_enhancement
  25. React 19 > Server components > Actions and server actions

    > Form component > New hooks: useActionState(), useOptimistic(), useFormStatus() > Use() API > … and more https://react.dev/blog/2024/04/25/react-19
  26. … and more https://react.dev/blog/2024/04/25/react-19 > Forwardref: Removed, ref is now

    a prop > Better hydration errors: see where error occurred in HTML > Metadata: ability to hoist from any component > CSS optimizations: priority stylesheets > Async script support > Custom element support > Context improvements > Support for preloading resources
  27. React Compiler (prev. React Forget) https://react.dev/learn/react-compiler > Automatically memoizes your

    code 1. Skipping cascading re-rendering of components 2. Skipping expensive calculations from outside of React > Experimental and open source > Requires you to follow the rules of React “Forget” about useCallback() and useMemo()
  28. How to get started (NB! Experimental!) https://react.dev/learn/react-compiler > Install eslint

    extension to avoid breaking rules of react > Enable as vite plugin, next.js plugin etc > View memoized components in devtools
  29. React Compiler √ Less time spent on manual memoization √

    Less complex code √ More performant apps
  30. The Dilemma > The styled-components library allows us to dynamically

    create React components with attached CSS. It works by managing a <style> tag that gets updated as components re-render.
  31. The Dilemma > The styled-components library allows us to dynamically

    create React components with attached CSS. It works by managing a <style> tag that gets updated as components re-render. > “React Server Components” is a new paradigm for React that gives us a new type of component, the Server Component. Server Components render exclusively on the server. Their code isn’t even included in our JS bundles.
  32. The Dilemma > The styled-components library allows us to dynamically

    create React components with attached CSS. It works by managing a <style> tag that gets updated as components re-render. > “React Server Components” is a new paradigm for React that gives us a new type of component, the Server Component. Server Components render exclusively on the server. Their code isn’t even included in our JS bundles.
  33. Deployment > Vercel: zero cofiguration but potentially expensive > Self-hosted

    > Node.js server (i.e App Service) > Docker image > Static HTML files (not supporting server-features) https://nextjs.org/docs/app/building-your-application/deploying
  34. TASK Task: Make a “chat” or message box for each

    contact. Think about: 1. Loading states 2. Error states 3. Validation 4. Reducing JS on the client 5. Using new React 19 hooks if relevant