Skip to content

Commit 38400a5

Browse files
committed
Timeout stalled IPFS metadata fetches
1 parent a7b8ae1 commit 38400a5

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

src/data/ethereumProvider.jsx

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import ABI from "./ABI.json";
66
import { environment } from "./environments";
77

88
import notifyWithToast, { MESSAGE_TYPE, notifyProviderConnectionStatus } from "../utils/notifyWithTost";
9-
import { ipfsGateway } from "../utils/addToIPFS";
9+
import fetchIPFSJson from "../utils/fetchIPFSJson";
1010

1111
export const networkMap = {
1212
"0x1": {
@@ -129,10 +129,8 @@ const EthereumProvider = ({ children, chainId: chainIdFromUrl }) => {
129129
const fetchMetaEvidenceContents = async (chainId) => {
130130
const rawMetaEvidenceList = (await getAllMetaEvidences(chainId))?.map((item) => item.uri);
131131
if (!rawMetaEvidenceList) return;
132-
const result = await Promise.allSettled(
133-
rawMetaEvidenceList?.map((metaEvidenceURI) => fetch(ipfsGateway + metaEvidenceURI).then((r) => r.json()))
134-
);
135-
setMetaEvidenceContents(result.map((item) => item.value));
132+
const result = await Promise.allSettled(rawMetaEvidenceList.map((metaEvidenceURI) => fetchIPFSJson(metaEvidenceURI)));
133+
setMetaEvidenceContents(result.map((item) => (item.status === "fulfilled" ? item.value : {})));
136134
};
137135

138136
const sendTransaction = async (unsignedTx) => {

src/index.html

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
<meta name="description" content="Accurate and relevant news.">
77
<title>The Truth Post</title>
88
<link href="stylesheets/styles.scss" rel="stylesheet"/>
9-
<script src="index.jsx" type="module"></script>
109
</head>
1110
<body>
1211
<div id="app"></div>

src/utils/fetchIPFSJson.js

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,24 @@
11
import { ipfsGateway } from "/src/utils/addToIPFS";
22

3+
const IPFS_FETCH_TIMEOUT_MS = 3000;
4+
35
export default async function fetchIPFSJson(uri) {
46
if (!uri) return {};
57

8+
const controller = new AbortController();
9+
const timeoutId = setTimeout(() => controller.abort(), IPFS_FETCH_TIMEOUT_MS);
10+
611
try {
7-
const response = await fetch(ipfsGateway + uri);
12+
const response = await fetch(ipfsGateway + uri, {
13+
signal: controller.signal,
14+
});
815
if (!response.ok) throw new Error("Network response was not OK");
916

1017
return await response.json();
1118
} catch (error) {
12-
console.error(error);
19+
console.error(`Failed to fetch IPFS JSON for ${uri}`, error);
1320
return {};
21+
} finally {
22+
clearTimeout(timeoutId);
1423
}
1524
}

0 commit comments

Comments
 (0)