app router api
Answered
<ragewire/> posted this in #help-forum
Hello i have api routes using the app router, some of my pages have to fetch from theses before they can load, but nextjs build fails before it can connect to it, how can i fix this
97 Replies
Answer
@joulev https://nextjs-faq.com/fetch-api-in-rsc
so i built my whole site, using api routes and got to rewrite it all, i hate nextjs app router lol
@<ragewire/> so i built my whole site, using api routes and got to rewrite it all, i hate nextjs app router lol
because you misunderstood how the app router works
this is not just the app router's problem. if you fetch your own api route in getStaticProps it won't work either
@joulev this is not just the app router's problem. if you fetch your own api route in getStaticProps it won't work either
ah, its my first time using nextjs api built in, i often use external api server but i decided to move to nextjs api for this project
will they work with api routes (yes right because they dont get built?) (this is hosted on a vps not serverless btw)
@<ragewire/> ah, its my first time using nextjs api built in, i often use external api server but i decided to move to nextjs api for this project
you can fetch external api server in server components just fine
@<ragewire/> what about middlewares?
why do you want to fetch your api route in middleware?
@joulev why do you want to fetch your api route in middleware?
because prisma i heard does not work in middleware
yes it works but it is only needed in extremely rare cases
@<ragewire/> because prisma i heard does not work in middleware
oh well, then yes it works
@joulev yes it works but it is only needed in extremely rare cases
for my case, i cant get around it, without some janky user experance
@<ragewire/> for my case, i cant get around it, without some janky user experance
indeed. using prisma in middleware is indeed one of those rare cases, you have no other choices
yes it works, don't worry
@joulev you can fetch external api server in server components just fine
I know, i used to do that but this is my first time moving to nextjs api router instead of having a external api server
i thought they would of work
but thinking of it now i can see why they dont
lol its 3am and i started this migration at like 1am lol
so i wasnt thinking oof
yeah it's exactly the same as what you would get if you fetched your own api route in getstaticprops
@joulev well go sleep lol
can't, broke prod lol,
@joulev yeah it's exactly the same as what you would get if you fetched your own api route in getstaticprops
wondering, i heard you can put a server action componet inside of a client componet by wrapping it?
like
'use client'
<client>
<server>
<client>
'use client'
<client>
<server>
<client>
i remeber seeing some type of blog post doing something like this
@<ragewire/> wondering, i heard you can put a server action componet inside of a client componet by wrapping it?
no you can't, you have to use the interleave pattern https://nextjs.org/docs/app/building-your-application/rendering/composition-patterns#interleaving-server-and-client-components
tldr: pass the server component as prop, instead of importing the server component in the client component
tldr: pass the server component as prop, instead of importing the server component in the client component
@joulev no you can't, you have to use the interleave pattern <https://nextjs.org/docs/app/building-your-application/rendering/composition-patterns#interleaving-server-and-client-components>
tldr: pass the server component as prop, instead of importing the server component in the client component
ah i see, i got to pass server componets in as props if needed
im trying to slowly move to more server based stuff, but my code is pretty 'use client' heavy lol since its old and lots of code. so its a bit of a pain to recode some stuff
@joulev its not too bad, since must of the api code i wrote is just calling a external service so its not to bad to just move it over to a non api route
yeah. it's mainly just copy and paste things around, i don't expect fixing this bug to take long even for big apps
@joulev yeah. it's mainly just copy and paste things around, i don't expect fixing this bug to take long even for big apps
yeah, since im dumb and may of tried to force update prod so thats down so i got to do that quicky so im just pasting it into the routes lol (going to fix it tmr lol) its not the main verison but some users use it but 95% are sleeping so not to bad, thats why i get my unpaid discord support to handle the tickets
lol
thank god for me, i follow the dry, so i put must of my code into functions and my api calls basicly just ran functions and formated the data to return as a api
so must of my code rn is just copy and pasting one function
dont do what i do, move stuff over to a new thing at 3am
and then hit the force merge button
on prod lol
@joulev hey thanks for helping and have a good night/day i just moved everything and just pushed to prod and it worked, is it messy yes, are have of the api routes still there because thoses work yes, thats a tmr me issue lol
is there 100 @ts-ignore yes lol
but i am tried
yeah glad it works, time to go sleep haha, it's important
fixing those ts-ignore is for tomorrow
or should i say, later today
i got tihs same code but with movies instead of tv shows like 3 times lol
but im too tried to but it into a function and handle errors
just returing 500 page error for any issues incase they happen
lol
yaaaaa it rended
im so happy
thank god
@joulev i hate my life, why did i choose to learn coding, now cloudflare is giving me issues lol
well this one i cant help lol. good luck
@joulev well this one i cant help lol. good luck
you know what coolify is?
its a build mgnt system
and it handles ssl certs for domains automaticly
well for some reaosn during my mid life issue
it reseted my domain config for that instance lol
this was blank and i didnt noticed
but simple issue
just retype in domain
press enter, rebuild and should be good
awesome
@joulev awesome
sometimes coding can really suck, i had a hydration error where, i could not figure it out, the error message / line number was off (it poitned to a usestate) and i was wtf. and it wouldt happen all the time, so basicly in my nav bar i have search and i have a local storage to save what oyu where typgin and the state would be loaded which caused it
i really wished they had a option to just diable hydration errors for a layout or what ever, even tho that would be cause really bad code, i woudl make me happy when debugging another error lol
@<ragewire/> sometimes coding can really suck, i had a hydration error where, i could not figure it out, the error message / line number was off (it poitned to a usestate) and i was wtf. and it wouldt happen all the time, so basicly in my nav bar i have search and i have a local storage to save what oyu where typgin and the state would be loaded which caused it
https://usehooks-ts.com/react-hook/use-local-storage
const [value, setValue, removeValue] = useLocalStorage('test-key', 0, {
// needed for hydration to work smoothly
initializeWithValue: false,
});@<ragewire/> i really wished they had a option to just diable hydration errors for a layout or what ever, even tho that would be cause really bad code, i woudl make me happy when debugging another error lol
suppressHydrationWarning https://react.dev/reference/react-dom/client/hydrateRoot#suppressing-unavoidable-hydration-mismatch-errors since it's 3am
tomorrow migrate to a SSR-safe version
@joulev suppressHydrationWarning <https://react.dev/reference/react-dom/client/hydrateRoot#suppressing-unavoidable-hydration-mismatch-errors> since it's 3am
so i had a nav componet and i had put that in ever element and it still did that
untill i just ctrl + / and disabled the feature to save what youw here typing utnill tmr
no it only works one level deep, so you need to put it in every single html element that uses the value from localStorage
the code is from a vue code base, which was super old, written by a different dev, used a different css lib other then tailwind, wasnt ts lol
i migrated all that to nextjs 😦 and i hate my life
oh you already did well. migrating is no easy job, migrating from the pages router to the app router alone is already a major project that can require many people to finish
@joulev oh you already did well. migrating is no easy job, migrating from the pages router to the app router alone is already a major project that can require many people to finish
oh i migrated that old code base from vue to the pages and then app router, basicly all client and now to more server lol
@joulev oh you already did well. migrating is no easy job, migrating from the pages router to the app router alone is already a major project that can require many people to finish
the 3am problems end up getting solved by chatgpt im ngl, when its 3am and you habe to get something fixed, chatgpty is there to help
lol
im happy now
@joulev thanks for the help btw
ima head to bed
its 3:49am i got to be up at 7 something 😦 for racing lol
@joulev i wont see this untill tmr but do you use any server comonet / action libs?
you're welcome, good night good sleep, though avoid whatever event at 7am if possible, 2 hours of sleep won't help with debugging and logical thinking
been out all day since then lol
and now i got to work on more migration
😦