NextJs Supabase SSR rate limit with cache wrapper...
Hey everyone 👋,
I've been working with Supabase in my Next.js projects and ran into an issue with constant refetching, leading to rate limits (HTTP 429). I'm using the @supabase/ssr package, and my goal is to reduce the number of requests by caching the authenticated user data.
I've implemented a cached function in Next.js to get the authenticated user data like this:
However, despite caching, I'm still facing constant refetching and subsequent rate-limiting issues. How can I optimize this to bypass the constant refetching and prevent rate limits?
I'd appreciate any insights or suggestions on how to handle this more efficiently. Thanks in advance! 🙌
9 Replies
Normally the supabase clients should not do an auth request until the access_token is close to expiring. I don't know if getUser counts towards the rate limits (I don't think so) but it always goes to the server versus getSession which only does to refresh. What is your jwt expire time set to?
I am doing this all on server side
My ultimate goal with this is to get the authed user id and name to send to other places...
I've not seen this issue for SSR in moderating, but don't read them in detail as I'm no up on auth-helpers. But it would surprise if normal operation causes rate errors.
Here is a use-case example:
Is your jwt expire set to 1hour default?
Yep. Haven't changed the default
Funny thing is: I reverted my getAuthedUser function to this pattern and the rate limit is gone...
Something seems wrong.
You could try getSession instead of getUser, I guess, but you are probably more familiar with the guides on SSR than I am. getUser goes to the server each time, but like I said before I don't think that involves a rate limit. getSession will return the user object and not go to the server unless it is expired.
You could try getSession instead of getUser, I guess, but you are probably more familiar with the guides on SSR than I am. getUser goes to the server each time, but like I said before I don't think that involves a rate limit. getSession will return the user object and not go to the server unless it is expired.
Ohhh lets try then
If you keep getting rate limits look at the auth or API Edge logs and see what is being called too often. Maybe just some coding error. I'm out for the night. Good luck.