如何修复 Hugo:can't evaluate field IsServer in type interface {}

问题:

构建 Hugo 站点时,你看到如下错误消息:

hugo_isserver_error.txt
03:27:57.943	ERROR render of "page" failed: "/opt/buildhome/repo/themes/tailbliss/layouts/_default/baseof.html:5:7": execute of template failed: template: _default/single.html:5:7: executing "_default/single.html" at <partial "head.html" .>: error calling partial: "/opt/buildhome/repo/themes/tailbliss/layouts/partials/head.html:25:6": execute of template failed: template: partials/head.html:25:6: executing "partials/head.html" at <hugo>: can't evaluate field IsServer in type interface {}

解决方案

此问题由 Hugo 找不到 .IsServer 引起:

error_excerpt.txt
can't evaluate field IsServer in type interface {}

例如在以下模板代码中:

hugo_template_example.html
{{ if hugo.IsServer }}
<link rel="stylesheet" href="{{ $styles.RelPermalink }}" />
{{ end }}

然而,.IsServer 仅在版本 0.120.0 中添加到 Hugo,所以如果你使用较旧版本,你会看到此错误。

修复很简单。你可以:

  • 推荐: 将你的 Hugo 版本更新到 0.120.0 或更新版本。撰写本文时,版本 0.134.2 是最新版本。
  • 如果不可能:从你的模板代码中删除 .IsServer 检查。这通常不会对你的站点产生太大影响(因为生产构建完全不受影响,hugo serve 开发构建通常只会略有改变),但你可能需要测试你的站点以确保一切按预期工作。

注意: 对于 Cloudflare Pages,你可以将部署环境变量 HUGO_VERSION 设置为 0.120.0 或更新版本以修复此问题。我用 0.134.2 测试过,工作正常。


按类别查看类似文章: Hugo