-
Notifications
You must be signed in to change notification settings - Fork 129
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(client): add retries to fetch (#382)
- Loading branch information
1 parent
cbbfd86
commit 98ca959
Showing
4 changed files
with
36 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 GitHub Actions / nightly (ubuntu-latest, 20)
Check failure on line 6 in src/runtime/plugins/supabase.client.ts GitHub Actions / ci (ubuntu-latest, 20)
|
||
|
@@ -12,6 +13,10 @@ export default defineNuxtPlugin({ | |
...clientOptions, | ||
cookieOptions, | ||
isSingleton: true, | ||
global: { | ||
fetch: fetchWithRetry, | ||
...clientOptions.global, | ||
}, | ||
}) | ||
|
||
const currentSession = useSupabaseSession() | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
} |