mirror of
https://github.com/cinnyapp/cinny.git
synced 2025-11-05 06:50:28 +03:00
swap fetch to use retry logic (shouldn't occur very often, but when it does we don't want to immediately fail)
This commit is contained in:
parent
60af716dbc
commit
77bbb94bfa
1 changed files with 40 additions and 2 deletions
42
src/sw.ts
42
src/sw.ts
|
|
@ -19,6 +19,45 @@ function sendAndWaitForReply(client: WindowClient, type: string, payload: object
|
||||||
return promise;
|
return promise;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
async function fetchWithRetry(
|
||||||
|
url: string,
|
||||||
|
token: string,
|
||||||
|
retries = 3,
|
||||||
|
delay = 250
|
||||||
|
): Promise<Response> {
|
||||||
|
let lastError: Error | undefined;
|
||||||
|
|
||||||
|
/* eslint-disable no-await-in-loop */
|
||||||
|
for (let attempt = 1; attempt <= retries; attempt += 1) {
|
||||||
|
try {
|
||||||
|
const response = await fetch(url, {
|
||||||
|
headers: {
|
||||||
|
Authorization: `Bearer ${token}`,
|
||||||
|
},
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!response.ok) {
|
||||||
|
throw new Error(`HTTP error! Status: ${response.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
return response;
|
||||||
|
} catch (error) {
|
||||||
|
lastError = error instanceof Error ? error : new Error(String(error));
|
||||||
|
|
||||||
|
if (attempt < retries) {
|
||||||
|
console.warn(
|
||||||
|
`Fetch attempt ${attempt} failed: ${lastError.message}. Retrying in ${delay}ms...`
|
||||||
|
);
|
||||||
|
await new Promise((res) => {
|
||||||
|
setTimeout(res, delay);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
/* eslint-enable no-await-in-loop */
|
||||||
|
throw new Error(`Fetch failed after ${retries} retries. Last error: ${lastError?.message}`);
|
||||||
|
}
|
||||||
|
|
||||||
function fetchConfig(token?: string): RequestInit | undefined {
|
function fetchConfig(token?: string): RequestInit | undefined {
|
||||||
if (!token) return undefined;
|
if (!token) return undefined;
|
||||||
|
|
||||||
|
|
@ -81,8 +120,7 @@ self.addEventListener('fetch', (event: FetchEvent) => {
|
||||||
if (!client) throw new Error('Client not found');
|
if (!client) throw new Error('Client not found');
|
||||||
const token = await sendAndWaitForReply(client, 'token', {});
|
const token = await sendAndWaitForReply(client, 'token', {});
|
||||||
if (!token) throw new Error('Failed to retrieve token');
|
if (!token) throw new Error('Failed to retrieve token');
|
||||||
const response = await fetch(url, fetchConfig(token));
|
const response = await fetchWithRetry(url, token);
|
||||||
if (!response.ok) throw new Error(`Fetch failed: ${response.statusText}`);
|
|
||||||
return response;
|
return response;
|
||||||
})()
|
})()
|
||||||
);
|
);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue