next-theme / hexo-theme-next

🎉 Elegant and powerful theme for Hexo.
https://theme-next.js.org
Other
2.34k stars 420 forks source link

Custom CDN Option #425

Closed ljcbaby closed 2 years ago

ljcbaby commented 2 years ago

PR Checklist

PR Type

What is the current behavior?

Issue resolved:

What is the new behavior?

Add options for custom CDN providers.

How to use?

In NexT _config.yml:

vendors:
  # The CDN provider of NexT internal scripts.
  # Available values: local | jsdelivr | unpkg | cdnjs | custom
  # Warning: If you are using the latest master branch of NexT, please set `internal: local`
  internal: local
  # The default CDN provider of third-party plugins.
  # Available values: local | jsdelivr | unpkg | cdnjs | custom
  # Dependencies for `plugins: local`: https://github.com/next-theme/plugins
  plugins: jsdelivr
  # Custom CDN if you want.
  # Example: 'https://cdn.jsdelivr.net/npm/${name}@${version}/${file}'
  custom_cdn_url: 'https://code.bdstatic.com/npm/${name}@${version}/${file}'
coveralls commented 2 years ago

Pull Request Test Coverage Report for Build 1611536877


Totals Coverage Status
Change from base Build 1524610652: 0.0%
Covered Lines: 138
Relevant Lines: 141

💛 - Coveralls
jimaek commented 2 years ago

Please dont use the undocumented jsDelivr endpoints. They exist only for internal use and can be removed and stop working at any moment. Only cdn.jsdelivr.net is production ready

ljcbaby commented 2 years ago

@jimaek Thanks for the reminder. I added these mainly to address the accessibility of jsDelivr in mainland China after losing the mainland China endpoints. According to my partial experiments, Fastly has better speed in mainland China, but Cloudflare has better access success rate.

ljcbaby commented 2 years ago

AS #424.We are also considering adding public CDNs in mainland China with unpkg or CDNJS as their upstream to deal with this impact.

ljcbaby commented 2 years ago

My personal opinion: this outbreak may be related to the abuse of jsDelivr by some users in mainland China through Github.

jimaek commented 2 years ago

The outage was a one time unique event we had no control over. Loss of ICP license. Now that we use regular CDN providers our regular failover systems will work. So jsDelivr is still better than unpkg or cdnjs who simply use Cloudflare with no failover or routing optimization.

ljcbaby commented 2 years ago

@jimaek I don't think we can be overly optimistic, the network environment in mainland China is complex and this incident will definitely affect the accessibility of jsDelivr.

mrgeneralgoo commented 2 years ago

@ljcbaby Nice PR, works for me, but not well.

https://www.chengxiaobai.com/trouble-maker/customize-cdn-provider-for-hexo-theme-next

@njzjz Couldn't agree more.

ljcbaby commented 2 years ago

@ljcbaby Nice PR, works for me, but not well.

https://www.chengxiaobai.com/trouble-maker/customize-cdn-provider-for-hexo-theme-next

@njzjz Couldn't agree more.

Thank you for your suggestion.

This pr is for webmasters who still want to try jsDelivr but want to get a better access experience in mainland China for debugging.

We discuss the introduction of a public CDN for mainland China in #424.

As well, I'm personally opposed to modifying the theme files directly, which may cause long term maintenance problems.

stevenjoezhang commented 2 years ago

How about adding a new option like:

custom_cdn_url: 'https://fastly.jsdelivr.net/npm/${name}@${version}/${file}'

And we parse it like this:

str.replace(/\$\{(.+?)\}/g, (match, p1) => obj[p1])
ljcbaby commented 2 years ago

@stevenjoezhang Good method, this also can give each websitemaster better freedom.

ljcbaby commented 2 years ago

But how to solve preconnect ?

stevenjoezhang commented 2 years ago

Maybe url.parse(config.custom_cdn_url).hostname?

ljcbaby commented 2 years ago

I seem to be unable to complete this one, I haven't learn JavaScript.