Skip to content

createServerClient causes too many /token requests #1048

@Jordaneisenburger

Description

@Jordaneisenburger

Bug report

  • I confirm this is a bug with Supabase, not with my own application.
  • I confirm I have searched the Docs, GitHub Discussions, and Discord.

Describe the bug

When doing a query to supabase using the createServerClient it causes /token to be requested 4 times. This causes Rate limit issues when for example on localhost I cntrl + reload the page 10 times quickly we already hit that limit

To Reproduce

Steps to reproduce the behavior, please provide code snippets or a repository:

  • Create a new nextjs app (app router)
  • Create a utils/supabase/server.ts file with contents below
// This code comes from https://supabase.com/docs/guides/auth/server-side/nextjs
import { createServerClient, type CookieOptions } from '@supabase/ssr'
import { cookies } from 'next/headers'

export function createClient() {
  const cookieStore = cookies()

  return createServerClient(
    process.env.NEXT_PUBLIC_SUPABASE_URL!,
    process.env.NEXT_PUBLIC_SUPABASE_ANON_KEY!,
    {
      cookies: {
        get(name: string) {
          return cookieStore.get(name)?.value
        },
        set(name: string, value: string, options: CookieOptions) {
          try {
            cookieStore.set({ name, value, ...options })
          } catch (error) {
            // The `set` method was called from a Server Component.
            // This can be ignored if you have middleware refreshing
            // user sessions.
          }
        },
        remove(name: string, options: CookieOptions) {
          try {
            cookieStore.set({ name, value: '', ...options })
          } catch (error) {
            // The `delete` method was called from a Server Component.
            // This can be ignored if you have middleware refreshing
            // user sessions.
          }
        },
      },
    }
  )
}
  • Adjust the homepage to look like file below (query whatever table you want just make sure to use the id)
import { createClient } from '@/utils/supabase/server/getSupabaseClient';

export default async function Home() {
    const supabase = createClient();
    const res = await supabase.auth.getUser();

    const { data, error } = await supabase
        .from('profiles')
        .select('name')
        .eq('id', res.data?.user?.id!)
        .single();

    return 'something';
}
  • build the nextjs project & start the nextjs production server
  • Load the page and inspect the Supabase Auth logs
    Screenshot 2024-05-15 at 18 18 13

So from my observations it seems that await supabase.auth.getUser(); causes 2 /token requests and await supabase .from('profiles') is doing 2 additional /token requests

The /user is also happening twice but this isn't causing problems for now but I don't think should happen either.

Expected behavior

I'd expect the queries to only happen once

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugSomething isn't working

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions