hexo博客網(wǎng)站主頁空白或404

太久沒更新博客了,最近準(zhǔn)備拾起來。為了“改頭換面”,今天想調(diào)整一下 sidebar 上的頭像,因?yàn)槭切码娔X,沒有 hexo 等環(huán)境,于是按照之前分享過的一篇博文,安裝 hexo,替換 source 目錄下的頭像圖片并調(diào)整 _config.xml,本地預(yù)覽一切正常,然后直接執(zhí)行 hexo d部署完成,訪問網(wǎng)站域名 www.yangbing.club 發(fā)現(xiàn)直接404了,刷新了多次,用無痕瀏覽,等了許久再試,還是老樣子,這可把我嚇壞了,從未遇到過這等情況,等于博客直接掛了。

web-site-404.png

排查和定位

github 上的線索

發(fā)現(xiàn) Actions 中此次 hexo deploy 觸發(fā)的 build 和 deploy 均失敗了。

build error 如下,detail

/usr/local/bundle/gems/jekyll-3.9.0/lib/jekyll/theme.rb:84:in `rescue in gemspec': The hexo-theme-next theme could not be found. (Jekyll::Errors::MissingDependencyException)
    from /usr/local/bundle/gems/jekyll-3.9.0/lib/jekyll/theme.rb:81:in `gemspec'
    from /usr/local/bundle/gems/jekyll-3.9.0/lib/jekyll/theme.rb:19:in `root'
    from /usr/local/bundle/gems/jekyll-3.9.0/lib/jekyll/theme.rb:12:in `initialize'
    from /usr/local/bundle/gems/jekyll-3.9.0/lib/jekyll/site.rb:439:in `new'
    from /usr/local/bundle/gems/jekyll-3.9.0/lib/jekyll/site.rb:439:in `configure_theme'
    from /usr/local/bundle/gems/jekyll-3.9.0/lib/jekyll/site.rb:55:in `config='
    from /usr/local/bundle/gems/jekyll-3.9.0/lib/jekyll/site.rb:23:in `initialize'
    from /usr/local/bundle/gems/jekyll-3.9.0/lib/jekyll/commands/build.rb:30:in `new'
    from /usr/local/bundle/gems/jekyll-3.9.0/lib/jekyll/commands/build.rb:30:in `process'
    from /usr/local/bundle/gems/github-pages-225/bin/github-pages:70:in `block (3 levels) in <top (required)>'
    from /usr/local/bundle/gems/mercenary-0.3.6/lib/mercenary/command.rb:220:in `block in execute'
    from /usr/local/bundle/gems/mercenary-0.3.6/lib/mercenary/command.rb:220:in `each'
    from /usr/local/bundle/gems/mercenary-0.3.6/lib/mercenary/command.rb:220:in `execute'
    from /usr/local/bundle/gems/mercenary-0.3.6/lib/mercenary/program.rb:42:in `go'
    from /usr/local/bundle/gems/mercenary-0.3.6/lib/mercenary.rb:19:in `program'
    from /usr/local/bundle/gems/github-pages-225/bin/github-pages:6:in `<top (required)>'
    from /usr/local/bundle/bin/github-pages:23:in `load'
    from /usr/local/bundle/bin/github-pages:23:in `<main>'
/usr/local/lib/ruby/2.7.0/rubygems/dependency.rb:311:in `to_specs': Could not find 'hexo-theme-next' (>= 0) among 158 total gem(s) (Gem::MissingSpecError)
Checked in 'GEM_PATH=/github/home/.gem/ruby/2.7.0:/usr/local/lib/ruby/gems/2.7.0:/usr/local/bundle', execute `gem env` for more information
    from /usr/local/lib/ruby/2.7.0/rubygems/dependency.rb:323:in `to_spec'
    from /usr/local/lib/ruby/2.7.0/rubygems/specification.rb:986:in `find_by_name'
    from /usr/local/bundle/gems/jekyll-3.9.0/lib/jekyll/theme.rb:82:in `gemspec'
    from /usr/local/bundle/gems/jekyll-3.9.0/lib/jekyll/theme.rb:19:in `root'
    from /usr/local/bundle/gems/jekyll-3.9.0/lib/jekyll/theme.rb:12:in `initialize'
    from /usr/local/bundle/gems/jekyll-3.9.0/lib/jekyll/site.rb:439:in `new'
    from /usr/local/bundle/gems/jekyll-3.9.0/lib/jekyll/site.rb:439:in `configure_theme'
    from /usr/local/bundle/gems/jekyll-3.9.0/lib/jekyll/site.rb:55:in `config='
    from /usr/local/bundle/gems/jekyll-3.9.0/lib/jekyll/site.rb:23:in `initialize'
    from /usr/local/bundle/gems/jekyll-3.9.0/lib/jekyll/commands/build.rb:30:in `new'
    from /usr/local/bundle/gems/jekyll-3.9.0/lib/jekyll/commands/build.rb:30:in `process'
    from /usr/local/bundle/gems/github-pages-225/bin/github-pages:70:in `block (3 levels) in <top (required)>'
    from /usr/local/bundle/gems/mercenary-0.3.6/lib/mercenary/command.rb:220:in `block in execute'
    from /usr/local/bundle/gems/mercenary-0.3.6/lib/mercenary/command.rb:220:in `each'
    from /usr/local/bundle/gems/mercenary-0.3.6/lib/mercenary/command.rb:220:in `execute'
    from /usr/local/bundle/gems/mercenary-0.3.6/lib/mercenary/program.rb:42:in `go'
    from /usr/local/bundle/gems/mercenary-0.3.6/lib/mercenary.rb:19:in `program'
    from /usr/local/bundle/gems/github-pages-225/bin/github-pages:6:in `<top (required)>'
    from /usr/local/bundle/bin/github-pages:23:in `load'
    from /usr/local/bundle/bin/github-pages:23:in `<main>'
  Logging at level: debug
