详细拆解:我的Hexo博客优化清单

承接上一篇《博客诞生记》,今天来聊聊我在搭建Hexo博客后做的那些优化工作。这些优化不仅提升了博客的性能和用户体验,也让我的写作和部署流程更加顺畅。

一、性能优化:让加载飞起来

1. 图片懒加载

图片是页面加载的主要瓶颈之一,我采用了浏览器原生的懒加载功能:

1
2
3
4
5
# 主题配置文件 themes/butterfly/_config.yml
lazyload:
enable: true
native: true
field: site

通过设置native: true,直接使用浏览器的loading="lazy"属性,无需额外引入第三方库,性能更好。

2. 资源压缩与合并

启用了HTML、CSS、JS的压缩功能,减小文件体积:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# Hexo配置文件 _config.yml
html_minifier_terser:
enable: true
collapseWhitespace: true
minifyCSS: true
minifyJS: true

clean_css:
enable: true
level: 2

uglify:
mangle: true
compress:
drop_console: true
drop_debugger: true

3. CDN加速

将静态资源托管到CDN,加速全球访问:

1
2
3
4
# 主题配置文件 themes/butterfly/_config.yml
CDN:
internal_provider: jsdelivr
third_party_provider: jsdelivr

4. 图像优化

启用imagemin插件自动压缩图片:

1
2
3
4
5
6
# Hexo配置文件 _config.yml
asset_pipeline:
enable: true
imagemin:
enable: true
quality: 80

二、功能优化:让博客更实用

1. 永久链接配置

使用hexo-abbrlink插件生成固定链接,避免文章链接因文件名变更而失效:

1
2
3
4
# Hexo配置文件 _config.yml
abbrlink:
alg: crc32
rep: hex

2. 本地搜索功能

集成hexo-generator-searchdb插件,实现本地文章搜索:

1
2
3
4
5
# Hexo配置文件 _config.yml
search:
path: search.xml
field: post
content: true

3. SEO优化

  • 生成sitemap.xml和atom.xml
  • 配置robots.txt
  • 添加Open Graph和Structured Data
1
2
3
4
5
# 主题配置文件 themes/butterfly/_config.yml
Open_Graph_meta:
enable: true
structured_data:
enable: true

4. 字数统计与阅读时间

使用hexo-wordcount插件在文章页显示字数和预计阅读时间:

1
2
3
4
5
# 主题配置文件 themes/butterfly/_config.yml
wordcount:
enable: true
post_wordcount: true
min2read: true

三、核心插件清单

插件名称功能描述
hexo-abbrlink生成固定链接
hexo-generator-searchdb本地搜索功能
hexo-wordcount字数统计与阅读时间
hexo-generator-sitemap生成sitemap.xml
hexo-clean-cssCSS压缩
hexo-html-minifier-terserHTML压缩
hexo-uglifyJavaScript压缩
hexo-asset-pipeline资源版本控制与图像优化
hexo-offlinePWA支持

四、写作流程:高效创作与管理

1. VSCode + Git 工作流

  • 使用VSCode作为主编辑器,安装Markdown插件增强写作体验
  • 通过Git管理博客源码和文章,实现版本控制
  • 分支管理:main分支用于生产,feature分支用于测试新功能

2. 文章管理

  • 使用Hexo命令创建新文章:hexo new "文章标题"
  • 采用Front-matter管理文章元数据:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    ---
    title: 文章标题
    date: 2026-01-04 14:30:00
    categories: 技术
    tags:
    - Hexo
    - 博客
    abbrlink: 123456
    ---

3. 图片处理

  • 将文章图片放在对应文章的asset目录下,便于管理
  • 使用在线工具压缩图片后再上传,减小文件体积
  • 统一图片命名规范:文章标题-序号.jpg

五、部署心得:自动化与细节

1. 部署流程优化

  • 简化npm scripts,移除不必要的日志文件:
    1
    2
    3
    4
    5
    6
    "scripts": {
    "build": "hexo generate",
    "clean": "hexo clean",
    "deploy": "hexo deploy",
    "server": "hexo server"
    }
  • 部署命令:hexo clean && hexo generate && hexo deploy

2. GitHub Pages 部署

  • 配置deploy插件自动部署到GitHub Pages:
    1
    2
    3
    4
    5
      # Hexo配置文件 _config.yml
    deploy:
    type: git
    repo: https://github.com/Jay-R-J/Jay-R-J.github.io.git
    branch: main
  • 自定义域名:在GitHub仓库设置中绑定自定义域名,同时在source目录下添加CNAME文件

3. 安全配置

  • 配置Content Security Policy (CSP),防止XSS攻击:
    1
    2
    3
    4
    5
    6
    7
    8
    # 主题配置文件 themes/butterfly/_config.yml
    csp:
    enable: true
    policy:
    - "default-src 'self'"
    - "script-src 'self' https://cdn.jsdelivr.net https://unpkg.com https://cdnjs.cloudflare.com https://busuanzi.ibruce.info"
    - "style-src 'self' https://cdn.jsdelivr.net https://unpkg.com https://cdnjs.cloudflare.com"
    - "img-src 'self' https://cdn.jsdelivr.net https://unpkg.com https://cdnjs.cloudflare.com"

4. 访问量统计:让数据说话

不蒜子统计(推荐:简单易用)

不蒜子是一个轻量级的访问量统计工具,无需注册即可使用。

配置步骤

1
2
3
4
5
6
# 主题配置文件 themes/butterfly/_config.yml
# 网站 PV / UV 统计 (不蒜子)
busuanzi:
site_uv: true # 显示网站访客数
site_pv: true # 显示网站总访问量
page_pv: true # 显示单页访问量

效果

  • 网站首页会显示总访客数和总访问量
  • 每篇文章底部会显示该文章的访问量

LeanCloud统计(进阶:数据可控)

如果你需要更详细的访问数据和完全可控的数据存储,可以使用LeanCloud统计。

配置步骤

  1. 访问 LeanCloud官网 注册并登录
  2. 创建应用,获取App ID和App Key
  3. 在主题配置中添加:
    1
    2
    3
    4
    5
    6
    7
    8
    # 主题配置文件 themes/butterfly/_config.yml
    leancloud:
    enable: true
    app_id: your_app_id
    app_key: your_app_key
    server_url: your_server_url # 国内节点需要配置
    path: /your_path # 可选,自定义路径
    web_analytics: true # 启用网站分析

注意事项

  • 国内用户需要配置 server_url(可在LeanCloud控制台获取)
  • 需要在LeanCloud的安全中心添加你的博客域名到Web安全域名列表

访问量显示位置

  • 侧边栏:显示网站总访客数和总访问量
  • 文章页:显示单篇文章的访问量
  • 首页文章卡片:可选择是否显示每篇文章的访问量

六、总结

通过以上优化,我的Hexo博客在性能、功能、用户体验等方面都得到了显著提升。这些优化工作不仅让博客更好用,也让我对Hexo的生态和Web开发有了更深入的理解。

优化是一个持续的过程,未来我还会探索更多优化方向,比如:

  • 接入更多分析工具,了解读者行为
  • 优化移动端体验
  • 探索更多主题定制可能性

如果你也是Hexo用户,希望这篇优化清单能对你有所帮助。欢迎在评论区分享你的优化经验!