Next.js Discord

Discord Forum

Idiomatic way to always fetch API route when loading page route (without moving logic to page)

Unanswered
American black bear posted this in #help-forum
Open in Discord
American black bearOP
Whenver I load app/someRoute/page.tsx, I want the data GETted from app/api/someData/route.ts to always be made available to the component/page.
I can make a fetch request of course, but is there a proper Next way to handle this?
I am using NextAuth so the data must be GETted from api/auth/csrf, so the logic cannot just be moved into the page itself

4 Replies

@joulev yes, by moving the logic of your route handler inside the page itself. <https://nextjs-discord-common-questions.joulev.dev/fetching-own-api-endpoint-in-react-server-components>
American black bearOP
Haha apologies for asking a common question, I tried searching but clearly didn't use the right terms.
Another apology as I thought this would be a generic route question, but what if you do not control the logic of that route? I would have tagged this as NextAuth if I'd known it wasn't generic, but I am forced to GET a csrfToken from /api/auth/csrf route
So I am unable to move the logic out of that route into the page
American black bearOP
Will do