Next.js Discord

Discord Forum

Tracking down why API route is called multiple times

Unanswered
Oriental Scops-Owl posted this in #help-forum
Open in Discord
Oriental Scops-OwlOP
i have successfully ported most of my nextjs app from pages folder to app folder.

i created an AuthProvider which checks if the auth cookie is set on server-side by fetching a local API route from nextjs server

it is all working well, but it seems the API route to check the cookie is being called 3 times on page load. im assuming this is just hydration related to nextjs, but was wondering if there was a way to find out exactly why its being called so many times

12 Replies

Sweat bee
I was just about to ask for this myself! I also figured that all my api requests are being triggered three times on hard load. The first one I have a cache: HIT and then I have two cache: SKIP calls... But I have no clue why this is happening?!
Oriental Scops-OwlOP
@Sweat bee strict mode will send 2 everytime. if you disable strict mode, one will go away.
but i still have 2 sending
Sweat bee
I turned off strict mode and now its sending 2 times, the first one hits the cache the second skips it
but might not be related with your problem...
Oriental Scops-OwlOP
maybe... mine is still sending twice as well so idk
Blue Picardy Spaniel
@Oriental Scops-Owl @Sweat bee did you find a fix?
This is just how react strict mode works. This problem will not exist on production.
@Clown This is just how react strict mode works. This *problem* will not exist on production.
Blue Picardy Spaniel
i’ve gathered that, however i’ve tried disabling strict mode and the issue persists
and it is an issue because my server makes external api requests and therefore i’m getting rate limited
Oriental Scops-OwlOP
uhhh i forget if mine was fixed or not honestly @Blue Picardy Spaniel i know disabling strict mode helped
Oriental Scops-OwlOP
disabling strict mode got me down to 2 per refresh. the other one is prob just from my code itself somewhere