Тёмный
No video :(

Next.js MISTAKE is 👀 Easy to Make! 

Dave Gray
Подписаться 333 тыс.
Просмотров 53 тыс.
50% 1

A common mistake in Next.js is attempting to fetch data from a Route Handler in a Server Component. This video shows you why!
💖 Support me on Patreon ➜ / davegray
💻 Web Dev Roadmap for Beginners (Free!): bit.ly/DaveGra...
🚀 Discord ➜ / discord
👇 Follow Me On Social Media:
GitHub: github.com/git...
X: / yesdavidgray
LinkedIn: / davidagray
Blog: www.davegray.c...

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

 

26 авг 2024

Поделиться:

Ссылка:

Скачать:

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

Добавить в:

Мой плейлист
Посмотреть позже
Комментарии : 63   
@imfad3d
@imfad3d 9 месяцев назад
We are slowly moving towards monolithic architecture again !
@Weagle1337
@Weagle1337 9 месяцев назад
We already did it
@techaddictdude
@techaddictdude 9 месяцев назад
@@Weagle1337 But why? Didn't we use to hate monolithic based code?
@SajjadHematiNourani
@SajjadHematiNourani 9 месяцев назад
I hope React comes up with an SSR solution
@imfad3d
@imfad3d 9 месяцев назад
@@SajjadHematiNourani Is't that next js is all about !!
@AkshayParkad
@AkshayParkad 7 месяцев назад
don't be surprised if we started to see php again..
@GabrielGasp
@GabrielGasp 9 месяцев назад
I agree with that as long as you are reaching to the database directly from your server components (which I particularly am not a big fan of). If your actual backend is a separate app, I like to use Next.js route handlers as a BFF for both client and server components, this way I can aggregate multiple requests and transform the data before it actually reaches the component. Imagine you have a page that needs to make fetch requests to 3 endpoints to get all the data it needs to render the page. If you move these 3 requests to a route handler and make only one request in your actual component, in my opinion this makes the code more readable and organized. This is only my opinion of course, and I know that performance-wise this it suboptimal (though the difference is negligible most of the time).
@DaveGrayTeachesCode
@DaveGrayTeachesCode 9 месяцев назад
Consider creating a function that does this and then import it in your server components and in your route handler. Write once and use it where you need it without adding an extra request.
@robertmazurowski5974
@robertmazurowski5974 7 месяцев назад
​@@DaveGrayTeachesCode can I use that in use client React components? Write a function instead of an endooint? I thought these endpoints are made for use client components.
@amansarma417
@amansarma417 9 месяцев назад
Sometimes you need to fetch data in case you have an infinite scrolling section there you definitely need to fetch again
@blaqhakym
@blaqhakym 9 месяцев назад
Thanks always for helpful contents mentor ❤
@mraviteja9539
@mraviteja9539 7 месяцев назад
Does it mean no need of backend or what?
@mathiasriissorensen6994
@mathiasriissorensen6994 9 месяцев назад
I really like the message here, but I just run into a problem when using prisma, as I would also like to use revalidateTag. I would have to use API route handlers, as I would not be able to add tags in my server components without using fetch.
@blaqhakym
@blaqhakym 9 месяцев назад
I just would like to say it here that I'm actively looking for a junior dev role. I've tried all I can to land one but nothing is happening which is definitely due to my lack years of experience. I would appreciate any help or advise please. Thanks
@viveksingh8443
@viveksingh8443 9 месяцев назад
Sir I am too struggling with the same situation
@viveksingh8443
@viveksingh8443 9 месяцев назад
Any help will be appreciated
@mergen7630
@mergen7630 9 месяцев назад
Me too bro I feel you
@user-ej7ij2mi7l
@user-ej7ij2mi7l 9 месяцев назад
Thank you for the good video. Use api route on the client side and fetch directly with DB (e.g., prisma ORM) on the server component. Is it correct that I understand? And if I fetch data from DB, does the server component automatically cache that data?
@DaveGrayTeachesCode
@DaveGrayTeachesCode 9 месяцев назад
That is correct. Your last question would be better discussed on my Discord. The short answer being "it depends".
@user-ej7ij2mi7l
@user-ej7ij2mi7l 9 месяцев назад
@@DaveGrayTeachesCode thank you
@fahadfarooq155
@fahadfarooq155 7 месяцев назад
How to revalidate the data i read from a database in a server component?
@deepjyotideb1173
@deepjyotideb1173 9 месяцев назад
So i recently found out that server components perform pretty badly when trying to upload a file
@StabilDEV
@StabilDEV 8 месяцев назад
How so
@vittoriomorellini1939
@vittoriomorellini1939 9 месяцев назад
Api from client component if we don't use server actions. I can avoid api always with Next.js 14. It is a revolution
@mehdiyahiacherif2326
@mehdiyahiacherif2326 8 месяцев назад
i still don't understand it, are those people new to web or what ? what happens when you now want to interact with a CRM or a report app like grafana or some automation tool like n8n/zapier ? you will rebuild the API routes ?
@StabilDEV
@StabilDEV 8 месяцев назад
Route handlers don't "slow things down". Also, if you need multiple frontends you will want to build a proper api.
@DaveGrayTeachesCode
@DaveGrayTeachesCode 8 месяцев назад
Route handlers don't slow things down by themselves, but making an additional request in your chain does. Just request the data directly from your server component. No need to go from server component to route handler to API or database when you can go straight from the server component to the source.
@StabilDEV
@StabilDEV 8 месяцев назад
Usually the db is hosted somewhere else while the route handler should be in the same place where the SC is rendered. Also NextJS has its own implementation of fetch so I wouldn't be surprised if the fetch call is internally stripped away altogether. Overall this fetch call inside the server component should have absolutely negligible impact, especially compared to the round trip from route handler to db. Please elaborate if you think this is wrong
@DaveGrayTeachesCode
@DaveGrayTeachesCode 8 месяцев назад
I agree that the db is usually hosted somewhere else. The route handler is indeed part of the Next.js app. I'm elaborating for you by sharing a quote and link from the docs: "Since Server Components render on the server, you don't need to call a Route Handler from a Server Component to fetch data. Instead, you can fetch the data directly inside the Server Component." Reference: nextjs.org/docs/app/building-your-application/data-fetching/fetching-caching-and-revalidating#fetching-data-on-the-client-with-route-handlers ..in addition, your route handler is being built _at the same time_ as your component during the build process. Let's say you are trying to generate SSG pages and sending a fetch request to a route handler for the page data. You will get a build error because the handler is not available during build time. Server components should request directly from the source. Putting a handler between the component and data source requires an unnecessary 2nd fetch request and is slower even if you consider the impact to be negligible.
@MChiemelie
@MChiemelie 9 месяцев назад
Can we fetch data from an API endpoint in our server component without using useEffect?
@DaveGrayTeachesCode
@DaveGrayTeachesCode 9 месяцев назад
Yes. You cannot use hooks in a server component.
@Anto-mi5pn
@Anto-mi5pn 8 месяцев назад
I have a really stupid question. I am using nextjs with a custom express server. But both my client and server files exist in the same parent directory. So what I do is, I use server typeORM classes as types in nextjs components. Is that a bad thing to do? Will my server code end up on the client if I do this?
@DaveGrayTeachesCode
@DaveGrayTeachesCode 8 месяцев назад
I think you will be interested in using the "use server" directive. Reference here: nextjs.org/docs/app/building-your-application/rendering/composition-patterns#keeping-server-only-code-out-of-the-client-environment
@your_daddy_mf
@your_daddy_mf 9 месяцев назад
Hey Dave how was your first night 😁... backend ✅
@samareshdas767
@samareshdas767 9 месяцев назад
But what If I have an express app which spits out REST or graphQL. Where to make the rqst then, considering my startup manager is dead set on nextjs and express
@DaveGrayTeachesCode
@DaveGrayTeachesCode 9 месяцев назад
_Directly from a server component_ .. A route also runs on the same server. No need to go from server component to route.
@samareshdas767
@samareshdas767 9 месяцев назад
@@DaveGrayTeachesCode thanks
@salman0ansari
@salman0ansari 9 месяцев назад
So what's the point of route and server components?
@Nexjsdeveloper
@Nexjsdeveloper 9 месяцев назад
since server component first rendering in server so you can directly connect to db ineasted of sending request over http from server to server this can make delay and decrease your speed.
@lordhuzi3316
@lordhuzi3316 9 месяцев назад
Is kt better to switch to next from express and node
@DaveGrayTeachesCode
@DaveGrayTeachesCode 9 месяцев назад
Not necessarily. It depends on your needs. I find creating a REST API with Node.js & Express can still fulfill more robust needs. NestJS is a great Node.js framework for this, too.
@wtf_jokes9639
@wtf_jokes9639 9 месяцев назад
Wait wait wait Wait I'm just learning react, are you telling me the react framework has a framework on top of it?
@xdneos
@xdneos 9 месяцев назад
Nextjs is a node JS framework, its a server that use react to render frontend
@fahadfarooq155
@fahadfarooq155 7 месяцев назад
Well technically react is a library, not a framework.
@anwaralikhan9048
@anwaralikhan9048 9 месяцев назад
May your marriage be as beautiful as your wedding day. Cheers to a love that grows stronger with each passing day!"
@IamPali2024
@IamPali2024 6 месяцев назад
Next js for frontend development is what Blazor for backend development 😂😢😅
@xtraszone
@xtraszone 9 месяцев назад
I always prefer jamstack
@DaveGrayTeachesCode
@DaveGrayTeachesCode 9 месяцев назад
Next.js is a JAMStack.
@brazenbull36
@brazenbull36 5 месяцев назад
lmao slowly going back to the good ol php way 😂
@nadyashaymardanova6000
@nadyashaymardanova6000 9 месяцев назад
💟
@LassassinX
@LassassinX 8 месяцев назад
An example would be nice
@thebugcoder3391
@thebugcoder3391 9 месяцев назад
How do I do this?
@DaveGrayTeachesCode
@DaveGrayTeachesCode 9 месяцев назад
The point is what not to do. Do not use route handlers with server components.
@thebugcoder3391
@thebugcoder3391 9 месяцев назад
@@DaveGrayTeachesCode Ah. Sorry, my question was kinda vague. But what should I do instead of making a call to route handlers in my server component? :)
@blaqhakym
@blaqhakym 9 месяцев назад
He explained that if a server component is making a request to let's say a database, it can be done directly from the component without the need to use the API route handler. But if it's a client component making the request, then it will require a route handler like you would do with plain react in a MERN stack, except in this case, you'd use the API route handler because it is safe to use with or hide environmental variables. I hope I'm right tho. I'm just a learner
@thebugcoder3391
@thebugcoder3391 9 месяцев назад
@@blaqhakym Ah, thanks!
@DaveGrayTeachesCode
@DaveGrayTeachesCode 9 месяцев назад
@@thebugcoder3391 wherever the route handler would get the data from. You can do the same with a server component without putting the route handler in the middle.
@TheBlackmanIsGod
@TheBlackmanIsGod 9 месяцев назад
Route handler? Rest APIS? Server components??? Jesus Christ I just want to make an app, then they’re like ok just learn to recode the entire internet should take you a year or 2 then you’ll be making $100k+ a year…….. I hate tech…… it’s so hard now…… Back in 2012 we would raw dog development on a live server with an “under construction” splash page up……. Times were much simpler then you could be way more creative!!! All the need for everything to be done “yesterday”, is what gave rise to frameworks which are exactly what kill creativity, but hey at least it gets done faster……
@LongJourneys
@LongJourneys 8 месяцев назад
At this point just go back to php
Далее
Non-Serializable Props in Next.js
0:59
Просмотров 8 тыс.
Svelte 5's Secret Weapon: Classes + Context
18:14
Просмотров 18 тыс.
HOW DID SHE DECIDE TO DO THIS?!
00:27
Просмотров 7 млн
The Story of Next.js
12:13
Просмотров 566 тыс.
What Theo Won't Tell You About Next.js
8:37
Просмотров 87 тыс.
No Code App Development is a Trap
9:31
Просмотров 220 тыс.
I tried 8 different Postgres ORMs
9:46
Просмотров 406 тыс.
I Use Notion to Run 17 Businesses - Here’s How
9:04
NVIDIA Needs to STOP - RTX 3050 & Misleading Branding
11:35
When RESTful architecture isn't enough...
21:02
Просмотров 272 тыс.
My Weird Journey To Next.js
24:28
Просмотров 48 тыс.
Why I Don't Use NextJS For My Side Project Anymore
6:51
HOW DID SHE DECIDE TO DO THIS?!
00:27
Просмотров 7 млн