-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
Showing
6 changed files
with
38 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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, | ||
}, | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |