Published on

解决因递归调用导致 Hugo 渲染超时的问题

Authors
  • avatar
    作者
    老麦

前因

套了 CDN 的图床被刷,决定将 Hugo 博客里的图片都保存回本地。用了这么久的 Cloudflare 服务,我对其访问速度非常之满意,心想着自己的博客流量也就那么点,用户体验这个问题先放一边吧。

方案

将图片从储存桶拉回本地,修改并开启 Hugo 的 Image processing1 功能,本地测试一切顺利。但万万没想到在 Github Actions 上构建时就报错了。

ERROR render of "page" failed: 

"/home/runner/work/laomaiorg/laomaiorg/layouts/_default/baseof.html:21:11": execute of template failed: template: _default/single.html:21:11: executing "_default/single.html" at <partial "base/footer" .>: error calling partial: partial "base/footer" timed out after 30s. This is most likely due to infinite recursion. If this is just a slow template, you can try to increase the 'timeout' config setting. Total in 49887 ms 

Error: error building site: render: failed to render pages: render of "page" failed:

"/home/runner/work/laomaiorg/laomaiorg/layouts/_default/baseof.html:21:11": execute of template failed: template: _default/single.html:21:11: executing "_default/single.html" at <partial "base/footer" .>: error calling partial: partial "base/footer" timed out after 30s. This is most likely due to infinite recursion. If this is just a slow template, you can try to increase the 'timeout' config setting. 

Error: Process completed with exit code 1.

根据提示可以看出是 footer 这个部件的问题,这时我就想到应该是 footer 里的统计字数的函数出现了递归调用的问题,导致渲染超时。也难怪,处理 268 张图片的同时还进反复统计,报错也说得通。去 Hugo 官方文档一搜便找到了解决方法。

timeout 2

默认值:“30 秒”

生成页面内容的超时,指定为持续时间或以秒为单位。注意: 这用于摆脱递归内容生成。如果您的页面生成速度很慢(例如,因为它们需要大图像处理或依赖于远程内容),您可能需要提高此限制。

结果

考虑增加渲染模板的超时限制。在 Hugo 配置文件(config.tomlconfig.yamlconfig.json)中添加以下设置,以 config.yaml 为例:

timeout: 120s

Footnotes

  1. https://gohugo.io/content-management/image-processing/

  2. https://gohugo.io/getting-started/configuration/#timeout