抱歉,您的浏览器无法访问本站
本页面需要浏览器支持(启用)JavaScript
了解详情 >

hehe 童鞋选择将所有的站点托管到杜老师这「是收费的」这样就可以专心维护网站的内容,也不用费心思在运维上。在做站点迁移时遇到了 Hugo 框架的一个报错,原因是使用 Hugo 搭建相册网站需要遍历大量图片,而在生成站点文件时出现了超时问题,本文记录解决方法。

报错信息

1
2
3
ERROR render of "page" failed: "/home/runner/work/photo/photo/themes/gallery/layouts/_default/single.html:3:5": execute of template failed: template: _default/single.html:3:5: executing "main" at <partial "gallery.html" .>: error calling partial: partial "gallery.html" 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 60412 ms
Error: error building site: render: failed to render pages: render of "page" failed: "/home/runner/work/photo/photo/themes/gallery/layouts/_default/single.html:3:5": execute of template failed: template: _default/single.html:3:5: executing "main" at <partial "gallery.html" .>: error calling partial: partial "gallery.html" 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.

注意:杜老师是通过 GitHub Actions 来部署,逻辑是先准备 Hugo 的运行环境,再根据站点的数据渲染站点文件。在生成站点文件时出现如上错误信息。

解决思路

从错误信息看,Hugo 网站在渲染页面时出现了问题,具体是 gallery.html 这个 partial 文件在执行时超时了,并且怀疑是由于无限递归导致的。

首先检查 gallery.html 中代码。无限递归问题可能是 gallery.html 中调用了自身,或者在调用其它 partial 时形成了循环。仔细检查文件,确保没有递归调用自己或其它可能导致循环的部分。

其次检查数据结构。如果 gallery.html 中依赖某些数据结构,可能是数据结构中存在循环引用。例如,某个对象或者列表在渲染时不断递归调用。

再次增加超时时间。如果确认不是无限递归问题,而是模板渲染确实很慢,可以尝试增加超时时间。在 Hugo 的配置文件中,增加 timeout 配置项,如 timeout = '60000'

然后优化模板性能。如果模板渲染确实很慢,可能是模板的代码过于复杂。可以尝试优化模板代码,减少不必要循环和复杂逻辑。

接着调试模板。使用 Hugo 的调试工具来逐步检查模板的执行过程。可通过在模板中添加日志输出来帮助调试。

再者检查依赖插件。如果网站使用第三方插件或依赖,可能是插件导致了问题。尝试禁用插件,看看能否解决问题。

最后检查 Hugo 的版本。确保使用的 Hugo 版本是最新的。旧版本可能存在已知的 bug,而新版本可能已修复了这些问题。

评论