Skip to content

Commit

Permalink
Use IPFS to fetch images not in cache (#109)
Browse files Browse the repository at this point in the history
  • Loading branch information
FrederikBolding authored Oct 10, 2023
1 parent cd04849 commit cb5b575
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 14 deletions.
46 changes: 35 additions & 11 deletions gatsby-node.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,20 +19,44 @@ exports.sourceNodes = async ({
(response) => response.json()
);

async function fetchIcon(name, file) {
const cid = file.url.slice(7);

// Try iconsDownload first as it is way faster.
try {
return await createRemoteFileNode({
url: `https://chainid.network/iconsDownload/${cid}`,
createNode,
createNodeId,
store,
cache,
reporter,
name,
ext: `.${file.format}`,
});
} catch {}

// Fallback to IPFS
try {
return await createRemoteFileNode({
url: `https://ipfs.io/ipfs/${cid}`,
createNode,
createNodeId,
store,
cache,
reporter,
name,
ext: `.${file.format}`,
});
} catch {}

return null;
}

const iconFiles = await icons.reduce(async (previousPromise, icon) => {
const iconName = icon.name;
const iconFile = icon.icons?.[0];
const cid = iconFile.url.slice(7);
const result = await createRemoteFileNode({
url: `https://chainid.network/iconsDownload/${cid}`,
createNode,
createNodeId,
store,
cache,
reporter,
name: iconName,
ext: `.${iconFile.format}`,
}).catch(() => null);
const result = await fetchIcon(iconName, iconFile);
const acc = await previousPromise;
if (result) {
acc[iconName] = result;
Expand Down
5 changes: 2 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,8 @@
"gatsby"
],
"scripts": {
"develop": "gatsby develop",
"start": "gatsby develop",
"build": "gatsby build",
"start": "GATSBY_INCOMPLETE_RETRY_LIMIT=1 GATSBY_STALL_RETRY_LIMIT=1 gatsby develop",
"build": "GATSBY_INCOMPLETE_RETRY_LIMIT=1 GATSBY_STALL_RETRY_LIMIT=1 gatsby build",
"serve": "gatsby serve",
"clean": "gatsby clean",
"typecheck": "tsc --noEmit"
Expand Down

0 comments on commit cb5b575

Please sign in to comment.