Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add retries to Supabase client #382

Merged
merged 15 commits into from
Aug 30, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/runtime/plugins/supabase.client.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createBrowserClient } from '@supabase/ssr'
import type { Session } from '@supabase/supabase-js'
import { fetchWithRetry } from '../utils/fetch-retry'
import { defineNuxtPlugin, useRuntimeConfig, useSupabaseSession, useSupabaseUser } from '#imports'

export default defineNuxtPlugin({
Expand All @@ -12,6 +13,10 @@ export default defineNuxtPlugin({
...clientOptions,
cookieOptions,
isSingleton: true,
global: {
fetch: fetchWithRetry,
...clientOptions.global,
},
})

const currentSession = useSupabaseSession()
Expand All @@ -26,6 +31,7 @@ export default defineNuxtPlugin({

// Updates the session and user states through auth events
client.auth.onAuthStateChange((_, session: Session | null) => {
client.realtime.setAuth(session?.access_token ?? null)
Copy link
Collaborator

@larbish larbish Aug 29, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is this line for? I can't find any documentation about this realtime.setAuth method.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sorry, this slipped in there because there is a realtime bug that I am debugging with the Supabase team where the auth token does not correctly propagate to realtime. Will take it out for now

if (JSON.stringify(currentSession.value) !== JSON.stringify(session)) {
currentSession.value = session
currentUser.value = session?.user ?? null
Expand Down
5 changes: 5 additions & 0 deletions src/runtime/plugins/supabase.server.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { createServerClient, parseCookieHeader } from '@supabase/ssr'
import { getHeader, setCookie } from 'h3'
import { fetchWithRetry } from '../utils/fetch-retry'
import { defineNuxtPlugin, useRequestEvent, useRuntimeConfig, useSupabaseSession, useSupabaseUser } from '#imports'
import type { CookieOptions } from '#app'

Expand All @@ -24,6 +25,10 @@ export default defineNuxtPlugin({
) => cookies.forEach(({ name, value, options }) => setCookie(event, name, value, options)),
},
cookieOptions,
global: {
fetch: fetchWithRetry,
...clientOptions.global,
},
})

// Initialize user and session states
Expand Down
7 changes: 6 additions & 1 deletion src/runtime/server/services/serverSupabaseClient.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import type { SupabaseClient } from '@supabase/supabase-js'
import { createServerClient, parseCookieHeader, type CookieOptions } from '@supabase/ssr'
import { getHeader, setCookie, type H3Event } from 'h3'
import { fetchWithRetry } from '../../utils/fetch-retry'
import { useRuntimeConfig } from '#imports'
import type { Database } from '#build/types/supabase-database'

Expand All @@ -13,7 +14,7 @@ export const serverSupabaseClient = async <T = Database>(event: H3Event): Promis
url,
key,
cookieOptions,
clientOptions: { auth = {} },
clientOptions: { auth = {}, global = {} },
},
} = useRuntimeConfig().public

Expand All @@ -30,6 +31,10 @@ export const serverSupabaseClient = async <T = Database>(event: H3Event): Promis
) => cookies.forEach(({ name, value, options }) => setCookie(event, name, value, options)),
},
cookieOptions,
global: {
fetch: fetchWithRetry,
...global,
},
})
}

Expand Down
20 changes: 20 additions & 0 deletions src/runtime/utils/fetch-retry.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
export async function fetchWithRetry(req: RequestInfo | URL, init?: RequestInit): Promise<Response> {
const retries = 3
for (let attempt = 1; attempt <= retries; attempt++) {
try {
return await fetch(req, init)
}
catch (error) {
// Don't retry if it's an abort
if (init?.signal?.aborted) {
throw error
}
if (attempt === retries) {
console.error(`Error fetching request ${req}`, error, init)
throw error
}
console.warn(`Retrying fetch attempt ${attempt + 1} for request: ${req}`)
}
}
throw new Error('Unreachable code')
}