Skip to content

Commit

Permalink
fix(ipfs-gateway): server side request issues
Browse files Browse the repository at this point in the history
- `Unidata` will use default gateway when running at server side.
- `toGateway` adds an option `needRequestAtServerSide` to return a valid gateway for server side usage.
  • Loading branch information
runjuu committed Sep 8, 2022
1 parent 0b3bcd1 commit 8b4c855
Show file tree
Hide file tree
Showing 6 changed files with 38 additions and 13 deletions.
2 changes: 1 addition & 1 deletion .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -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=
2 changes: 1 addition & 1 deletion deploy/prod/secrets.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down
3 changes: 2 additions & 1 deletion src/lib/env.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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/"
4 changes: 3 additions & 1 deletion src/lib/ipfs-gateway.ts
Original file line number Diff line number Diff line change
@@ -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,
},
})
35 changes: 27 additions & 8 deletions src/lib/ipfs-parser.ts
Original file line number Diff line number Diff line change
@@ -1,14 1,33 @@
import { isIpfsUrl } from "@crossbell/ipfs-gateway"

import { IPFS_GATEWAY } from "~/lib/env"
import { ipfsGateway } from "./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)
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)
}
5 changes: 4 additions & 1 deletion src/lib/unidata.ts
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 8b4c855

Please sign in to comment.