Skip to content

Commit 443d491

Browse files
authored
fix(retry-plugin): add autoParseResponse to auto-select JSON or text based on file extension (#4228)
1 parent 64840dd commit 443d491

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

.changeset/thirty-cooks-dream.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': patch
3+
---
4+
5+
fix(retry-plugin): add autoParseResponse to auto-select JSON or text based on file extension

packages/retry-plugin/src/fetch-retry.ts

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,18 @@ import {
99
import logger from './logger';
1010
import { getRetryUrl, combineUrlDomainWithPathQuery } from './utils';
1111

12+
function autoParseResponse(url: string, response: Response) {
13+
try {
14+
const parsed = new URL(url);
15+
if (parsed.pathname.endsWith('.js')) {
16+
return response.text();
17+
}
18+
return response.json();
19+
} catch (error) {
20+
return response.json();
21+
}
22+
}
23+
1224
async function fetchRetry(
1325
params: FetchRetryOptions,
1426
lastRequestUrl?: string,
@@ -63,7 +75,7 @@ async function fetchRetry(
6375
`${PLUGIN_IDENTIFIER}: Request failed: ${response.status} ${response.statusText || ''} | url: ${requestUrl}`,
6476
);
6577
}
66-
await responseClone.json().catch((error) => {
78+
await autoParseResponse(requestUrl, responseClone).catch((error) => {
6779
throw new Error(
6880
`${PLUGIN_IDENTIFIER}: JSON parse failed: ${(error as Error)?.message || String(error)} | url: ${requestUrl}`,
6981
);

0 commit comments

Comments
 (0)