Configuration file: /github/workspace/./_config.yml
             Theme: hexo-theme-next
github-pages 225 | Error:  The hexo-theme-next theme could not be found.

deploy error 如下,detail

Actor: sherlockyb
Action ID: 2021541511
Artifact URL: https://pipelines.actions.githubusercontent.com/QrvX2bmakfCgkbXyG4yo3O1Y8LmS1Eviu5tBLZPSm6baFeu9Sw/_apis/pipelines/workflows/2021541511/artifacts?api-version=6.0-preview
{"count":0,"value":[]}
Failed to create deployment for 64e432b334b99462a8b0c082f955d6f5a99e6cde.
Error: Error: No uploaded artifact was found! Please check if there are any errors at build step.
Error: Error: No uploaded artifact was found! Please check if there are any errors at build step.
Sending telemetry for run id 2021541511

deploy 不用看,自然是 build 失敗導(dǎo)致缺少需要的文件。從 build 錯誤日志中看到,

Error: The hexo-theme-next theme could not be found.

這個沒改過,我又確認(rèn)了下 site 目錄下的文件夾,hexo-theme-next 是存在的。于是我將其改為 next 試試,還是不行,感覺不是這個問題,此路不通。

可能是CNAME失效,導(dǎo)致域名跳轉(zhuǎn)失???

直接訪問 https://sherlockyb.github.io 試試,發(fā)現(xiàn)并沒有出現(xiàn)404,但首頁空白,難道 hexo 生成的 index.html 是空的?通過 inspect 看了下首頁源碼,還真是空白頁!除了 html, head 和 body 三對空標(biāo)簽,其他什么內(nèi)容都沒有。

github-io-index-empty.png

然后也查看了下本地 hexo generate 產(chǎn)生的 public 文件夾,發(fā)現(xiàn)不僅 index.html,很多其他文件也都是 zero bytes,

local-hexo-generate-empty-file.png

于是問題變?yōu)?hexo 生成的HTML為空,繼續(xù) Google,發(fā)現(xiàn)有網(wǎng)友也遇到過類似問題并解決了,原因是,

hexo 與 node 的版本不兼容,要么 node 過高,要么 hexo 過低

我查了下本地安裝的 hexo 版本,./node_modules/hexo/bin/hexo --version

