Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(retry-plugin): allow fallback function to receive failed URL #3133

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading