From e17ec5aa11843696e3c5fa77eb46c9fc09c8df05 Mon Sep 17 00:00:00 2001 From: DIYgod Date: Fri, 17 Nov 2023 18:05:00 +0800 Subject: [PATCH] feat: use filebase buid-in image optimization --- src/components/ui/Image.tsx | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/src/components/ui/Image.tsx b/src/components/ui/Image.tsx index 7a210df5..77a82b24 100644 --- a/src/components/ui/Image.tsx +++ b/src/components/ui/Image.tsx @@ -5,6 +5,7 @@ import React, { useEffect } from "react" import { useGetState } from "~/hooks/useGetState" import { useIsMobileLayout } from "~/hooks/useMobileLayout" +import { IPFS_GATEWAY } from "~/lib/env" import { toGateway, toIPFS } from "~/lib/ipfs-parser" export type TImageProps = { @@ -151,6 +152,25 @@ export const Image = ({ } }} ref={imageRefInternal} + loader={ + src.startsWith("ipfs://") && + IPFS_GATEWAY === "https://ipfs.crossbell.io/ipfs/" + ? ({ src, width, quality }) => { + // https://docs.filebase.com/ipfs/about-ipfs/ipfs-gateways#filebase-ipfs-image-optimization + try { + const urlObj = new URL(src) + urlObj.searchParams.set("img-quality", (quality || 75) + "") + urlObj.searchParams.set("img-format", "auto") + if (width) { + urlObj.searchParams.set("img-width", width + "") + } + return urlObj.toString() + } catch (error) { + return src + } + } + : undefined + } />