Skip to content
Merged
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: 3 additions & 2 deletions components/nft-image/renderers/NFTImageRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { Col } from "react-bootstrap";
import styles from "../NFTImage.module.scss";
import NFTImageBalance from "../NFTImageBalance";
import type { BaseRendererProps } from "../types/renderer-props";
import { withArweaveFallback } from "../utils/arweave-fallback";

function getSrc(
nft: BaseRendererProps["nft"],
Expand Down Expand Up @@ -45,7 +46,7 @@ export default function NFTImageRenderer(props: Readonly<BaseRendererProps>) {
id={props.id ?? `image-${props.nft.id}`}
src={src}
alt={props.nft.name}
onError={({ currentTarget }) => {
onError={withArweaveFallback(({ currentTarget }) => {
if (currentTarget.src === props.nft.thumbnail) {
currentTarget.src = props.nft.scaled
? props.nft.scaled
Expand All @@ -55,7 +56,7 @@ export default function NFTImageRenderer(props: Readonly<BaseRendererProps>) {
} else if ("metadata" in props.nft) {
currentTarget.src = props.nft.metadata.image;
}
}}
})}
/>
{props.showBalance && (
<NFTImageBalance
Expand Down
5 changes: 3 additions & 2 deletions components/nft-image/renderers/NFTVideoRenderer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { Col } from "react-bootstrap";
import styles from "../NFTImage.module.scss";
import NFTImageBalance from "../NFTImageBalance";
import type { BaseRendererProps } from "../types/renderer-props";
import { withArweaveFallback } from "../utils/arweave-fallback";

export default function NFTVideoRenderer(props: Readonly<BaseRendererProps>) {
return (
Expand Down Expand Up @@ -32,7 +33,7 @@ export default function NFTVideoRenderer(props: Readonly<BaseRendererProps>) {
: props.nft.animation
}
className={props.imageStyle}
onError={({ currentTarget }) => {
onError={withArweaveFallback(({ currentTarget }) => {
if (
"metadata" in props.nft &&
currentTarget.src === props.nft.compressed_animation
Expand All @@ -41,7 +42,7 @@ export default function NFTVideoRenderer(props: Readonly<BaseRendererProps>) {
} else if ("metadata" in props.nft) {
currentTarget.src = props.nft.metadata.animation;
}
}}></video>
})}></video>
</Col>
);
}
46 changes: 46 additions & 0 deletions components/nft-image/utils/arweave-fallback.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import type React from "react";

function isArweaveUrl(url: string): boolean {
try {
const h = new URL(url).hostname.toLowerCase();
return h === "arweave.net" || h.endsWith(".arweave.net");
} catch {
return false;
}
}

function getArweaveFallbackUrl(url: string): string | null {
if (!isArweaveUrl(url)) return null;
try {
const u = new URL(url);
u.hostname = "ar-io.net";
u.host = "ar-io.net" + (u.port ? ":" + u.port : "");
return u.toString();
} catch {
return null;
}
}

type MediaErrorEvent =
| React.SyntheticEvent<HTMLImageElement, Event>
| React.SyntheticEvent<HTMLVideoElement, Event>;

export function withArweaveFallback(
onError?: (event: MediaErrorEvent) => void
): (event: MediaErrorEvent) => void {
return (event: MediaErrorEvent) => {
const target = event.currentTarget;
const src = target.src;
if (src && isArweaveUrl(src)) {
const fallback = getArweaveFallbackUrl(src);
if (fallback) {
if (target.dataset['arweaveOriginalSrc'] === undefined) {
target.dataset['arweaveOriginalSrc'] = src;
}
target.src = fallback;
return;
}
}
onError?.(event);
Comment thread
prxt6529 marked this conversation as resolved.
};
}
1 change: 1 addition & 0 deletions config/nextConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ export function sharedConfig(
{ protocol: "https", hostname: "6529.io" },
{ protocol: "https", hostname: "staging.6529.io" },
{ protocol: "https", hostname: "arweave.net" },
{ protocol: "https", hostname: "ar-io.net" },
{ protocol: "http", hostname: "localhost" },
{ protocol: "https", hostname: "media.generator.seize.io" },
{ protocol: "https", hostname: "d3lqz0a4bldqgf.cloudfront.net" },
Expand Down