Skip to content

Commit

Permalink
feat(retry-plugin): allow fallback function to receive failed URL (#3133
Browse files Browse the repository at this point in the history
)

Co-authored-by: Zack Jackson <[email protected]>
Co-authored-by: Karina <[email protected]>
  • Loading branch information
3 people authored Dec 16, 2024
1 parent 1ba0c16 commit 7f7bc9b
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 1 deletion.
5 changes: 5 additions & 0 deletions .changeset/orange-goats-hope.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@module-federation/retry-plugin': minor
---

Allow fallback function to receive the failed URL in order to build the fallback URL.
4 changes: 3 additions & 1 deletion apps/website-new/docs/en/plugin/plugins/retry-plugin.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,9 @@ type FetchWithRetryOptions = {
options?: RequestInit;
retryTimes?: number;
retryDelay?: number;
fallback?: (() => string) | ((url: string | URL | globalThis.Request) => string);
fallback?:
| (() => string)
| ((url: string | URL | globalThis.Request) => string);
}

type ScriptWithRetryOptions = {
Expand Down
18 changes: 18 additions & 0 deletions packages/retry-plugin/__tests__/retry.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,24 @@ describe('fetchWithRetry', () => {
expect(fetch).toHaveBeenLastCalledWith('https://fallback.com', {});
});

it('should build fallback URL from remote after retries fail', async () => {
mockErrorFetch();
const retryTimes = 3;
const responsePromise = fetchWithRetry({
url: 'https://example.com',
retryTimes,
retryDelay: 0,
fallback: (url) => `${url}/fallback`,
});
vi.advanceTimersByTime(2000 * retryTimes);

await expect(responsePromise).rejects.toThrow(
'The request failed three times and has now been abandoned',
);
expect(fetch).toHaveBeenCalledTimes(5); //first fetch + retryTimes fetch
expect(fetch).toHaveBeenLastCalledWith('https://example.com/fallback', {});
});

it('should handle JSON parse error', async () => {
const mockFetch = vi.fn().mockResolvedValueOnce({
ok: true,
Expand Down

0 comments on commit 7f7bc9b

Please sign in to comment.