Skip to content

feat: support ipfs #46

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
Jan 3, 2020
Merged

feat: support ipfs #46

merged 8 commits into from
Jan 3, 2020

Conversation

hugomrdias
Copy link
Member

@hugomrdias hugomrdias commented Dec 17, 2019

This PR adds support for hosting in IPFS.

Steps:
npx vuepress build docs
all-relative ./docs/.vuepress/dist
ipfs add ./docs/.vuepress/dist
Enjoy.

Theres still a couple of assests in the public folder that all-relative doesn't handle but that should be easy to fix.

Working example https://ipfs.io/ipfs/Qmb3bTUHXuxcuU9VWB5yG99Usa826kw4ZagyZJ5DucSZo3/

@hugomrdias
Copy link
Member Author

thanks @olizilla for all-relative


__webpack_public_path__ = ipfsPathPrefix + '/'
if (typeof window !== 'undefined') {
window.__VUEPRESS_ROUTER_BASE__ = ipfsPathPrefix + '/'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice! this var is the secret ingredient, where did you find it?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

vuepress bootstrap code.
but __webpack_public_path__ = ipfsPathPrefix + '/' this one is even more important without it the router would even load

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I knew there was a __webpack_public_path__ lurking about but I missed this one! Great work detective 🕵️‍♂️

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yeah @satazor put me in the right direction with that one i just did the last mile

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the highest of fives to @satazor! It also makes me wonder if we could make a more generic IPFS plugin for webpack compiled apps using some of these framework config vars. we'd need to do detection for router logic but it doesn't seem impossible now... 🤔

Copy link

@satazor satazor Dec 18, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! I also thought about that in the past @cwaring and it’s doable. The route logic detection could also be pluggable, meaning we could have addons/plugins for typical frameworks such as Gatsby and Vue press.

I guess it would take some investment time but it would pay off later.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nice, if we are delaying the adoption of origin isolation for a while then this might be a worthy endeavour to the community. I see this question crop up all the time.

@cwaring
Copy link
Member

cwaring commented Dec 17, 2019

Yes, I'm so happy we didn't have to regex the application source in the end! Do you think we could wrap this up neatly into a plugin by using https://vuepress.vuejs.org/plugin/option-api.html#chainwebpack to handle the path injection script?

@hugomrdias
Copy link
Member Author

Yes, I'm so happy we didn't have to regex the application source in the end! Do you think we could wrap this up neatly into a plugin by using vuepress.vuejs.org/plugin/option-api.html#chainwebpack to handle the path injection script?

yes we should be able to build a plugin to do all this in one go, just need to figure out the last missing assets either but improving all-relative or in the plugin itself.

@olizilla
Copy link
Member

@hugomrdias I'm guessing it would be helpful if I actually published all-relative to npm!

@olizilla
Copy link
Member

It a thing https://www.npmjs.com/package/all-relative

@cwaring
Copy link
Member

cwaring commented Dec 18, 2019

thanks @olizilla, so excited to see this all come together!

@cwaring
Copy link
Member

cwaring commented Dec 20, 2019

I've fixed the relative css path issues upstream in all-relative and enabled the circleci build config. So when @olizilla gets a few min to cut a new release we should be good to merge this and update the DNS for full IPFS support! 🎉

@hugomrdias
Copy link
Member Author

hugomrdias commented Dec 20, 2019

I think some head loads were failing also like manifest etc

@cwaring
Copy link
Member

cwaring commented Dec 20, 2019

Yes I've noticed that too but they are not critical for launch and I don't think all-relative should touch them by default. I'd only like to enable PWA functionality from the root domain so I can pick that up in a future update :)

@jessicaschilling jessicaschilling added design-front-end dif/expert Extensive knowledge (implications, ramifications) required labels Dec 24, 2019
@cwaring cwaring mentioned this pull request Jan 3, 2020
@cwaring cwaring merged commit dd1f388 into master Jan 3, 2020
@cwaring cwaring deleted the feat/support-ipfs branch January 3, 2020 14:50
@samajammin
Copy link

Hey folks! Thanks for providing this as a reference!

I'm trying to add IPFS support on ethereum.org (ethereum/ethereum-org-website#751). I've copied your approach - it's up & running locally! This seems to have solved the issue with relative links. The only issue I see now is broken images from the /public dir. @hugomrdias - you mentioned that is your PR description. Did you find a solution to that?

Thanks again.

@hugomrdias
Copy link
Member Author

hugomrdias commented Feb 19, 2020

@samajammin ill take a look

@cwaring
Copy link
Member

cwaring commented Feb 19, 2020

Hey @samajammin, great to see you drop in here and thanks for the email. I'll reply on this thread to keep the conversation open for others.

I fixed all the docs asset path issues with the latest release of all-relative but if you have any config files with absolute paths (such as a favicon xml or app manifests) then these will need to be updated manually. In our case we decided to leave those considering the primary app will be loaded from the root domain and these were not critical to the functionality of the app on IPFS.

vuepress-plugin-ipfs isn't ready just yet but I intend to port the functionality provided in this PR over to it after completing another project.

Let me know how you get on and if we need to make any additional updates for this patch to be more accessible.

@samajammin
Copy link

samajammin commented Feb 19, 2020

Awesome, thanks for the guidance!

So I now have the site pinned here: https://gateway.pinata.cloud/ipfs/QmZxrnb4P3RHcHpF7mTRzQ5x2vF1b5vZtWR23Ef5MsAcV9/

Looking at my build output in /docs/.vuepress/dist, the images seem to be relative links as intended but I'm seeing some inconsistent results on the IPFS gateway.

e.g. sometimes I load this page & the images appear, with DOM inspection showing relative links:

Screen Shot 2020-02-19 at 8 33 24 AM

Sometimes I click to the page & the images are broken. The source shows they are not relative links:
Screen Shot 2020-02-19 at 8 33 04 AM

Do you see this as well? Any idea what may be causing the inconsistency here? It appears to be loading images correctly on page reloads but when navigating to this page from other routes, the images are broken.

@samajammin
Copy link

One more item I'm noticing - the mp4 file on our homepage is not referenced correctly:

Screen Shot 2020-02-19 at 8 51 34 AM

Looking at the build output HTML:

<video id="hero-video" alt="Ethereum.org - Light" width="380" height="380" src="/ethereum-hero-light.mp4" playsinline autoplay loop muted class="mx-auto inline-block" data-v-5d8b6e4d=""></video>

I believe the src should be src="./ethereum-hero-light.mp4" vs. src="/ethereum-hero-light.mp4". Updating this manually in the browser gets it working. I'm not sure why this particular video element / mp4 file is having issues, it looks like all-relative searches for all nodes w/ a src property, which would still apply to this?

@cwaring
Copy link
Member

cwaring commented Feb 24, 2020

Interesting, this isn't something we've seen on the docs project yet. It seems that VuePress is outputting absolute paths when the page/components re-render after routing. I haven't had chance to dig any deeper into your codebase but I wonder if it's something to do with how those individual assets are being included within the components?

A couple of things I'd try next:

  • Is it possible to make those asset inclusions baseurl aware?
  • What happens if you switch inline images to display using a CSS background instead?

Let me know if either of these routes work and I'll aim to dig in as soon as I can.

@cwaring
Copy link
Member

cwaring commented Feb 27, 2020

@samajammin curiosity got the better of me and I think I've found the culprit!

If you are injecting asset paths into the markup dynamically (via strings) you will have to ensure they are relative. The easiest way to do this is to hook into window.__VUEPRESS_ROUTER_BASE__ and if it exists prepend it to the root path.

A meta function like this should work (⚠️ not tested):

const withBasePath = path => window && window.__VUEPRESS_ROUTER_BASE___ ? window.__VUEPRESS_ROUTER_BASE__ + path : '/' + path || '/' + path;

withBasePath('ethereum-hero-dark.mp4')
// => "/ipfs/QmZxrnb4P3RHcHpF7mTRzQ5x2vF1b5vZtWR23Ef5MsAcV9/ethereum-hero-dark.mp4"

Let me know how it goes, I'll likely add a utility like this to the final plugin 🤙🏻

@cwaring
Copy link
Member

cwaring commented Feb 27, 2020

Or perhaps you could modify public-path.js script to inject a base url tag into the header once if it detects a window object, that should make all path urls work correctly even if they are injected at runtime. This method would also need to be tested to see how it interacts with the router links.

@samajammin
Copy link

Thanks for the ideas @cwaring! I appreciate you taking the time to look into this.

I tried your suggestion to create a withBasePath helper but unfortunately that did not solve the issue. I suspect this is because the site build happens in node, where window.__VUEPRESS_ROUTER_BASE___ is not available. As a result, those images still do not have the relative path needed.

When I have a chance (hopefully this week) I will keep experimenting but if you have any more ideas or if there's something you think I'm missing, please let me know.

@samajammin
Copy link

Here's the PR where I tried that update in case you'd like to experiment on your own:
ethereum/ethereum-org-website#751

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
dif/expert Extensive knowledge (implications, ramifications) required
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants