The most important function to write performant next.js apps

แชร์
ฝัง
  • เผยแพร่เมื่อ 13 ต.ค. 2024
  • Become a YT Members to get extra perks!
    www.youtube.co...
    My Products
    🏗️ WDC StarterKit: wdcstarterkit.com
    📖 ProjectPlannerAI: projectplanner...
    🤖 IconGeneratorAI: icongeneratora...
    Useful Links
    💬 Discord: / discord
    🔔 Newsletter: newsletter.web...
    📁 GitHub: github.com/web...
    📺 Twitch: / webdevcody
    🤖 Website: webdevcody.com
    🐦 Twitter: / webdevcody

ความคิดเห็น • 113

  • @WebDevCody
    @WebDevCody  3 หลายเดือนก่อน +24

    Said hook, but meant function. Don’t kill me

  • @windwardhive7363
    @windwardhive7363 3 หลายเดือนก่อน +38

    rsc in the mud. react query ftw

  • @StaleDegree
    @StaleDegree 3 หลายเดือนก่อน +14

    Btw for fetch api calls, nextjs natively memoizes the results of the fetch request. So if you make the same fetch request in multiple components in the react tree, you don't need to use the new cache requests. This only applies to the native fetch api. If you are using libraries like axios to fetch or database ORMs then cache is the move like Cody shows.

    • @Vantivify
      @Vantivify 3 หลายเดือนก่อน +3

      From nextjs 15 it wont be the default behaviour.

    • @daphenomenalz4100
      @daphenomenalz4100 3 หลายเดือนก่อน

      Would that be a security issue?

  • @kaansal9523
    @kaansal9523 3 หลายเดือนก่อน +3

    Whenever I ask a question like: "When the cache will resets?" You always answer my question in seconds. Thank you for this very important and easy solution.

  • @naughtiousmaximus7853
    @naughtiousmaximus7853 3 หลายเดือนก่อน +28

    Good Cody: Uses Bearded Blue theme
    Evil Cody: Doesnt use Bearded Blue theme

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน +10

      The maintainer of bearded theme is cookn up my own webdevcody theme with my brand colors 🤣 I’m going to suss it out to see if people hate it or not

    • @daphenomenalz4100
      @daphenomenalz4100 3 หลายเดือนก่อน +1

      @@WebDevCody that's great lol

  • @hello19286
    @hello19286 3 หลายเดือนก่อน +1

    I think it's also important to mention that this is already built in for regular fetches in React. React essentially automatically extends the fetch API and will return the cached fetch requests on a per request basis. This becomes especially important if you are fetching from the database (like you are in this function), because that will not get cached automatically.

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน

      Often you can’t use fetch to get data from a database. You usually use a database driver.

    • @Vantivify
      @Vantivify 3 หลายเดือนก่อน

      How people missunderstand such things. React has nothing to do with fetch. Its behaviour that nextjs introduced and made it default which is sooo wrong. Fortunately they remove it as a default and instead it will be opt in in nextjs 15

    • @hello19286
      @hello19286 3 หลายเดือนก่อน

      @@Vantivify You're the one unfortunately misunderstanding. React ALSO extends the fetch API. That is why in the video he is importing from React and not Nextjs. This video is about request memoization which is a React caching layer. You are talking about the nextjs data cache layer, that is what is being turned off in nextjs 15.
      Look up request memoization if you don't believe me.

  • @szymon7932
    @szymon7932 3 หลายเดือนก่อน +3

    What keyboard do you use? It sounds amazing

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน +1

      Klack.app

    • @Silver-fh4fb
      @Silver-fh4fb 3 หลายเดือนก่อน

      Cody did a video on the app th-cam.com/video/AJDSZcv27JI/w-d-xo.html

  • @Ivcota
    @Ivcota 3 หลายเดือนก่อน +2

    straight up "I just know nextjs thumbnail" right here

  • @randohundo9795
    @randohundo9795 3 หลายเดือนก่อน +1

    Really useful stuff. Especially when you host your app on a "pay per-request" platform you do not want to have it make 7 requests on a page refresh per user.

  • @jfhandfield
    @jfhandfield 3 หลายเดือนก่อน +1

    Thank you for that Cody. Really helpful. If I understand correctly, pretty new to nextjs deving, since it's caching the info, could it replace a React Context since you don't really need to store the data but just get the cached version of the call ?

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน +1

      Cache is specifically for your RSC, context is for your client components. It’s different imo

  • @JoshIbbotson
    @JoshIbbotson 3 หลายเดือนก่อน

    Hey Cody, what keyboard do you have? It sounds amazing

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน

      Klack app

  • @sameerahmedk
    @sameerahmedk 3 หลายเดือนก่อน +1

    Thank you for sharing! This shows how important it is to check the # of renders/function calls in react.
    Curious to know if useContext hook can also work (wrapping the overall root in context provider and then using it in components) or no?

    • @aleksandrriazantsev5334
      @aleksandrriazantsev5334 3 หลายเดือนก่อน

      Curious as well

    • @JayellsNext
      @JayellsNext 2 หลายเดือนก่อน

      See this what I do then recall through middleware for certain pages I’m not sure if it’s right or wrong but I do it 😅

  • @nasko235679
    @nasko235679 3 หลายเดือนก่อน +4

    Question: Aren't those children components client components? How are you even using the validateRequests() function inside them? What I do when using lucia is have a parent server component that checks if the user exists in validateRequests() and if they do I pass it as a prop to the children client components for rendering reasons (log out button, displaying someone's name etc) and if it doesn't exist I simply redirect the user to a login page. I believe that way the function also gets called only once.

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน

      Yeah I only run getCurrentUser inside server components and often I’ll show different client components if the user is authenticated or not. Sometimes I’ll pass in the user info into the client components, but I often find it better to just try to conditionally show things in the RSC

    • @nasko235679
      @nasko235679 3 หลายเดือนก่อน

      @@WebDevCody If you're doing that why is the request running 6-7 times? Isn't it supposed to run on the initial request of the server component only?

    • @JonathanIngram
      @JonathanIngram 3 หลายเดือนก่อน

      ​@@nasko235679 I believe it's running 6-7 times because we're looking at the outputted console.log statement from a function, whereas by default Server Components will usually look to memoize duplicate fetch requests instead (i.e. if the same fetch request is called multiple times from different components). So therefore, if your function had to do a lot of heavy calculations immediately after receiving that fetch data, then the cache idea would be a perfect opportunity to use it, to help ensure those calculations are only run once.

  • @slemchik03
    @slemchik03 3 หลายเดือนก่อน

    Awesome video. But do next-auth/clerk cache the session request for me or do I need to do it manually?

  • @agustind
    @agustind 3 หลายเดือนก่อน +2

    Does anybody know which keyboard is Cody using? it sounds awesome

    • @h4nto772
      @h4nto772 3 หลายเดือนก่อน

      he should be using the macbook keyboard with a software that adds the sound of key press on the recording. he has a video showing it

  • @devaccess
    @devaccess 3 หลายเดือนก่อน

    Thank you so much Cody,
    Please which extension are you using for your error to show directly on the editot?

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน

      Error lens

  • @timekouka
    @timekouka 3 หลายเดือนก่อน +4

    Currently working aswell on a SaaS starter with some cool functionalities implemented. Strongly feel you when you tell that you'll never finish that boilerplate 😂
    and nice one!!

  • @null_spacex
    @null_spacex 3 หลายเดือนก่อน +1

    Been going through exactly this with my next.js supabase app

  • @codingwithjamal
    @codingwithjamal 3 หลายเดือนก่อน

    I learned something new, when the video started, I thought you were going to use react context. Do people still use context is apps now days or is it not recommended for using data in our app

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน +1

      Context is often still needed for various things. I guess it depends on what you need it for. Often you’ll fetch the initial value in your RSC and populate the context value using it so the client component can use it when it hydrates

    • @codingwithjamal
      @codingwithjamal 3 หลายเดือนก่อน

      @@WebDevCody makes sense, i see RSC has replaced a lot of the old fetch paradigms

  • @devfren
    @devfren 3 หลายเดือนก่อน

    Thanks man! Learned something new

  • @shacodes
    @shacodes 3 หลายเดือนก่อน

    may be create a hook with Context API, that will use same instance

  • @aymenbachiri-yh2hd
    @aymenbachiri-yh2hd 2 หลายเดือนก่อน

    Thank you so much

  • @fanush3754
    @fanush3754 3 หลายเดือนก่อน

    might be a stupid question but how does this work with server components? After working on a few projects I noticed that the response is usually cached on build time in production and not on refresh. Does this cache function override the default nextjs behavior to revalidate on build time and instead revalidates on user request, for instance a Ctrl + R?

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน

      If you’re on a page that uses a cookie the page will always be dynamic

  • @sinatofangsaz2209
    @sinatofangsaz2209 3 หลายเดือนก่อน

    wow this one was super helpful. thanks!

  • @bijesh619
    @bijesh619 3 หลายเดือนก่อน +1

    Which theme? Feels easy to look at.

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน +2

      Bearded theme feat web dev Cody. It’s not out yet

    • @bijesh619
      @bijesh619 3 หลายเดือนก่อน

      ​@@WebDevCodyohh, nice one.

  • @PROJECTJMUSIC
    @PROJECTJMUSIC 3 หลายเดือนก่อน

    I personally like to fetch the user in the root layout and pass it to React Query as initialData in a context.

  • @gobeksalata
    @gobeksalata 3 หลายเดือนก่อน

    but, how does internally cache handles the weakmap to be clear when the request is over? that is the question I wonder how react server components handles that 🤔

  • @roberth8737
    @roberth8737 3 หลายเดือนก่อน

    Is this something that needs to be done with Clerk as well - or is it just next auth Lucia thing?

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน

      No clue, you’d need to check their docs or look to see if they call cache somewhere

  • @katanaut
    @katanaut 3 หลายเดือนก่อน +1

    Great content as always. Love this short but informative videos ❤

  • @tatsumii1420
    @tatsumii1420 3 หลายเดือนก่อน

    whats the diffrence between unstable_cache and cache?

  • @dailynews7822
    @dailynews7822 3 หลายเดือนก่อน

    What theme you use in VS code ?

  • @sedatdogan6057
    @sedatdogan6057 3 หลายเดือนก่อน

    What is this theme name ? i loved

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน

      Web dev Cody theme

    • @sedatdogan6057
      @sedatdogan6057 3 หลายเดือนก่อน

      ​@@WebDevCody Can i wish theme link 😂

  • @andresgutgon
    @andresgutgon 3 หลายเดือนก่อน

    So I 'm using middleware with next-auth and jwt strategy to avoid calling db because you can't unless db is serveless. i guess with react cache i can avoid middleware and use db sessions right?

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน

      Yes, it’s hard to answer this question. You cant connect to a database from your middleware because it requires serverless compatible libraries. Many database drivers are not serverless friendly, such as a postgres driver. This means that you either need to expose an api endpoint on your app and do a fetch request from your middleware to your api (which is slow and defeats the purpose of middleware running on edge), or you need to find a driver that will work (or find a way to expose your database over https publically which I’d say is a security concern). My personal opinion is to not use middleware because of added complexity and to just verify sessions on your RSC directly.

  • @ajzack983
    @ajzack983 3 หลายเดือนก่อน

    forgive my next.js react ignorance but:
    for how long does the cache holds ?
    from what I understood the cache holds until a full page relaod happens ?
    in this case why not just call the function once and put it in a global frontend state store ?

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน +1

      Idk how to answer this honestly. The cache is per request. Sometimes you need to verify values on each request on your backend. Sometimes different RSC in your tree might need the same data during your SSR, so you’d cache it server side

    • @hello19286
      @hello19286 3 หลายเดือนก่อน

      This is a different level of cache, this cache (request memoization) get evicted after the request ends. NextJS has 4 levels of caching, I know, extra confusing. You are talking about the Data cache.

    • @ajzack983
      @ajzack983 3 หลายเดือนก่อน

      ​@@hello19286I understand but I'm asking about this specific use case since we know probably the user data won't change unless maybe u change your profile or do certain actions then you refresh the user data by fetching the user again.
      Isn't this a common practice among next.js devs to have a front end state store for such use cases ?
      Fetch once, share anywhere and refresh on need.

  • @kal.leroux
    @kal.leroux 3 หลายเดือนก่อน

    how about moving things so that doesn't need to be send over rsc to client component like the button leave so if there is issue (or security issue) with the cache fct that react core team is giving us it won't affect us (I think rsc are good for small peoject but for larger project I prefer having front and backend separated so it easiest to keep track of bug and to separate concern)

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน +1

      All my buttons and forms are always client components. I’m not sure I understand what you’re asking

    • @kal.leroux
      @kal.leroux 3 หลายเดือนก่อน

      @@WebDevCody if it's client side just make a context and all your client component check from the context

  • @asustufa1515
    @asustufa1515 3 หลายเดือนก่อน

    Without stained blue it feels weird :D

  • @ThiagoVieira91
    @ThiagoVieira91 3 หลายเดือนก่อน +1

    Learned something new today. Day improved

  • @dejeandev
    @dejeandev 3 หลายเดือนก่อน

    kind of hard to imagine why this problem would exist in the first place

  • @null_spacex
    @null_spacex 3 หลายเดือนก่อน +1

    Lmao that joke at the start

  • @yuniorchavez8052
    @yuniorchavez8052 3 หลายเดือนก่อน

    how do you revalidate the cached data if you update for example the avatar of your profile, without refreshing the entire page

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน

      RevalidatePath or revalidateTag but those are unrelated to what I just discussed

  • @cod-the-creator
    @cod-the-creator 3 หลายเดือนก่อน

    I've always put that kind of stuff in global store and only do the request if the information is missing (essentially the first time the app loads). Is this not a good practice?

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน

      I mean, you should really be checking and validating the users, tokens or sessions every single request. For example, if someone invokes your server action, you need to make sure that the Paul request has a valid non-expired session so you can’t really trust that any other way, other than the look it up every time.

  • @keshavakumar9828
    @keshavakumar9828 3 หลายเดือนก่อน

    There is always something to learn from your videos. Thank you !

  • @r-i-ch
    @r-i-ch 3 หลายเดือนก่อน

    Wouldn't this be a client-only concern instead of something on the server?

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน

      Not sure what you mean

  • @z-aru
    @z-aru 3 หลายเดือนก่อน

    So cache is like useMemo but on the server?

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน +1

      Yeah

  • @mohammadzaidbhati4663
    @mohammadzaidbhati4663 3 หลายเดือนก่อน

    what about next auth where can i add react cache to make it efficient

  • @grandpaonfire6834
    @grandpaonfire6834 3 หลายเดือนก่อน

    can you not just call getcurrentuser from root and then pass the value with the context api?

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน

      You can’t access context values in RSC, but yes you could pass it down as props if you want

  • @amershboul9107
    @amershboul9107 3 หลายเดือนก่อน

    🐐

  • @LucasFariaDev
    @LucasFariaDev 3 หลายเดือนก่อน +1

    i don't think I was ever first! Learned about this recently on Josh's Comeau course, definitely a good one to keep in mind

  • @GaurangRShah
    @GaurangRShah 3 หลายเดือนก่อน

    Someone got a clickety-clack keyboard, didn’t they?

  • @2006Pk
    @2006Pk 3 หลายเดือนก่อน

    real chads use trpc + react query

  • @josippardon8933
    @josippardon8933 3 หลายเดือนก่อน

    This is cache method is not cacheing at all.
    It does request deduplication acctualy.
    If multiple compoments call this func at same time, it Will be executed only once and its return value Will be forwarded to every place where it was called.
    But yea, nothing is "cached" (saved for later)

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน +1

      Take that up with the react team 😉

  • @legoEgoJJproduction
    @legoEgoJJproduction 3 หลายเดือนก่อน

    This would be 100% easier to watch if there wasn’t loud af typing

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน

      Sorry, many people like it

  • @safarl45
    @safarl45 3 หลายเดือนก่อน +5

    Next.js sucks! Use remix!!!

    • @emmanuelezeagwula7436
      @emmanuelezeagwula7436 3 หลายเดือนก่อน +1

      It will implement RSC very soon so it doesn't change anything to be honest

  • @SeibertSwirl
    @SeibertSwirl 3 หลายเดือนก่อน

    Good job love!!! First!!

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน +3

      You’ll get your reward later tonight

    • @ziaahmad8738
      @ziaahmad8738 3 หลายเดือนก่อน +3

      @@WebDevCody woah

    • @ezwa
      @ezwa 3 หลายเดือนก่อน +2

      i was not expecting this when trying to learn react

    • @Innesb
      @Innesb 3 หลายเดือนก่อน +3

      @@WebDevCodyAwww! Cuddles and a cup of cocoa later tonight.

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน +2

      @@Innesb 🤣

  • @NizzyABI
    @NizzyABI 3 หลายเดือนก่อน

  • @ListonFermi
    @ListonFermi 3 หลายเดือนก่อน

    I use node js as backend.
    I get the similar multiple calls to verify jwt when a page loads. (So, I did the verify jwt in that middleware itself, want to know if it's a good practice .)
    I tried the cache in the middleware.ts
    const verifyToken = cache(
    async (tokenName: string, req: NextRequest): Promise => {
    ///logic
    });
    It's throwing this error
    TypeError: (0 , react__WEBPACK_IMPORTED_MODULE_2__.cache) is not a function

    • @WebDevCody
      @WebDevCody  3 หลายเดือนก่อน +1

      Verifying the jwt in the middleware is also fine. You shouldn’t need to call cache in the middleware. If the token is invalid just redirect the user to another page. I think you’d still need to call a method to get the user session inside your RSC.