Handling RSC Errors in Next.js

Поділитися
Вставка
  • Опубліковано 5 сер 2024
  • In today's video we'll use an error boundary to refresh a React Server Component that threw an error. We'll also dive into transitions and see how they allow us to batch together state updates that finish at different times.
    - 0:00 - Intro
    - 1:06 - Error handling in Next.js
    - 2:11 - Resetting without fixing the error
    - 3:16 - Rerunning the server component
    - 4:23 - Avoiding race conditions with transitions
    - 6:47 - Why a transition is needed
    For more videos on React Server Components check out Build UI: buildui.com/?...

КОМЕНТАРІ • 30

  • @vickonsscope6477
    @vickonsscope6477 3 місяці тому +5

    Best use case of the react transition mechanism... I really love this... Thank you a lot.🎉🎉

    • @RyanToronto
      @RyanToronto  3 місяці тому

      Thank you! Glad you enjoyed it!

  • @haydenpitout5151
    @haydenpitout5151 3 місяці тому

    Thanks Ryan ... great work as always 👍

  • @erikplachta
    @erikplachta 3 місяці тому

    Appreciate the video. Nice to see startTransition solving some real world problems.

  • @talhaibnemahmud
    @talhaibnemahmud 3 місяці тому +1

    Really good video

  • @mehrabmahmudpur6984
    @mehrabmahmudpur6984 3 місяці тому

    Great video. Thank's a lot.

  • @bideshbanerjee5506
    @bideshbanerjee5506 3 місяці тому

    Hello Ryan you are the only one who talks deep about RSC.
    I just want to know one thing that suppose for a particular page there are 4 - 5 components and an user changes or updates something in a component with server action then the whole json representation of every component of that page will get send by the server, is that how it works ?? and if it is, how optimal is that approach or it works differently ?? I know it's lot to ask...but will appreciate a video or response
    Thank you ❤

    • @RyanToronto
      @RyanToronto  2 місяці тому

      Thank you! To answer your question it really depends on the framework you are using and how your app and action is setup. If you're using Next.js with server actions you'll most likely want to call the built-in `revalidate()` function to update any components on the page.

  • @oscarljimenez5717
    @oscarljimenez5717 3 місяці тому +2

    Great video like always :)
    One question. What's the standard for showing clean errors on the console? Using a logger like "pino"?
    But then i can't throw an error that my error boundary can catch :/

    • @RyanToronto
      @RyanToronto  3 місяці тому +1

      Thanks, glad you enjoyed it!
      Yah I think pino is great for logging. That said I'd still throw the error and have the error boundary catch it, but maybe also use pino right before you throw the error.

  • @luisllaboj
    @luisllaboj 3 місяці тому

    Is router.refresh() by default wrapped in a transition under the hood?
    Is that why reset() executes first? So, is Router.refresh() considered an asynchronous while reset() remains sync, unless we put both together in a transition and we force both to be completed before continuing?

    • @RyanToronto
      @RyanToronto  3 місяці тому +1

      Yup, router.refresh uses a transition under the hood.
      We need to put both the reset and the router.refresh in the transition so React knows to not reset until the refresh is complete. We're telling React we'd like to transition to a state where the router is refreshed and the error boundary is reset. React won't apply the state update until those two things are ready.
      Another way of saying this: Putting them inside the transition tells React that they need to happen together.

  • @luisllaboj
    @luisllaboj Місяць тому

    What would happen if wrap a transition in a transition? The outer transition holds a state for the inner transition or are they batched together or “unwrapped” like it was a promise passed to a promise constructor? lol idk if that makes sense

    • @RyanToronto
      @RyanToronto  Місяць тому +1

      Great question! Transitions that wrap transitions are very common and I think saying they are "batched" is the right way of thinking about it. If a transition is inside another transition then neither state update will apply until they are both ready. In other words the state updates happen at the same time for all nested transitions.

    • @luisllaboj
      @luisllaboj Місяць тому

      @@RyanToronto cool, yea that makes sense, thanks!

  • @tauhid97k
    @tauhid97k 2 місяці тому

    Thanks. Can you make a video on setting cookies from a separate backend in the next.js server action? So, When I use server action and set credentials: "include" it doesn't work. This is because it's happening on the server. How can I forward the cookies in the browser to that separate API/backend server from server action?

    • @RyanToronto
      @RyanToronto  2 місяці тому +1

      Great question. You can use Next's built in cookies() function to read the cookies in your server action and then pass them into the fetch request your action is making to your backend.

    • @tauhid97k
      @tauhid97k 2 місяці тому

      @@RyanToronto I've tried this and several other methods but it's not consistent or maybe I don't quite understand the situation here. From the client side credentials: "include" works as expected. So I thought with your amazing explanation I could understand better. But Thanks. I'll keep trying.

  • @0xtz_
    @0xtz_ 3 місяці тому

    we have an issue in some pages we have a conditional display based on the url query and we need on each query change to reload to get the updated data 😢
    any idea please hmmm even with parallel routes I need to noStore() to revalidate the data eeeeeh

    • @RyanToronto
      @RyanToronto  3 місяці тому

      Sorry to hear about your issue. Do you have an example app or demo of your app anywhere?

    • @0xtz_
      @0xtz_ 3 місяці тому

      @@RyanToronto it's a private code that I can't share
      but it's a
      server component takes an id in the query and display the needed component
      I can provide a basic example of you have a discord or something hhh

  • @nick-ui
    @nick-ui 3 місяці тому

    Very unique solution with startTransition, how did you understand that it is necessary to use it

    • @erikplachta
      @erikplachta 3 місяці тому +1

      The refresh wasn’t happening because reset was executed before refresh had time to complete.
      - Router.Refresh is refreshing the connection to the server.
      - Reset restarts the component.
      No async option on refresh.

    • @RyanToronto
      @RyanToronto  3 місяці тому +1

      Exactly what @erikplachta said! Since you want refresh and reset to happen at the same time they both need to be wrapped in a transition.

  • @TfMukles
    @TfMukles 3 місяці тому

    make a video on refresh token in nextjs. I am facing the issues. In nextjs layout and page is stream simultaneously can you please make a video.

    • @RyanToronto
      @RyanToronto  3 місяці тому

      Hey there, would love to! Do you have an example app, mockups, or any code you can share so I can better understand the app and problem?