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
American black bearOP
Whenver I load
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
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 itself4 Replies
@American black bear 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
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
@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
So I am unable to move the logic out of that route into the page
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 routeSo I am unable to move the logic out of that route into the page
@American black bear 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
in that case i don't know, i don't use next-auth. you might want to update the question to reflect this
American black bearOP
Will do