Skip to content

Commit 7f7bc9b

Browse files
ciandt-crodriguesScriptedAlchemydanpeen
authored
feat(retry-plugin): allow fallback function to receive failed URL (#3133)
Co-authored-by: Zack Jackson <[email protected]> Co-authored-by: Karina <[email protected]>
1 parent 1ba0c16 commit 7f7bc9b

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

.changeset/orange-goats-hope.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
'@module-federation/retry-plugin': minor
3+
---
4+
5+
Allow fallback function to receive the failed URL in order to build the fallback URL.

apps/website-new/docs/en/plugin/plugins/retry-plugin.mdx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,9 @@ type FetchWithRetryOptions = {
9696
options?: RequestInit;
9797
retryTimes?: number;
9898
retryDelay?: number;
99-
fallback?: (() => string) | ((url: string | URL | globalThis.Request) => string);
99+
fallback?:
100+
| (() => string)
101+
| ((url: string | URL | globalThis.Request) => string);
100102
}
101103

102104
type ScriptWithRetryOptions = {

packages/retry-plugin/__tests__/retry.spec.ts

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,24 @@ describe('fetchWithRetry', () => {
118118
expect(fetch).toHaveBeenLastCalledWith('https://fallback.com', {});
119119
});
120120

121+
it('should build fallback URL from remote after retries fail', async () => {
122+
mockErrorFetch();
123+
const retryTimes = 3;
124+
const responsePromise = fetchWithRetry({
125+
url: 'https://example.com',
126+
retryTimes,
127+
retryDelay: 0,
128+
fallback: (url) => `${url}/fallback`,
129+
});
130+
vi.advanceTimersByTime(2000 * retryTimes);
131+
132+
await expect(responsePromise).rejects.toThrow(
133+
'The request failed three times and has now been abandoned',
134+
);
135+
expect(fetch).toHaveBeenCalledTimes(5); //first fetch + retryTimes fetch
136+
expect(fetch).toHaveBeenLastCalledWith('https://example.com/fallback', {});
137+
});
138+
121139
it('should handle JSON parse error', async () => {
122140
const mockFetch = vi.fn().mockResolvedValueOnce({
123141
ok: true,

0 commit comments

Comments
 (0)