Тёмный
No video :(

The most important function to write performant next.js apps 

Web Dev Cody
Подписаться 233 тыс.
Просмотров 17 тыс.
50% 1

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

Опубликовано:

 

5 сен 2024

Поделиться:

Ссылка:

Скачать:

Готовим ссылку...

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 116   
@WebDevCody
@WebDevCody 2 месяца назад
Said hook, but meant function. Don’t kill me
@naughtiousmaximus7853
@naughtiousmaximus7853 2 месяца назад
Good Cody: Uses Bearded Blue theme Evil Cody: Doesnt use Bearded Blue theme
@WebDevCody
@WebDevCody 2 месяца назад
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 2 месяца назад
@@WebDevCody that's great lol
@kaansal9523
@kaansal9523 2 месяца назад
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.
@windwardhive7363
@windwardhive7363 2 месяца назад
rsc in the mud. react query ftw
@StaleDegree
@StaleDegree 2 месяца назад
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 2 месяца назад
From nextjs 15 it wont be the default behaviour.
@daphenomenalz4100
@daphenomenalz4100 2 месяца назад
Would that be a security issue?
@hello19286
@hello19286 2 месяца назад
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 2 месяца назад
Often you can’t use fetch to get data from a database. You usually use a database driver.
@Vantivify
@Vantivify 2 месяца назад
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 2 месяца назад
@@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.
@Ivcota
@Ivcota 2 месяца назад
straight up "I just know nextjs thumbnail" right here
@randohundo9795
@randohundo9795 2 месяца назад
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.
@timekouka
@timekouka 2 месяца назад
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!!
@nasko235679
@nasko235679 2 месяца назад
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 2 месяца назад
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 2 месяца назад
@@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 2 месяца назад
​@@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.
@jfhandfield
@jfhandfield 2 месяца назад
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 2 месяца назад
Cache is specifically for your RSC, context is for your client components. It’s different imo
@JS_Jordan
@JS_Jordan 2 месяца назад
Thanks man! Learned something new
@slemchik03
@slemchik03 Месяц назад
Awesome video. But do next-auth/clerk cache the session request for me or do I need to do it manually?
@null_spacex
@null_spacex 2 месяца назад
Been going through exactly this with my next.js supabase app
@daphenomenalz4100
@daphenomenalz4100 2 месяца назад
gengar!!
@katanaut
@katanaut 2 месяца назад
Great content as always. Love this short but informative videos ❤
@szymon7932
@szymon7932 2 месяца назад
What keyboard do you use? It sounds amazing
@WebDevCody
@WebDevCody 2 месяца назад
Klack.app
@Silver-fh4fb
@Silver-fh4fb 2 месяца назад
Cody did a video on the app ru-vid.com/video/%D0%B2%D0%B8%D0%B4%D0%B5%D0%BE-AJDSZcv27JI.html
@sameerahmedk
@sameerahmedk 2 месяца назад
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 2 месяца назад
Curious as well
@JayellsNext
@JayellsNext Месяц назад
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 😅
@sinatofangsaz2209
@sinatofangsaz2209 2 месяца назад
wow this one was super helpful. thanks!
@shacodes
@shacodes 2 месяца назад
may be create a hook with Context API, that will use same instance
@ThiagoVieira91
@ThiagoVieira91 2 месяца назад
Learned something new today. Day improved
@keshavakumar9828
@keshavakumar9828 2 месяца назад
There is always something to learn from your videos. Thank you !
@fanush3754
@fanush3754 Месяц назад
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 Месяц назад
If you’re on a page that uses a cookie the page will always be dynamic
@aymenbachiri-yh2hd
@aymenbachiri-yh2hd Месяц назад
Thank you so much
@null_spacex
@null_spacex 2 месяца назад
Lmao that joke at the start
@JoshIbbotson
@JoshIbbotson 2 месяца назад
Hey Cody, what keyboard do you have? It sounds amazing
@WebDevCody
@WebDevCody 2 месяца назад
Klack app
@gobeksalata
@gobeksalata 2 месяца назад
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 🤔
@PROJECTJMUSIC
@PROJECTJMUSIC 2 месяца назад
I personally like to fetch the user in the root layout and pass it to React Query as initialData in a context.
@devaccess
@devaccess 2 месяца назад
Thank you so much Cody, Please which extension are you using for your error to show directly on the editot?
@WebDevCody
@WebDevCody 2 месяца назад
Error lens
@agustind
@agustind 2 месяца назад
Does anybody know which keyboard is Cody using? it sounds awesome
@alaskandonut
@alaskandonut 2 месяца назад
Maybe a custom with linear switches. Maybe tactiles.
@h4nto772
@h4nto772 2 месяца назад
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
@LucasFariaDev
@LucasFariaDev 2 месяца назад
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
@myonlylovejesus887
@myonlylovejesus887 2 месяца назад
wow a new theme. what is the name of this theme? really like the green accent.
@bijesh619
@bijesh619 2 месяца назад
Which theme? Feels easy to look at.
@WebDevCody
@WebDevCody 2 месяца назад
Bearded theme feat web dev Cody. It’s not out yet
@bijesh619
@bijesh619 2 месяца назад
​@@WebDevCodyohh, nice one.
@asustufa1515
@asustufa1515 2 месяца назад
Without stained blue it feels weird :D
@yuniorchavez8052
@yuniorchavez8052 2 месяца назад
how do you revalidate the cached data if you update for example the avatar of your profile, without refreshing the entire page
@WebDevCody
@WebDevCody 2 месяца назад
RevalidatePath or revalidateTag but those are unrelated to what I just discussed
@dejeandev
@dejeandev 2 месяца назад
kind of hard to imagine why this problem would exist in the first place
@kal.leroux
@kal.leroux 2 месяца назад
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 2 месяца назад
All my buttons and forms are always client components. I’m not sure I understand what you’re asking
@kal.leroux
@kal.leroux 2 месяца назад
@@WebDevCody if it's client side just make a context and all your client component check from the context
@ajzack983
@ajzack983 2 месяца назад
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 2 месяца назад
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 2 месяца назад
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 2 месяца назад
​@@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.
@tatsumii1420
@tatsumii1420 2 месяца назад
whats the diffrence between unstable_cache and cache?
@cod-the-creator
@cod-the-creator 2 месяца назад
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 2 месяца назад
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.
@roberth8737
@roberth8737 2 месяца назад
Is this something that needs to be done with Clerk as well - or is it just next auth Lucia thing?
@WebDevCody
@WebDevCody 2 месяца назад
No clue, you’d need to check their docs or look to see if they call cache somewhere
@andresgutgon
@andresgutgon 2 месяца назад
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 2 месяца назад
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.
@dailynews7822
@dailynews7822 2 месяца назад
What theme you use in VS code ?
@r-i-ch
@r-i-ch 2 месяца назад
Wouldn't this be a client-only concern instead of something on the server?
@WebDevCody
@WebDevCody 2 месяца назад
Not sure what you mean
@codingwithjamal
@codingwithjamal 2 месяца назад
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 2 месяца назад
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 2 месяца назад
@@WebDevCody makes sense, i see RSC has replaced a lot of the old fetch paradigms
@grandpaonfire6834
@grandpaonfire6834 2 месяца назад
can you not just call getcurrentuser from root and then pass the value with the context api?
@WebDevCody
@WebDevCody 2 месяца назад
You can’t access context values in RSC, but yes you could pass it down as props if you want
@sedatdogan6057
@sedatdogan6057 2 месяца назад
What is this theme name ? i loved
@WebDevCody
@WebDevCody 2 месяца назад
Web dev Cody theme
@sedatdogan6057
@sedatdogan6057 2 месяца назад
​@@WebDevCody Can i wish theme link 😂
@2006Pk
@2006Pk 2 месяца назад
real chads use trpc + react query
@mohammadzaidbhati4663
@mohammadzaidbhati4663 2 месяца назад
what about next auth where can i add react cache to make it efficient
@mohammadzaidbhati4663
@mohammadzaidbhati4663 2 месяца назад
i am also using convex for backend
@josippardon8933
@josippardon8933 2 месяца назад
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 2 месяца назад
Take that up with the react team 😉
@z-aru
@z-aru 2 месяца назад
So cache is like useMemo but on the server?
@WebDevCody
@WebDevCody 2 месяца назад
Yeah
@legoEgoJJproduction
@legoEgoJJproduction 2 месяца назад
This would be 100% easier to watch if there wasn’t loud af typing
@WebDevCody
@WebDevCody 2 месяца назад
Sorry, many people like it
@amershboul9107
@amershboul9107 2 месяца назад
🐐
@GaurangRShah
@GaurangRShah 2 месяца назад
Someone got a clickety-clack keyboard, didn’t they?
@SeibertSwirl
@SeibertSwirl 2 месяца назад
Good job love!!! First!!
@WebDevCody
@WebDevCody 2 месяца назад
You’ll get your reward later tonight
@ziaahmad8738
@ziaahmad8738 2 месяца назад
@@WebDevCody woah
@ezwa
@ezwa 2 месяца назад
i was not expecting this when trying to learn react
@Innesb
@Innesb 2 месяца назад
@@WebDevCodyAwww! Cuddles and a cup of cocoa later tonight.
@WebDevCody
@WebDevCody 2 месяца назад
@@Innesb 🤣
@safarl45
@safarl45 2 месяца назад
Next.js sucks! Use remix!!!
@emmanuelezeagwula7436
@emmanuelezeagwula7436 2 месяца назад
It will implement RSC very soon so it doesn't change anything to be honest
@ListonFermi
@ListonFermi 2 месяца назад
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 2 месяца назад
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.
@NizzyABI
@NizzyABI 2 месяца назад
@WebDevCody
@WebDevCody 2 месяца назад
Далее
How to learn coding faster
2:58
Просмотров 1,4 тыс.
ДО ВСТРЕЧИ НА РАЗГОНЕ
52:11
Просмотров 453 тыс.
Why I still choose next.js
19:39
Просмотров 19 тыс.
Fetching Data Doesn't Get Better Than This
6:58
Просмотров 113 тыс.
How I structure my next.js applications
23:19
Просмотров 27 тыс.
I earn $137/day with this SIMPLE tech stack
5:42
Просмотров 4,6 тыс.
10 common mistakes with the Next.js App Router
20:37
Просмотров 209 тыс.
Learn Next.js Intercepting Routes In 11 Minutes
11:08
This is the coolest side project I've worked on
19:18
The Story of Next.js
12:13
Просмотров 573 тыс.