diff --git a/packages/supabase/src/client/middleware.ts b/packages/supabase/src/client/middleware.ts index 0a40f1f1c5..94dc89ec99 100644 --- a/packages/supabase/src/client/middleware.ts +++ b/packages/supabase/src/client/middleware.ts @@ -3,7 +3,7 @@ import type { NextRequest, NextResponse } from "next/server"; export async function updateSession( request: NextRequest, - response: NextResponse + response: NextResponse, ) { const supabase = createServerClient( process.env.NEXT_PUBLIC_SUPABASE_URL!, @@ -22,7 +22,7 @@ export async function updateSession( response.cookies.set({ name, value: "", ...options }); }, }, - } + }, ); await supabase.auth.getUser(); diff --git a/packages/supabase/src/queries/cached-queries.ts b/packages/supabase/src/queries/cached-queries.ts index 136199948b..8375fad157 100644 --- a/packages/supabase/src/queries/cached-queries.ts +++ b/packages/supabase/src/queries/cached-queries.ts @@ -61,23 +61,25 @@ export const getTransactions = async ( }, ["transactions", teamId], { - revalidate: 180, + revalidate: 600, tags: [`transactions_${teamId}`], }, )(params); }; +// Cache per request export const getSession = cache(async () => { const supabase = createClient(); - return supabase.auth.getSession(); + return supabase.auth.getUser(); }); -export const getUser = async () => { +// Cache per request and revalidate every 30 minutes +export const getUser = cache(async () => { const { - data: { session }, + data: { user }, } = await getSession(); - const userId = session?.user?.id; + const userId = user?.id; if (!userId) { return null; @@ -92,10 +94,11 @@ export const getUser = async () => { ["user", userId], { tags: [`user_${userId}`], - revalidate: 180, + // 30 minutes, jwt expires in 1 hour + revalidate: 1800, }, - )(userId); -}; + )(); +}); export const getTeamUser = async () => { const supabase = createClient(); @@ -111,7 +114,7 @@ export const getTeamUser = async () => { ["team", "user", data.id], { tags: [`team_user_${data.id}`], - revalidate: 180, + revalidate: 1800, }, )(data.id); }; @@ -349,7 +352,7 @@ export const getTrackerProjects = async ( ["tracker_projects", teamId], { tags: [`tracker_projects_${teamId}`], - revalidate: 180, + revalidate: 3600, }, )(params); }; @@ -372,7 +375,7 @@ export const getTrackerRecordsByRange = async ( ["tracker_entries", teamId], { tags: [`tracker_entries_${teamId}`], - revalidate: 180, + revalidate: 3600, }, )(params); }; @@ -597,7 +600,7 @@ export const getTags = async () => { ["tags", teamId], { tags: [`tags_${teamId}`], - revalidate: 180, + revalidate: 3600, }, )(); };