From 8b4c85521c010fb9f197c9e9dacd3779b7475d3e Mon Sep 17 00:00:00 2001 From: Runjuu Date: Fri, 9 Sep 2022 00:58:26 +0800 Subject: [PATCH] fix(ipfs-gateway): server side request issues - `Unidata` will use default gateway when running at server side. - `toGateway` adds an option `needRequestAtServerSide` to return a valid gateway for server side usage. --- .env.example | 2 +- deploy/prod/secrets.yaml | 2 +- src/lib/env.ts | 3 ++- src/lib/ipfs-gateway.ts | 4 +++- src/lib/ipfs-parser.ts | 37 ++++++++++++++++++++++++++++--------- src/lib/unidata.ts | 5 ++++- 6 files changed, 39 insertions(+), 14 deletions(-) diff --git a/.env.example b/.env.example index 974c9b9a..7a7cc350 100644 --- a/.env.example +++ b/.env.example @@ -9,6 +9,6 @@ NEXT_PUBLIC_DISCORD_LINK="https://discord.gg/9XscSqJEq4" NEXT_PUBLIC_GITHUB_LINK="https://github.com/Crossbell-Box/xlog" NEXT_PUBLIC_CSB_IO="https://crossbell.io" NEXT_PUBLIC_CSB_SCAN="https://scan.crossbell.io" -NEXT_PUBLIC_IPFS_GATEWAY="/_ipfs/" +NEXT_PUBLIC_IPFS_GATEWAY="https://ipfs.4everland.xyz/ipfs/" REDIS_URL= diff --git a/deploy/prod/secrets.yaml b/deploy/prod/secrets.yaml index 01d003e6..da8b93e9 100644 --- a/deploy/prod/secrets.yaml +++ b/deploy/prod/secrets.yaml @@ -7,7 +7,7 @@ stringData: NEXT_PUBLIC_GITHUB_LINK: "https://github.com/Crossbell-Box/xlog" NEXT_PUBLIC_CSB_IO: "https://crossbell.io" NEXT_PUBLIC_CSB_SCAN: "https://scan.crossbell.io" - NEXT_PUBLIC_IPFS_GATEWAY: "/_ipfs/" + NEXT_PUBLIC_IPFS_GATEWAY: "https://ipfs.4everland.xyz/ipfs/" REDIS_URL: ${REDIS_URL} kind: Secret metadata: diff --git a/src/lib/env.ts b/src/lib/env.ts index f57b6f8f..b83f0fd0 100644 --- a/src/lib/env.ts +++ b/src/lib/env.ts @@ -11,4 +11,5 @@ export const GITHUB_LINK = process.env.NEXT_PUBLIC_GITHUB_LINK export const CSB_IO = process.env.NEXT_PUBLIC_CSB_IO || "https://crossbell.io" export const CSB_SCAN = process.env.NEXT_PUBLIC_CSB_SCAN || "https://scan.crossbell.io" -export const IPFS_GATEWAY = process.env.NEXT_PUBLIC_IPFS_GATEWAY || "/_ipfs/" +export const IPFS_GATEWAY = + process.env.NEXT_PUBLIC_IPFS_GATEWAY || "https://ipfs.4everland.xyz/ipfs/" diff --git a/src/lib/ipfs-gateway.ts b/src/lib/ipfs-gateway.ts index 5dae4554..f322dfb1 100644 --- a/src/lib/ipfs-gateway.ts +++ b/src/lib/ipfs-gateway.ts @@ -1,7 +1,9 @@ import { IpfsGateway } from "@crossbell/ipfs-gateway" +export const IPFS_SW_GATEWAY_PREFIX = "/_ipfs/" + export const ipfsGateway = new IpfsGateway({ serviceWorker: { - gatewayPrefix: "/_ipfs/", + gatewayPrefix: IPFS_SW_GATEWAY_PREFIX, }, }) diff --git a/src/lib/ipfs-parser.ts b/src/lib/ipfs-parser.ts index e339d00f..a56433a5 100644 --- a/src/lib/ipfs-parser.ts +++ b/src/lib/ipfs-parser.ts @@ -1,14 +1,33 @@ -import { IPFS_GATEWAY } from "~/lib/env" +import { isIpfsUrl } from "@crossbell/ipfs-gateway" -export const toGateway = (url: string) => { - return url - ?.replace("ipfs://", IPFS_GATEWAY) - .replace("https://gateway.ipfs.io/ipfs/", IPFS_GATEWAY) - .replace("https://ipfs.io/ipfs/", IPFS_GATEWAY) - .replace("https://cf-ipfs.com/ipfs/", IPFS_GATEWAY) - .replace("https://ipfs.4everland.xyz/ipfs/", IPFS_GATEWAY) +import { IPFS_GATEWAY } from "~/lib/env" +import { ipfsGateway } from "./ipfs-gateway" + +const IPFS_PREFIX = "ipfs://" + +export type ToGatewayConfig = { + needRequestAtServerSide?: boolean +} + +export const toGateway = (url: string, config?: ToGatewayConfig) => { + const ipfsUrl = toIPFS(url) + + if (isIpfsUrl(ipfsUrl)) { + if (config?.needRequestAtServerSide && typeof window === "undefined") { + return ipfsGateway.getFallbackWeb2Url(ipfsUrl) + } else { + return ipfsGateway.getSwWeb2Url(ipfsUrl) + } + } else { + return url + } } export const toIPFS = (url: string) => { - return url?.replace(IPFS_GATEWAY, "ipfs://") + return url + ?.replace(IPFS_GATEWAY, IPFS_PREFIX) + .replace("https://gateway.ipfs.io/ipfs/", IPFS_PREFIX) + .replace("https://ipfs.io/ipfs/", IPFS_PREFIX) + .replace("https://cf-ipfs.com/ipfs/", IPFS_PREFIX) + .replace("https://ipfs.4everland.xyz/ipfs/", IPFS_PREFIX) } diff --git a/src/lib/unidata.ts b/src/lib/unidata.ts index 88c3f57e..52b5681f 100644 --- a/src/lib/unidata.ts +++ b/src/lib/unidata.ts @@ -1,8 +1,11 @@ import Unidata from "unidata.js" + import { IPFS_GATEWAY } from "../lib/env" +import { IPFS_SW_GATEWAY_PREFIX } from "../lib/ipfs-gateway" let unidata = new Unidata({ - ipfsGateway: IPFS_GATEWAY, + ipfsGateway: + typeof window === "undefined" ? IPFS_GATEWAY : IPFS_SW_GATEWAY_PREFIX, }) export default unidata