06_Hexo-elemecdn 加速 Butterfly 第三方资源

:点击此处或下方 以展开或折叠目录

2023/01/09 更新

今天发现 Butterfly 文档 有介绍自定义 CDN 的配置项,只需简单配置即可完成需求,所以之前写的东西 属于是 画蛇添足,参考价值不高

image-20230110005050004

官方样例中的 cdn.staticfile.org CDN 加速由七牛云提供,技术社区掘金支持,可以直接填入使用

个人示例如下(主题配置文件)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# CDN
# Don't modify the following settings unless you know how they work
# 非必要請不要修改
CDN:
# The CDN provider of internal scripts (主題內部 js 的 cdn 配置)
# option: local/jsdelivr/unpkg/cdnjs/custom
# Dev version can only choose. ( dev版的主題只能設置為 local )
internal_provider: local

# The CDN provider of third party scripts (第三方 js 的 cdn 配置)
# option: local/jsdelivr/unpkg/cdnjs/custom
# when set it to local, you need to install hexo-butterfly-extjs
# third_party_provider: jsdelivr
third_party_provider: custom

# Add version number to CDN, true or false
version: false

# Custom format
# For example: https://cdn.staticfile.org/${cdnjs_name}/${version}/${min_cdnjs_file}
custom_format: https://cdn.staticfile.org/${cdnjs_name}/${version}/${min_cdnjs_file}

想用 elemecdn 的话,custom_format 配置项该怎么写 我也不清楚。尝试写 https://npm.elemecdn.com/${name}${verType}/${file}https://npm.elemecdn.com/${cdnjs_name}/${version}/${min_cdnjs_file} 都失败了

想用 elemecdn 的话,Butterfly 文档 给出了格式:

以下步骤已过时

前言

博客异常加载,f12发现 https://unpkg.com/swiper/swiper-bundle.min.js (主因)和 https://unpkg.com/@fancyapps/ui/dist/fancybox.umd.js 报 520(520,当源站服务器向 CDN 返回空白、未知或意外响应时,会发生 520 错误),日常都返的 200

基于此有了换 cdn 的想法。butterfly 官方教程 介绍了 local/jsdelivr/unpkg/cdnjs/custom 这些选择,都不是想要的,看到别人用的 Element cdn,学习一下

步骤

参考文章:butterfly官方教程#CDN安知鱼 - 可用cdn整理张洪 Heo - Butterfly CDN链接更改指南

修改源码:

  1. 修改 themes/butterfly/scripts/events/cdn.js,新增 elemecdn 字段

    image-20221028222132106
    1
    elemecdn: `https://npm.elemecdn.com/${name}${verType}/${file}`,
  2. 修改 _config.butterfly.yml,CDN.third_party_provider

    image-20221029143047143

    f12 看了下 网络,替换后没异常

    image-20221028223148954
  3. 根据 张洪Heo:Butterfly CDN链接更改指南 按需替换 cdn链接(需注意变量名,比如butterfly v4.4.0 fontawesome 写成 fontawesomeV6 似乎才会生效 Butterfly v4.8.0 变量名已改为 fontawesome)

    image-20221028223928594
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    CDN:
    option:
    # 张洪Heo https://blog.zhheo.com/p/790087d9.html
    jquery: https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/jquery/3.6.0/jquery.min.js
    pjax: https://npm.elemecdn.com/pjax@latest/pjax.min.js
    sharejs: https://lib.baomitu.com/social-share.js/1.0.16/js/social-share.min.js
    sharejs_css: https://lib.baomitu.com/social-share.js/1.0.16/css/share.min.css
    gitalk: https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/gitalk/1.7.2/gitalk.min.js
    gitalk_css: https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/gitalk/1.7.2/gitalk.min.css
    valine: https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/valine/1.4.16/Valine.min.js
    disqusjs: https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/disqusjs/1.3.0/disqus.js
    disqusjs_css: https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/disqusjs/1.3.0/disqusjs.css
    waline: https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/waline/1.5.4/Waline.min.js
    lazyload: https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/vanilla-lazyload/17.3.1/lazyload.iife.min.js
    instantpage: https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/instant.page/5.1.0/instantpage.min.js
    typed: https://lf9-cdn-tos.bytecdntp.com/cdn/expire-1-M/typed.js/2.0.12/typed.min.js
    # fancybox_css: https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/fancybox/3.5.7/jquery.fancybox.min.css
    # fancybox: https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/fancybox/3.5.7/jquery.fancybox.min.js
    medium_zoom: https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/medium-zoom/1.0.6/medium-zoom.min.js
    snackbar_css: https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/node-snackbar/0.1.16/snackbar.min.css
    snackbar: https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/node-snackbar/0.1.16/snackbar.min.js
    fontawesome: https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/font-awesome/6.0.0/css/all.min.css
    # fontawesomeV6: https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/font-awesome/6.0.0/css/all.min.css
    # translate: https://npm.elemecdn.com/js-heo@1.0.6/translate/tw_cn.js # 与主题原文件不符
    aplayer_css: https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/aplayer/1.10.1/APlayer.min.css
    aplayer_js: https://lf6-cdn-tos.bytecdntp.com/cdn/expire-1-M/aplayer/1.10.1/APlayer.min.js
    meting_js: https://npm.elemecdn.com/js-heo@1.0.12/metingjs/Meting.min.js
    prismjs_js: https://npm.elemecdn.com/prismjs@1.1.0/prism.js
    prismjs_lineNumber_js: https://npm.elemecdn.com/prismjs/plugins/line-numbers/prism-line-numbers.min.js
    prismjs_autoloader: https://npm.elemecdn.com/prismjs/plugins/autoloader/prism-autoloader.min.js
    justifiedGallery_js: https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/justifiedGallery/3.8.1/js/jquery.justifiedGallery.min.js
    justifiedGallery_css: https://lf3-cdn-tos.bytecdntp.com/cdn/expire-1-M/justifiedGallery/3.8.1/css/justifiedGallery.min.css
    #twikoo: https://npm.elemecdn.com/twikoo@1.6.8/dist/twikoo.all.min.js