hexo: 3.9.0
hexo-cli: 2.0.0
os: Darwin 20.6.0 darwin x64
node: 15.5.0
v8: 9.6.180.15-node.16
uv: 1.43.0
zlib: 1.2.11
brotli: 1.0.9
ares: 1.18.1
modules: 102
nghttp2: 1.47.0
napi: 8
llhttp: 6.0.4
openssl: 3.0.2+quic
cldr: 40.0
icu: 70.1
tz: 2021a3
unicode: 14.0
ngtcp2: 0.1.0-DEV
nghttp3: 0.1.0-DEV

hexo-3.9.0 是比較老的版本了,截止發(fā)稿最新版已經(jīng)是 6.1.0 了,而 node 的版本卻高達(dá) 15.5.0,是比較符合前面提到的 cause 的,剩下的問題就是如何升級 hexo 或者降級 node 了。

解決問題

降級 node

先看看如何降級 node。

先卸載再重裝是可以的,且簡單粗暴,但并不想這么做,因?yàn)橐延泻芏嗥渌?xiàng)目的前端代碼也依賴當(dāng)前版本的 node,如果因?yàn)榻导?node 版本而帶來未知的影響得不償失。網(wǎng)上又 Google 了一番,發(fā)現(xiàn)有 nvm 這個好東西,可以隨時切換指定版本的 node,就它了。

安裝 nvm

先是 brew install nvm ,但執(zhí)行完后,嘗試 nvm 命令時卻提示 command not found,按照文中網(wǎng)友說法,通過 brew 安裝存在 bug,可用如下腳本安裝,

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

在 Mac 下如果遇到如下錯誤,

./install.sh: line 416: /Users/sherlockyb/.zshrc: Permission denied

直接進(jìn)到 nvm 的默認(rèn)的 git 目錄 ~/.nvm,然后執(zhí)行 sudo ./install.sh,可以看到安裝腳本會轉(zhuǎn)而使用 .bash_profile文件,

Appending nvm source string to /Users/sherlockyb/.bash_profile

安裝結(jié)束后,重新打開 terminal 查看 nvm 版本如下,表示安裝成功。

sherlockyb@07L0220100005DD sherlockyb.github.io % nvm --version
0.39.1

用 nvm 降級 node 到 12.14.0

用法很簡單,兩行命令就搞定,

nvm install 12.14.0
nvm use 12.14.0

這里需注意,nvm use 只是臨時切換 node 版本,只適用于當(dāng)前 terminal,如果打開新的 terminal,node 還是之前的舊版本。如果想要永久切換的話,可使用如下命令,

nvm alias default 12.14.0

然后再查看 node 版本如下,已經(jīng)降級了

sherlockyb@07L0220100005DD sherlockyb.github.io % node --version
v12.14.0

此時再通過 ./node_modules/hexo/bin/hexo --version 查看 hexo 及其依賴包的版本時如下,node 版本已經(jīng)變了。

hexo: 3.9.0
hexo-cli: 2.0.0
os: Darwin 20.6.0 darwin x64
node: 12.14.0
v8: 7.7.299.13-node.16
uv: 1.33.1
zlib: 1.2.11
brotli: 1.0.7
ares: 1.15.0
modules: 72
nghttp2: 1.39.2
napi: 5
llhttp: 1.1.4
http_parser: 2.8.0
openssl: 1.1.1d
cldr: 35.1
icu: 64.2
tz: 2019c
unicode: 12.1

重新生成HTML

清除并重新 generate,

./node_modules/hexo/bin/hexo clean
./node_modules/hexo/bin/hexo g

然后再查看 public 文件夾,會發(fā)現(xiàn) index.html 已經(jīng)正常生成了,打開后內(nèi)容也是正常。最后本地預(yù)覽一下,網(wǎng)站各項(xiàng)功能都沒啥問題。

local-hexo-generate-successful-file.png

部署

最后通過 ./node_modules/hexo/bin/hexo d 部署到GitHub Pages,網(wǎng)站恢復(fù)正常。

升級 hexo

降級 node 已經(jīng)被驗(yàn)證是可行且簡單的,不妨再嘗試下升級 hexo 會咋樣。

