Menu

Two Postgres error codes are the only ones we retry, and the retry sits underneath a React cache()
📰

Two Postgres error codes are the only ones we retry, and the retry sits underneath a React cache()

DEV Community: nextjs·Daniel Pertu·4 days ago
#gn2eZzEJ
#devtonextjs#devto#dev#venue#call
Reading 0:00
15s threshold

Every signed-in page in PubTrivia needs the same two facts before it can render anything: who you are, and which venue you run. The layout needs them to draw the header. The page needs them to query. Any server action fired from that page needs them again to authorise itself.

Both facts are expensive in different ways. The first is a network round trip to an auth server. The second is up to two Postgres queries over a connection that may have just been created from cold. So both of them live in one 77 line file, and that file contains the only retry loop in the repository.

The auth call is a network call, 33 times

export const getAuthenticatedUser = cache ( async () => { const supabase = await createClient () const { data : { user }, error } = await supabase . auth . getUser () if ( error || ! user ) redirect ( ' /login ' ) return user })

Enter fullscreen mode

Exit fullscreen mode

cache is React's per-request memo, not a cross-request cache.…

Continue reading — create a free account

Join HashtagPLUS to read full articles, follow hashtags, vote, and join the conversation.