yusanshi / emgithub

Embed a file from GitHub repository just like GitHub Gist.
https://emgithub.com
MIT License
409 stars 77 forks source link

Fetch GitHub raw files from jsDelivr #15

Closed fantasticmao closed 2 years ago

fantasticmao commented 2 years ago

For some reasons, raw.githubusercontent.com is not available in Mainland China. However, I think we can use jsDelivr to get the GitHub raw files. jsDelivr works well in China as it is described in the document, and it can speed up obtaining files too.

I have tested this way and it is feasible, I can make a pull request if you also think it is a good idea.

By the way, I found a problem. If we get the embed.js through jsDelivr(i.e. use https://cdn.jsdelivr.net/gh/yusanshi/embed-like-gist@master/embed.js), then ${serviceProvider} will become cdn.jsdelivr.net, which is kind of strange. I think it can be written as emgithub.com directly :)

yusanshi commented 2 years ago

If I'm not wrong, I think you mean two places to use jsDelivr:

In summary, I think the first one is completely OK, you can make a PR (just one line of code, I think, hahaha). The second one is likely a breaking change and we can talk it more.

By the way, as for the second one, I think GitHub Pages is accessible in most networks of China, although slower. If https://emgithub.com/embed.js is accessible without any proxy in China, then only the first change is enough :eyes:

fantasticmao commented 2 years ago

Yes, the first point is the internal changes of embed.js, that is exactly what we need to do. But in my opinion, the fetchFromJsDelivr option may be necessary, because jsDelivr always needs time to purge cache when raw file is updated, even if jsDelivr purge cache API is explicitly called by users. I think users need to know the disadvantages of using jsDelivr CDN, maybe we could explain it in the documentation.

The second point I said is what users need to do when copying links from emgithub.com (e.g. paste the link here https://github.com/yusanshi/homepage/blob/f72910bc16f9a706639d836d83aa3cb27a2a7249/_posts/2019-12-17-emgithub.md#L39-L53 in your blog), it is the user’s own Choice, although they will get such a problem. I don't think it’s a good idea to make changes in 404.html too as you said, maybe we can talk about ​how to help users solve this proble.

yusanshi commented 2 years ago

I didn't realize the cache issue of jsDelivr and thanks for explaining this. So we can add a checkbox (text: Fetch from jsDelivr, maybe with a little question mark and when hovering on it a helping message is shown), or a dropdown with select tag (Fetch from githubusercontent, jsDelivr)? Anyway it's just a little bit of my opinion. You can choose any implementation you like and I'll appreciate it if you can make a PR for it.

As for the second, maybe adding some text in README? For example, if you are having connection issues with https://emgithub.com/embed.js, you can use a CDN for embed.js. For example, replace https://emgithub.com/embed.js with https://cdn.jsdelivr.net/gh/yusanshi/embed-like-gist@master/embed.js in the JavaScript tag ... However this will bring the serviceProvider issue and it'll be a little dirty to fix this (e.g. serviceProvider = serviceProvider.includes("cdn") ? "https://emgithub.com" : serviceProvider). I think this can be just put aside. https://emgithub.com/embed.js is basically OK without a proxy from my own experience.

fantasticmao commented 2 years ago

I have implemented the feature of fetching files from jsDelivr, I tested it on local machine and it works, please check the pull request when you have time.

As for the serviceProvider issue, I agree with you, and there is no change at the moment.

yusanshi commented 2 years ago

Looks good to me. Thanks and I'll merge #16