Data fetching pattern with a different backend?
Unanswered
New Guinea Freshwater Crocodile posted this in #help-forum
New Guinea Freshwater CrocodileOP
why is it preferred to fetch data in server side? In case of page router docs mention to use route handlers which run in server and for app router the preferred way is in server components. The issue is I’m fetching data from a backend which will be handling auth to so i wont be able to cache these fetch calls.
19 Replies
@New Guinea Freshwater Crocodile server components are technically what your api routes did in pages
The reason it's recommended is cause it's very easy to use
If you don't want to cache, you can opt out from it easily
New Guinea Freshwater CrocodileOP
why is it preferred in both cases to fetch from server side tho? isnt it an extra layer when you already have a backend server which handles both auth and data?
@New Guinea Freshwater Crocodile why is it preferred in both cases to fetch from server side tho? isnt it an extra layer when you already have a backend server which handles both auth and data?
The whole reason to use next is to fetch on the server
To do so on the client you can just use react.
Your api routes, are also on the server
New Guinea Freshwater CrocodileOP
i tried normal react but i wanted some routing features of nextjs cuz of url rewrites and stuff
i have a plan tho but idk if its a good one. we are using sessions for auth so i could maybe verify the sessions by accessing db directly and then call the fetch for data with some cache args. is this a good practice tho?
in this case i get auth and also could cache data too without compromising auth.
@Arinji The whole reason to use next is to fetch on the server
New Guinea Freshwater CrocodileOP
is it cuz of the cache capability or is there some other reasons too?
You can use env vars
Dosent require client to do heavy frtching
Much faster
@Arinji Much faster
New Guinea Freshwater CrocodileOP
ok thats one good reason enough to use server side fetching
one more question like would this put a lot of load on server side tho? if im fetching from next server?