SupabaseS
Supabase•3y ago
Ruyy

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:

import { createSupabaseServerClient } from "@/lib/database/server"
import { cookies } from "next/headers"
import { cache } from "react"


export const getAuthedUser = cache(async () => {
  const cookieStore = cookies()
  const supabase = createSupabaseServerClient(cookieStore)

  const { data: { user } } = await supabase.auth.getUser()

  if (!user) {
    return null;
  }

  return user;
})


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?

}
 [AuthApiError: Rate limit exceeded] {
  __isAuthError: true,
  name: 'AuthApiError',
  status: 429
}


I'd appreciate any insights or suggestions on how to handle this more efficiently. Thanks in advance! 🙌
Was this page helpful?