Next.js Discord

Discord Forum

Fetch data from local nextjs /api route, but server-side

Unanswered
Expensiveee posted this in #help-forum
Open in Discord
An alternative solution would be to have all my db calls in the page.tsx directly, but I'm looking for a solution where I call /api/me and get data before the page loads

22 Replies

just use fetch in server component?
@averydelusionalperson just use fetch in server component?
Like that?
But then I'd have to specify the full url, no other solution where I can just do /api/me
And fetch server-side
why can't you do /api/me tho?
Error: Failed to parse URL from /api/me
Needs a full URL
oh right
I forgot it is server
why do you want to use fetch tho?
like what's the point?
I don't have any preferences, what's an alternative solution that is not fethcing from db directly?
Cause I wanna use the same route so if I ever change somth (in the route) it changes on all pages that uses /api/me but Ig I could make a funciton for it and import it in each page that needs /me
that's how I do
I have seperate file for
data fetching logic
Ok, I'll think this through, thank you