Menu

Nine ways this app says "only once", from one request to for ever
📰

Nine ways this app says "only once", from one request to for ever

DEV Community: typescript·Daniel Pertu·5 days ago
#c5nCQq5q
#devtotypescript#devto#dev#once#customer
Reading 0:00
15s threshold

Nearly every bug I have fixed in Nakodo that mattered was a thing happening twice. Two emails to the same creator. Two jobs for the same creator. Two charges, nearly. An operator alert that arrived once a day for nine days.

Over a year the app has grown nine different answers to "only once", and I had never put them side by side. They are not nine implementations of one pattern. They are nine different lengths of promise, and picking the wrong length is the actual bug. Here they are from shortest to longest.

1. Once per request: cache()

export const getUser = cache ( async ( ) : Promise < SessionUser | null > => { const supabase = await createSupabase ( ) ; const { data } = await supabase . auth . getClaims ( ) ; . . . } ) ;

Enter fullscreen mode

Exit fullscreen mode

Eleven functions in the app are wrapped in React's cache() . The promise is "once per request, no matter how many components ask", and it exists because the layout, the page and three actions all want the signed-in user.…

Continue reading — create a free account

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