Skip to content

Commit

Permalink
redirect with subdomain resolution for ipfs
Browse files Browse the repository at this point in the history
  • Loading branch information
jubalm committed Jan 27, 2024
1 parent ff55327 commit 43d5b00
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 0 deletions.
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,7 @@
"make-plural": "^7.0.0",
"ms": "^2.1.3",
"multicodec": "^3.0.1",
"multiformats": "^13.0.1",
"multihashes": "^4.0.2",
"nock": "^13.3.3",
"node-vibrant": "^3.2.1-alpha.1",
Expand Down
31 changes: 31 additions & 0 deletions src/components/IPFSSubpathRedirect.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { CID } from 'multiformats'
import { useEffect } from 'react'

export const IPFSSubpathRedirect = () => {
function extractHashFromBasePath() {
const htmlBase = document.querySelector('base')
if (!htmlBase) return
const cidRegex = /\/ipfs\/(Qm[1-9A-HJ-NP-Za-km-z]{44,}|b[A-Za-z2-7]{58,}|B[A-Z2-7]{58,}|z[1-9A-HJ-NP-Za-km-z]{48,}|F[0-9A-F]{50,})/

Check failure on line 8 in src/components/IPFSSubpathRedirect.tsx

View workflow job for this annotation

GitHub Actions / lint

Insert `⏎·····`
return htmlBase.href.match(cidRegex)?.at(1)
}

function generateIPFSRedirect() {

Check warning on line 12 in src/components/IPFSSubpathRedirect.tsx

View workflow job for this annotation

GitHub Actions / lint

The 'generateIPFSRedirect' function makes the dependencies of useEffect Hook (at line 28) change on every render. Move it inside the useEffect callback. Alternatively, wrap the definition of 'generateIPFSRedirect' in its own useCallback() Hook
const cidFromPath = extractHashFromBasePath()

if (!cidFromPath) {
console.log('No IPFS subpath detected')
return
}

const v1CidString = CID.parse(cidFromPath).toV1().toString()
return `${location.protocol}//${v1CidString}.ipfs.${location.host}`
}

useEffect(() => {
const redirectUrl = generateIPFSRedirect()
if (!redirectUrl) return
window.location.href = redirectUrl
}, [generateIPFSRedirect])

return null
}
2 changes: 2 additions & 0 deletions src/pages/App.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import ErrorBoundary from 'components/ErrorBoundary'
import Loader from 'components/Icons/LoadingSpinner'
import { IPFSSubpathRedirect } from 'components/IPFSSubpathRedirect'
import NavBar, { PageTabs } from 'components/NavBar'
import { lazy, Suspense, useEffect, useState } from 'react'
import { Navigate, Route, Routes, useLocation } from 'react-router-dom'
Expand Down Expand Up @@ -100,6 +101,7 @@ export default function App() {

return (
<ErrorBoundary>
<IPFSSubpathRedirect />
<DarkModeQueryParamReader />
<HeaderWrapper transparent={isHeaderTransparent} scrollY={scrollY}>
<NavBar blur={isHeaderTransparent} />
Expand Down

0 comments on commit 43d5b00

Please sign in to comment.