此次 hexo 是通過 npm install hexo 安裝的,得到的默認(rèn)版本就是 3.9.0,嘗試升級 hexo 到最新版。

先執(zhí)行 npm i hexo-cli

安裝特別慢,這種一般是由于國內(nèi)訪問 npm 默認(rèn)的外網(wǎng)官方源 https://registry.npmjs.org/ 比較慢,于是將其切換為國內(nèi)鏡像,

npm config set registry http://r.cnpmjs.org/

再執(zhí)行上述命令時,發(fā)現(xiàn)快很多。

此時再查看 hexo 版本,暫時還未變化。

再依次執(zhí)行如下命令

npm install npm-check                     // 安裝 npm-check 到當(dāng)前 node_modules 目錄
./node_modules/npm-check/bin/cli.js       // 查看系統(tǒng)插件是否需要升級
npm install npm-upgrade
./node_modules/npm-upgrade/lib/bin/cli.js // 更新 package.json
npm update --save                         // 更新插件

執(zhí)行完 npm update --save 后,可以看到 hexo 版本升級到了 6.1.0,并且 hexo-deployer-git 等相關(guān) hexo 插件也隨之升級了。

+ hexo-generator-category@1.0.0
+ hexo-generator-archive@1.0.0
+ hexo-deployer-git@3.0.0
+ hexo-renderer-marked@5.0.0
+ hexo-generator-search@2.4.3
+ hexo-generator-index@2.0.0
+ hexo@6.1.0
+ hexo-generator-sitemap@3.0.1
+ hexo-renderer-ejs@2.0.0
+ hexo-generator-tag@1.0.0
+ hexo-renderer-stylus@2.0.1
+ hexo-server@3.0.0
added 93 packages from 104 contributors, removed 259 packages and updated 56 packages in 382.579s

問題層出不窮

就當(dāng)我以為這就搞定了時,執(zhí)行 ./node_modules/hexo-cli/bin/hexo --version 時卻報如下錯誤,

FATAL YAMLException: Specified list of YAML types (or a single Type object) contains a non-Type object.
    at /Users/biyang/DevCodes/sherlockyb.github.io/node_modules/hexo/node_modules/js-yaml/lib/schema.js:104:13
    at Array.forEach (<anonymous>)
    at Schema.extend (/Users/biyang/DevCodes/sherlockyb.github.io/node_modules/hexo/node_modules/js-yaml/lib/schema.js:102:12)
    at Object.<anonymous> (/Users/biyang/DevCodes/sherlockyb.github.io/node_modules/hexo/lib/plugins/renderer/yaml.js:5:36)
    at Module._compile (node:internal/modules/cjs/loader:1108:14)
    at Object.Module._extensions..js (node:internal/modules/cjs/loader:1137:10)
    at Module.load (node:internal/modules/cjs/loader:973:32)
    at Function.Module._load (node:internal/modules/cjs/loader:813:14)
    at Module.require (node:internal/modules/cjs/loader:997:19)
    at require (node:internal/modules/cjs/helpers:92:18)
    at module.exports (/Users/biyang/DevCodes/sherlockyb.github.io/node_modules/hexo/lib/plugins/renderer/index.js:15:16)
    at Hexo.init (/Users/biyang/DevCodes/sherlockyb.github.io/node_modules/hexo/lib/hexo/index.js:235:35)
    at /Users/biyang/DevCodes/sherlockyb.github.io/node_modules/hexo-cli/lib/hexo.js:49:17
    at tryCatcher (/Users/biyang/DevCodes/sherlockyb.github.io/node_modules/bluebird/js/release/util.js:16:23)
    at Promise._settlePromiseFromHandler (/Users/biyang/DevCodes/sherlockyb.github.io/node_modules/bluebird/js/release/promise.js:547:31)
    at Promise._settlePromise (/Users/biyang/DevCodes/sherlockyb.github.io/node_modules/bluebird/js/release/promise.js:604:18) {
  reason: 'Specified list of YAML types (or a single Type object) contains a non-Type object.',
  mark: undefined
}

hexo 6.1.0 的 bug

Google了一番發(fā)現(xiàn),在這個 issue 中找到了答案,貌似是 6.1.0 版本引入了 bug,解決方案是回退到 6.0.0,

