How to call api properly?
Unanswered
Common carp posted this in #help-forum
Common carpOP
Hi, I want to implement a feature that not allow some countries access my website. If the country is not allowed, I will redirect user to a page that announce user they're not allowed to access my site. I need to call api to check the location. Should I call it in useEffectLayout inside root layout?
4 Replies
Asian paper wasp
This normally belongs to middleware. That's the place where you can intercept every user request.
You grab the user's request info, call the location checking API, and redirect, if necessary
You grab the user's request info, call the location checking API, and redirect, if necessary
Don't do that on the client-side, e.g. using
Remember, every client-side operation is done on, well, a client browser. i.e. they are all technically bypassable by the users if they know what they are doing.
useEffect.Remember, every client-side operation is done on, well, a client browser. i.e. they are all technically bypassable by the users if they know what they are doing.
Common carpOP
If I call in middleware. It will be called in every request right? Does it affect performance?
Asian paper wasp
Correct, it will be called in every request. Yet, you can add conditions or simply use the matcher to only run middleware on page requests for example.
You do want to do that on every page request though since no one stops a user from accessing an arbitrary path of your site directly
Of course, you could deploy some sort of caching. e.g. May be if the same IP address has been checked within 1 hr, then you don't really have to check it again, since that likely to coming from the same region
You do want to do that on every page request though since no one stops a user from accessing an arbitrary path of your site directly
Of course, you could deploy some sort of caching. e.g. May be if the same IP address has been checked within 1 hr, then you don't really have to check it again, since that likely to coming from the same region