Skip to content

Commit 98ca959

Browse files
authored
feat(client): add retries to fetch (#382)
1 parent cbbfd86 commit 98ca959

File tree

4 files changed

+36
-1
lines changed

4 files changed

+36
-1
lines changed

src/runtime/plugins/supabase.client.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createBrowserClient } from '@supabase/ssr'
22
import type { Session } from '@supabase/supabase-js'
3+
import { fetchWithRetry } from '../utils/fetch-retry'
34
import { defineNuxtPlugin, useRuntimeConfig, useSupabaseSession, useSupabaseUser } from '#imports'
45

56
export default defineNuxtPlugin({
@@ -12,6 +13,10 @@ export default defineNuxtPlugin({
1213
...clientOptions,
1314
cookieOptions,
1415
isSingleton: true,
16+
global: {
17+
fetch: fetchWithRetry,
18+
...clientOptions.global,
19+
},
1520
})
1621

1722
const currentSession = useSupabaseSession()

src/runtime/plugins/supabase.server.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { createServerClient, parseCookieHeader } from '@supabase/ssr'
22
import { getHeader, setCookie } from 'h3'
3+
import { fetchWithRetry } from '../utils/fetch-retry'
34
import { defineNuxtPlugin, useRequestEvent, useRuntimeConfig, useSupabaseSession, useSupabaseUser } from '#imports'
45
import type { CookieOptions } from '#app'
56

@@ -24,6 +25,10 @@ export default defineNuxtPlugin({
2425
) => cookies.forEach(({ name, value, options }) => setCookie(event, name, value, options)),
2526
},
2627
cookieOptions,
28+
global: {
29+
fetch: fetchWithRetry,
30+
...clientOptions.global,
31+
},
2732
})
2833

2934
// Initialize user and session states

src/runtime/server/services/serverSupabaseClient.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import type { SupabaseClient } from '@supabase/supabase-js'
22
import { createServerClient, parseCookieHeader, type CookieOptions } from '@supabase/ssr'
33
import { getHeader, setCookie, type H3Event } from 'h3'
4+
import { fetchWithRetry } from '../../utils/fetch-retry'
45
import { useRuntimeConfig } from '#imports'
56
import type { Database } from '#build/types/supabase-database'
67

@@ -13,7 +14,7 @@ export const serverSupabaseClient = async <T = Database>(event: H3Event): Promis
1314
url,
1415
key,
1516
cookieOptions,
16-
clientOptions: { auth = {} },
17+
clientOptions: { auth = {}, global = {} },
1718
},
1819
} = useRuntimeConfig().public
1920

@@ -30,6 +31,10 @@ export const serverSupabaseClient = async <T = Database>(event: H3Event): Promis
3031
) => cookies.forEach(({ name, value, options }) => setCookie(event, name, value, options)),
3132
},
3233
cookieOptions,
34+
global: {
35+
fetch: fetchWithRetry,
36+
...global,
37+
},
3338
})
3439
}
3540

src/runtime/utils/fetch-retry.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
export async function fetchWithRetry(req: RequestInfo | URL, init?: RequestInit): Promise<Response> {
2+
const retries = 3
3+
for (let attempt = 1; attempt <= retries; attempt++) {
4+
try {
5+
return await fetch(req, init)
6+
}
7+
catch (error) {
8+
// Don't retry if it's an abort
9+
if (init?.signal?.aborted) {
10+
throw error
11+
}
12+
if (attempt === retries) {
13+
console.error(`Error fetching request ${req}`, error, init)
14+
throw error
15+
}
16+
console.warn(`Retrying fetch attempt ${attempt + 1} for request: ${req}`)
17+
}
18+
}
19+
throw new Error('Unreachable code')
20+
}

0 commit comments

Comments
 (0)