hexo-bug.png

于是通過 npm i hexo@6.0.0 回退,執(zhí)行成功后,再次查看 hexo 版本,回復(fù)正常了。

hexo: 6.0.0
hexo-cli: 4.3.0
os: darwin 20.6.0 11.6
node: 15.5.0
v8: 8.6.395.17-node.23
uv: 1.40.0
zlib: 1.2.11
brotli: 1.0.9
ares: 1.17.1
modules: 88
nghttp2: 1.41.0
napi: 7
llhttp: 2.1.3
openssl: 1.1.1i
cldr: 37.0
icu: 67.1
tz: 2019c
unicode: 13.0

swig模板失效了

然后嘗試重新生成 HTML,卻發(fā)現(xiàn)index.html文件內(nèi)容長這樣,

{% extends '_layout.swig' %}
{% import '_macro/post.swig' as post_template %}
{% import '_macro/sidebar.swig' as sidebar_template %}

{% block title %} {{ config.title }} {% endblock %}

{% block page_class %}
  {% if is_home() %} page-home {% endif %}
{% endblock %}

{% block content %}
  <section id="posts" class="posts-expand">
    {% for post in page.posts %}
      {{ post_template.render(post, true) }}
    {% endfor %}
  </section>

  {% include '_partials/pagination.swig' %}
{% endblock %}

{% block sidebar %}
  {{ sidebar_template.render(false) }}
{% endblock %}

這是 next/layoutindex.swig 中的原始內(nèi)容,說明 swig 模板壓根沒被處理,因?yàn)槲业?NexT 主題還比較老(5.1.0),用的是 swig,而從該 issue 可得知,鑒于 swig 缺乏維護(hù),hexo 從 5.0 開始移除了對 swig 模板的支持,改為獨(dú)立的 hexo-renderer-swig 插件,對于 NexT 則是從 7.4.2 版本開始,使用 Nunjucks 代替 swig 作為新的模板引擎。也就是說想要繼續(xù)使用 swig,需要單獨(dú)安裝 hexo-renderer-swig 插件。順便看了下 swig 的官網(wǎng),最新的 Release News 也是兩年前了。

話不多說,執(zhí)行 npm install hexo-renderer-swig,然后重新生成 HTML,看了下內(nèi)容,這回終于正常了。

若干小問題

通過 ./node_modules/hexo/bin/hexo s 本地預(yù)覽,網(wǎng)站整體功能是沒問題,但又發(fā)現(xiàn)若干小問題,

  • sidebar 的頭像沒有了,據(jù)說是 hexo 從 5.4.0 開始就去掉 avatar 的配置項(xiàng)了,交由主題去管理,好在 NexT 5.1.0 是有 avatar 這個配置項(xiàng)的,只不過之前沒開啟而已,這個還挺容易解決。
  • 翻頁的button 處,awesome icon 不展示,顯示為源碼了
  • 查看單個博客上下滑動時,左側(cè)目錄欄不跟著一起變了

從前面降級 node 可以看出,之前 3.9.0 版本的 hexo 還是好的,看起來就是高版本的 hexo 6.0 與低版本的 NexT 5.1.0 有若干不兼容的地方。上面提到的只是通過簡單驗(yàn)證發(fā)現(xiàn)的問題,可能還有其他未知的問題。當(dāng)然,這些小問題都是可以通過改配置或者是改源碼來修復(fù)。

放棄并回歸 node 降級

考慮到時間成本,我就不繼續(xù)下去了,放棄升級,回滾 hexo 至 3.9.0,最終采用降級 node 的方案。

劃重點(diǎn)

  • hexo 與 node 版本不兼容,可能導(dǎo)致生成的HTML為空,建議降級 node 更安全。
  • hexo 5.0 之后不再內(nèi)置支持 swig,若需要,得單獨(dú)安裝 hexo-renderer-swig。
  • nvm 乃管理 node 版本的神器,推薦。

同步更新到原文: https://www.yangbing.fun/2022/03/24/hexo-blog-website-page-blank-or-404/

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡書系信息發(fā)布平臺,僅提供信息存儲服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容