Skip to content

Commit 6f907df

Browse files
committed
change ipfs
1 parent c69e8cf commit 6f907df

File tree

6 files changed

+50
-14
lines changed

6 files changed

+50
-14
lines changed

components/FallBackImg.tsx

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
import { DetailedHTMLProps, ImgHTMLAttributes } from "react";
2+
3+
const FallBackImg = (props: DetailedHTMLProps<ImgHTMLAttributes<HTMLImageElement>, HTMLImageElement> & { src: string; fallBackSrc: string }) => {
4+
const {src, fallBackSrc, ...otherProps} = props
5+
return (
6+
<img
7+
src={src}
8+
onError={({ currentTarget }) => {
9+
currentTarget.onerror = null; // prevents looping
10+
currentTarget.src = fallBackSrc;
11+
}}
12+
{...otherProps}
13+
/>
14+
);
15+
};
16+
17+
export default FallBackImg;

components/NFT.tsx

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import React from "react";
33
import useCachedNFT from "../hooks/useCachedNFT";
44
import useConsentContext from "../hooks/useConsentContext";
55
import { NFT_DISALLOW_LIST } from "../utils/config";
6+
import FallBackImg from "./FallBackImg";
67

78
interface NFTData {
89
id: string;
@@ -19,7 +20,7 @@ const NFT: React.FC<{ nft: NFTData }> = ({ nft }) => {
1920
const [isLoaded, setIsLoaded] = React.useState(false);
2021
const { hasConsent, askUserForConsent } = useConsentContext();
2122

22-
const { image, name } = metadata;
23+
const { image, fallBackImage, name } = metadata;
2324
React.useEffect(() => {
2425
if (image && image === "/error") {
2526
setIsLoaded(true);
@@ -46,8 +47,9 @@ const NFT: React.FC<{ nft: NFTData }> = ({ nft }) => {
4647
className="top-0 right-0 h-full w-full object-contain object-center bg-white"
4748
/>
4849
) : (
49-
<img
50+
<FallBackImg
5051
src={image as string}
52+
fallBackSrc={fallBackImage as string}
5153
alt={name as string}
5254
className={`z-10 object-cover object-center w-full ${
5355
NFT_DISALLOW_LIST.includes(nft.id) || !hasConsent

components/nftDetail/NFTAnimation.tsx

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
11
import Script from 'next/script';
22
import React from 'react';
3+
import FallBackImg from '../FallBackImg';
34

4-
const NFTAnimation: React.FC<{ animationURL: string; animationType: string; image: string }> = ({
5+
const NFTAnimation: React.FC<{ animationURL: string; animationType: string; image: string, fallBackImage: string }> = ({
56
animationURL,
67
animationType,
78
image,
9+
fallBackImage
810
}) => {
911
if (animationType?.startsWith('video')) {
1012
return (
@@ -31,7 +33,7 @@ const NFTAnimation: React.FC<{ animationURL: string; animationType: string; imag
3133
} else if (animationType?.startsWith('audio')) {
3234
return (
3335
<div className="z-10 h-full w-[400px] flex flex-col">
34-
<img src={image as string} className="object-contain object-center" />
36+
<FallBackImg src={image as string} fallBackSrc={fallBackImage} className="object-contain object-center" />
3537
<audio
3638
src={animationURL as string}
3739
controls
@@ -65,7 +67,7 @@ const NFTAnimation: React.FC<{ animationURL: string; animationType: string; imag
6567
</>
6668
);
6769
} else {
68-
return <img src={image as string} />;
70+
return <FallBackImg fallBackSrc={fallBackImage} src={image as string} />;
6971
}
7072
};
7173

hooks/useCachedNFT.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,13 @@ import { ethers } from 'ethers';
33

44
import { INFURA_ENDPOINT } from '../utils/config';
55
import LRUCache from '../utils/cache';
6+
import { FALLBACK_IPFS_URL, IPFS_URL } from '../utils/nft';
67

78
interface NFTMetadata {
89
[index: string]: unknown;
910
}
1011

11-
const IPFS_URL = 'https://loopring.mypinata.cloud/ipfs/';
12+
// const IPFS_URL = 'https://ipfs.loopring.io/ipfs/';
1213

1314
// Two caches need to maintained
1415
// NFT URI cache {key-> token_address:nft_id}
@@ -89,6 +90,7 @@ const getNFTURI = async (nft) => {
8990
};
9091

9192
const getNFTMetadata = async (uri, nft, isErrorFallback = false) => {
93+
9294
const cacheKey = nft.id;
9395
let cacheResult = metadataCache.get(cacheKey);
9496
if (cacheResult) {
@@ -102,10 +104,17 @@ const getNFTMetadata = async (uri, nft, isErrorFallback = false) => {
102104
};
103105
}
104106
try {
105-
const metadata = await fetch(uri.replace('ipfs://', IPFS_URL)).then((res) => res.json());
107+
108+
const metadata = await fetch(uri.replace('ipfs://', IPFS_URL))
109+
.catch(() => {
110+
111+
return fetch(uri.replace('ipfs://', FALLBACK_IPFS_URL))
112+
})
113+
.then((res) => res.json());
106114
metadataCache.set(cacheKey, metadata);
107115
return metadata;
108116
} catch (error) {
117+
109118
if (!isErrorFallback) {
110119
return getNFTMetadata(`${uri}/metadata.json`, nft, true);
111120
}
@@ -132,7 +141,9 @@ const useCachedNFT = (nft) => {
132141
...metadata,
133142
uri: uri?.replace('ipfs://', IPFS_URL),
134143
image: metadata?.image?.replace('ipfs://', IPFS_URL),
144+
fallBackImage: metadata?.image?.replace('ipfs://', FALLBACK_IPFS_URL),
135145
animation_url: metadata?.animation_url?.replace('ipfs://', IPFS_URL),
146+
fallBackAnimation_url: metadata?.animation_url?.replace('ipfs://', FALLBACK_IPFS_URL),
136147
});
137148
})();
138149
}

pages/nft/[id].tsx

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import { INFURA_ENDPOINT, NFT_DISALLOW_LIST } from '../../utils/config';
1010
import useConsentContext from '../../hooks/useConsentContext';
1111
import { useNonFungibleTokenQuery } from '../../generated/loopringExplorer';
1212
import NFTAnimation from '../../components/nftDetail/NFTAnimation';
13+
import FallBackImg from '../../components/FallBackImg';
1314

1415
const provider = new ethers.providers.JsonRpcProvider(INFURA_ENDPOINT);
1516

@@ -42,7 +43,7 @@ const NFTDetail: React.FC<{}> = () => {
4243
const [hideConsentButton, setHideConsentButton] = React.useState<boolean>(false);
4344
const [animationType, setAnimationType] = React.useState<string>();
4445
const metadata = useCachedNFT(nft);
45-
const { image, name, animation_url } = metadata;
46+
const { image, name, animation_url, fallBackImage } = metadata;
4647

4748
React.useEffect(() => {
4849
if (nft) {
@@ -54,10 +55,10 @@ const NFTDetail: React.FC<{}> = () => {
5455
}, [nft]);
5556

5657
React.useEffect(() => {
57-
if (image && image === '/error') {
58+
if (image && image === '/error' && fallBackImage && fallBackImage === '/error') {
5859
setIsLoaded(true);
5960
}
60-
}, [image]);
61+
}, [image, fallBackImage]);
6162

6263
React.useEffect(() => {
6364
if (animation_url) {
@@ -94,21 +95,23 @@ const NFTDetail: React.FC<{}> = () => {
9495
animationURL={animation_url as string}
9596
animationType={animationType}
9697
image={image as string}
98+
fallBackImage={fallBackImage as string}
9799
/>
98100
) : animation_url === '/error' ? (
99101
<img
100102
src="/nft-error.svg"
101103
className="top-0 right-0 h-full w-full object-contain object-center bg-white rounded-xl"
102104
/>
103-
) : image ? (
104-
image === '/error' ? (
105+
) : (image || fallBackImage) ? (
106+
(image === '/error' && fallBackImage === '/error') ? (
105107
<img
106108
src="/nft-error.svg"
107109
className="top-0 right-0 h-full w-full object-contain object-center bg-white rounded-xl"
108110
/>
109111
) : (
110-
<img
112+
<FallBackImg
111113
src={image as string}
114+
fallBackSrc={fallBackImage as string}
112115
alt={name as string}
113116
className={`z-10 object-contain object-center m-auto h-full rounded-xl ${
114117
NFT_DISALLOW_LIST.includes(nft.id) || !hasConsent ? 'filter blur-xl' : ''

utils/nft.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@ export interface NFTInfo {
88
nftType: number
99
}
1010

11-
export const IPFS_URL = 'https://loopring.mypinata.cloud/ipfs/';
11+
export const IPFS_URL = 'https://ipfs.loopring1.io/ipfs/';
12+
export const FALLBACK_IPFS_URL = 'https://ipfs.io/ipfs/';
1213

1314
// Two caches need to maintained
1415
// NFT URI cache {key-> token_address:nft_id}

0 commit comments

Comments
 (0)