• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

Velenir/nextjs-ipfs-example: Example of Next.js app deployable to IPFS

原作者: [db:作者] 来自: 网络 收藏 邀请

开源软件名称:

Velenir/nextjs-ipfs-example

开源软件地址:

https://github.com/Velenir/nextjs-ipfs-example

开源编程语言:

JavaScript 100.0%

开源软件介绍:

Deploying Next.js site to IPFS

  1. Inject <base href="<dynamic_url>"> into <head> at runtime:
// ./pages/_document.js
import Document, { Html, Head, Main, NextScript } from 'next/document'

const scriptTxt = `
(function () {
  const { pathname } = window.location
  const ipfsMatch = /.*\\/Qm\\w{44}\\//.exec(pathname)
  const base = document.createElement('base')

  base.href = ipfsMatch ? ipfsMatch[0] : '/'
  document.head.append(base)
})();
`

class MyDocument extends Document {

  render() {
    return (
      <Html>
        <Head>
            <script dangerouslySetInnerHTML={{__html: scriptTxt}}/>
        </Head>
        <body>
          <Main />
          <NextScript />
        </body>
      </Html>
    )
  }
}

export default MyDocument
  1. All local resources must have relative links to make use of base.href
<img src="example.png"/>
// or
<img src="./example.png"/>
  1. Compiled JavaScript assets should also respect base.href:
// next.config.js
module.exports = {
  assetPrefix: './',
}
  1. Route.push|replace() would normally override location.pathname, so need to provide a different as parameter. One way to do it is with a custom Link component:
import {resolve} from 'url'

const BaseLink = ({href, as, ...rest}) => {

  const newAs = useMemo(() => {
    let baseURI_as = as || href
  
    // make absolute url relative
    // when displayed in url bar
    if (baseURI_as.startsWith('/')) {
      //  for static html compilation
      baseURI_as = '.' + href
      // <IPFSLink href="/about"> => <a class="jsx-2055897931" href="./about">About</a>
  
      // on the client
    //   document is unavailable when compiling on the server
      if (typeof document !== 'undefined') {
        baseURI_as = resolve(document.baseURI, baseURI_as)
        // => <a href="https://gateway.ipfs.io/ipfs/Qm<hash>/about">About</a>
      }
    }
    return baseURI_as
  }, [as, href])

  return <Link {...rest} href={href} as={newAs} />
}

This way a <BaseLink href="/about"> would lead to https://gateway.ipfs.io/ipfs/Qm<hash>/about when clicked.

As IPFS doesn't support automatic redirect to index for 404 routes, which is commonly employed when hosting SPA, one may want /about route to lead to https://gateway.ipfs.io/ipfs/Qm<hash>/about.html file if that file was statically compiled. In that case there will be need for further baseURI_as modification beyond simple baseURI_as += '.html' if there's need to preserve hash and search queries, for example.

Or better yet use exportTrailingSlash: true in next.config.js. Then pages/about.js will be compiled to out/about/index.html and /about route will be available at https://gateway.ipfs.io/ipfs/Qm<hash>/about/ which will survide page reloads without need for redirect to index for 404 routes:

module.exports = {
  assetPrefix: './',
  exportTrailingSlash: true,
}
  1. Build and export html files
yarn build && yarn export
  1. Add output directory to ipfs
ipfs add -r out



鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
davidar/ipfs-maps: OSM vector tiles on IPFS发布时间:2022-06-22
下一篇:
ipfs/js-ipfs-merkle-dag: [DEPRECATED]发布时间:2022-06-22
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap