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.
This commit is contained in:
Runjuu
2022-09-09 00:58:26 +08:00
parent 0b3bcd1aa1
commit 8b4c85521c
6 changed files with 39 additions and 14 deletions
+1 -1
View File
@@ -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=
+1 -1
View File
@@ -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:
+2 -1
View File
@@ -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/"
+3 -1
View File
@@ -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,
},
})
+28 -9
View File
@@ -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)
}
+4 -1
View File
@@ -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