Skip to content

Commit

Permalink
feat(client): add retries to fetch (#382)
Browse files Browse the repository at this point in the history
  • Loading branch information
felixgabler authored Aug 30, 2024
1 parent cbbfd86 commit 98ca959
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 1 deletion.
5 changes: 5 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({

Check failure on line 6 in src/runtime/plugins/supabase.client.ts

View workflow job for this annotation

GitHub Actions / nightly (ubuntu-latest, 20)

The inferred type of 'default' cannot be named without a reference to '.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected][email protected]_ji_ln35ihgqguqz575dzkkhtdvjym/node_modules/nuxt/app'. This is likely not portable. A type annotation is necessary.

Check failure on line 6 in src/runtime/plugins/supabase.client.ts

View workflow job for this annotation

GitHub Actions / ci (ubuntu-latest, 20)

The inferred type of 'default' cannot be named without a reference to '.pnpm/[email protected]_@[email protected]_@[email protected]_@[email protected][email protected]_ji_ln35ihgqguqz575dzkkhtdvjym/node_modules/nuxt/app'. This is likely not portable. A type annotation is necessary.
Expand All @@ -12,6 +13,10 @@ export default defineNuxtPlugin({
...clientOptions,
cookieOptions,
isSingleton: true,
global: {
fetch: fetchWithRetry,
...clientOptions.global,
},
})

const currentSession = useSupabaseSession()
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')
}

0 comments on commit 98ca959

Please sign in to comment.