Optimize data fetching with the new `cache` function in NextJs

Поділитися
Вставка
  • Опубліковано 21 гру 2024

КОМЕНТАРІ • 37

  • @rogerscript
    @rogerscript 9 місяців тому +4

    You are my new hero! best vibes from Nicaragua!

    • @hamedbahram
      @hamedbahram  9 місяців тому

      Thank you! Welcome onboard...

  • @sergheic31
    @sergheic31 9 місяців тому +1

    Great explanation Hamed! Definitely an important topic to understand. You made it very clear using good examples! Thanks!

  • @KeithClark-i7p
    @KeithClark-i7p 9 місяців тому +1

    Hi Hamed, thanks for the great info, I love the channel!
    Question for you: Let's say I have quite a few inputs to a cached function. Passing an object into a cached function causes cache misses unless you are passing the same object reference. Passing all parameters individually seems like it could be an anti-pattern or at the very least get pretty ugly. Do you have any tips on how to avoid something like cache((param1, param2, param3, param4, ...etc) => {...})?

    • @hamedbahram
      @hamedbahram  9 місяців тому

      Thanks! That's a good question. If your arguments are not primitives, you have to make sure you’re passing the same object reference. See this example in the docs → react.dev/reference/react/cache#memoized-function-still-runs

  • @SamKing-i6e
    @SamKing-i6e 9 місяців тому +1

    Hi Hamed, thorough explanation again. Im wondering what would be the difference here between this new cache feature from react and Next.js's built in de-duping? They both eliminate redundant API calls in the same render pass?

    • @hamedbahram
      @hamedbahram  9 місяців тому

      Thanks! NextJs doesn't have a built-in de-duping yet. The current deduplication in NextJs is all React in action :) There will be the `unstable_cache` from `next/cache` in future which is similar to what React does but for now we only have React memoization.

  • @Beast80K
    @Beast80K 8 місяців тому

    I have a very unique question regarding caching, I want to cache api route. Such that I don't want every user initiating new request.
    For ex. api1 has revalidate 20sec,
    If user1 fetches at 07:00:00 from api1 he gets new data, similarly when user2 fetches api1 at 07:00:01 he should get cached data.

    • @hamedbahram
      @hamedbahram  8 місяців тому

      GET route handlers are static by default, so if you're not using any request related information such as headers, cookies, etc. it would be cached. If you want to revalidate the cached data on intervals you can use the `export const revalidate = 20` to revalidate every 20 seconds.

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

    hi, how can i revalidate a caching db queries

  • @Beast80K
    @Beast80K 9 місяців тому +1

    *Respected Sir,*
    I have a doubt, what can be done if external API used is currently down, how can we shift in maintenance mode or something like that ?

    • @hamedbahram
      @hamedbahram  9 місяців тому

      You can catch the in the nearest error boundary or the `catch` function on the Promise you're passing to the `use` function.

    • @Beast80K
      @Beast80K 9 місяців тому

      @@hamedbahram Thanks, I handled that error in custom error boundary (i got confused because i was using SWR library's error object). But now, I'm experiencing a weird problem with API route handlers, my API route gets data from external API (locally hosted using mockoon tool), I'm using *{ next: { revalidate: 60} }* to get fresh data after 60 seconds & also *'force-dynamic'* to prevent it from becoming static during build. External API returns errors like 404, 429, 503 but API route handler responds with cached data & never returns a error, even other errors caught in catch block are ignored. Any suggestions ?

  • @sumitsahu8657
    @sumitsahu8657 9 місяців тому +1

    Thank you for the valuable tutorials ❤

  • @hasancheraghi2800
    @hasancheraghi2800 9 місяців тому +1

    Thank u Hamed

  • @prashlovessamosa
    @prashlovessamosa 9 місяців тому +2

    Sir can you please share your next js notes it would be super helpful.

    • @hamedbahram
      @hamedbahram  9 місяців тому

      The notes are part of the NextJs course material.

  • @aymenbachiri-yh2hd
    @aymenbachiri-yh2hd 3 місяці тому +1

    thank you so much

  • @abuzain859
    @abuzain859 29 днів тому

    great

  • @prashlovessamosa
    @prashlovessamosa 9 місяців тому +1

    Thanks Sir.

  • @MiladJoodi
    @MiladJoodi 9 місяців тому +1

    This is better than docs

  • @jimmy.im-kp
    @jimmy.im-kp 6 місяців тому

    awesome ! thanks!

  • @codechange75
    @codechange75 9 місяців тому +1

    It is a good video. Something learn good. Sir, can you please make video on next js frountend and node js backend authentication using next js middleware and protect route base on roles admin user and sub admin

    • @hamedbahram
      @hamedbahram  9 місяців тому

      Thanks! I'm working on auth related videos, I'll have that in mind.

  • @redwansikder3847
    @redwansikder3847 9 місяців тому +1

    Great

  • @LehmannMr
    @LehmannMr 9 місяців тому +1

    I found a bug !!!!! There's a hole in your shirt 👚 👚

    • @hamedbahram
      @hamedbahram  9 місяців тому

      😅 yeah that's how the sleeves are on my hoody.

  • @hellokevin_133
    @hellokevin_133 9 місяців тому +1

    Hey man, do I need to know a programming language other than Javascript for landing my first role? I'm based in the middle east, I'm sure you know how much of a mess it is lol.

    • @hamedbahram
      @hamedbahram  9 місяців тому

      Not really! Html, css, JS/TS plus a framework like React/NextJs can get you in, but definitely learn TypeScript, and up your knowledge of different databases before attempting to learn a new language.