supabase icon indicating copy to clipboard operation
supabase copied to clipboard

`serverSupabaseUser` is very slow (adds about 200-400ms)

Open madebyfabian opened this issue 1 year ago • 8 comments

Hi there, first of all, this is an awesome module, helps me much to build on nuxt3 and supabase. I noticed something strange. In nuxt server api routes, when using serverSupabaseUser like in the docs, this adds about 300-400ms to a request. I deployed the exact same code with and without serverSupabaseUser and the request with it took on average 550ms, and without it only 150ms on average.

With

Here is my test implementation (/api/test) which takes about 550ms:

import { serverSupabaseUser } from '#supabase/server'

export default defineEventHandler(async event => {
	const startTime = +new Date()
	console.log(startTime, 'start')
	const serverAuthUser = await serverSupabaseUser(event)
	if (!serverAuthUser) return sendError(event, createError({ statusCode: 401 }))

	console.log(+new Date(), 'end', { took: +new Date() - startTime })
	return true
})

Bildschirm­foto 2022-11-27 um 12 25 24

The logs:

1669548455166 start
1669548455296 end { took: 130 }
1669548423842 start
1669548424159 end { took: 317 }
1669548421038 start
1669548421166 end { took: 128 }
1669548418952 start
1669548419071 end { took: 119 }

Without

And here without it, just removed the serverSupabaseUser

export default defineEventHandler(async event => {
	const startTime = +new Date()
	console.log(startTime, 'start')

	console.log(+new Date(), 'end', { took: +new Date() - startTime })
	return true
})

Bildschirm­foto 2022-11-27 um 12 32 46

The logs:

1669548794478 start
1669548794478 end { took: 0 }
1669548791982 start
1669548791982 end { took: 0 }
1669548789446 start
1669548789446 end { took: 0 }
1669548786949 start
1669548786949 end { took: 0 }

Version

@nuxtjs/supabase: 0.3.0 nuxt: 3.0.0

Reproduction Link

Use the temporary-test-routes branch from this repo: https://github.com/madebyfabian/zingio/tree/temporary-test-routes

Steps to reproduce

  1. Deploy new nuxt project with nuxt/supabase to for example vercel
  2. Create a /server/api/test.ts with the contents above
  3. Deploy it with and without useSupabaseUser

What is Expected?

That useSupabaseUser only reads cookies and should only take minimal time. a

What is actually happening?

I suppose every useSupabaseUser call calls supabase.auth.getUser(), which loads the user object live from the database all the time. This does not seem to be an efficient solution, since it's adding many time to basically all requests that use it, for example only to validate if the current user id is allowed to access specific data.

madebyfabian avatar Nov 27 '22 11:11 madebyfabian