英文:
Hugo: adding http-equiv="refresh" content="0 on build. Make it stop?
问题
我有一个非常小的Hugo网站,我正在尝试将其部署到Netlify上。然而,在构建网站时,我遇到了一个意外的无限循环问题,尽管在开发中使用hugo serve --ignoreCache --disableFastRender --noHTTPCache -D
命令似乎可以正常工作。
该网站由两个页面组成:
/
/contact/
关于联系页面,它直接位于content
文件夹中,内容如下:
---
title: "Contact"
draft: false
date: "2020-02-25"
type: "page"
layout: "page"
aliases:
- /contact.html
- /blog/contact/
- /blog/contact.html
---
{{< contactformwide >}}
然后,我在themes/MYTHEME/layouts/shortcodes/contactformwide.html
中放置了联系表单。
我遇到了一个非常奇怪的错误,当我使用hugo serve --ignoreCache --disableFastRender --noHTTPCache -D
命令构建网站时,一切正常。但是当我尝试将其部署到Netlify或者使用hugo
在本地构建时,/contact/
页面陷入了无限循环。在构建过程中没有抛出任何错误,它似乎只是一次又一次地尝试加载页面,但无济于事。
我尝试调整config.yaml
中的baseURL
为生产环境的适当域名,但没有帮助。
在/contact/
页面上呈现(和重新呈现、重新呈现)的HTML如下所示(这是来自Netlify的版本,因此使用了mellifluous-marzipan
):
<!DOCTYPE html>
<html>
<head>
<title>https://mellifluous-marzipan-3893e2.netlify.app/contact/</title>
<link rel="canonical" href="https://mellifluous-marzipan-3893e2.netlify.app/contact/"/>
<meta name="robots" content="noindex">
<meta charset="utf-8" />
<meta http-equiv="refresh" content="0; url=https://mellifluous-marzipan-3893e2.netlify.app/contact/" />
</head>
</html>
除了前四行之外,这个HTML与我期望呈现的内容基本没有关系。
我猜测问题出在<meta http-equiv="refresh" ...
这一行,但它是从哪里来的?为什么在开发中没有出现?我在通常的head
中只有以下两个http-equiv
,而主站点(使用相同的head
)现在是正常的(**编辑:错误已修复),所以你可以看到head
的样子:
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
**编辑:**我刚刚注意到,我在开发环境中得到的页面(在webaddress/contact
中)在构建中存储在public/contact/contact.html
中。
无论是从/
点击链接(正常工作)还是手动访问/contact/
,都会出现相同的结果。
有人可以帮我解决这个问题吗?我可以提供存储库访问权限等,但也许答案很明显,只是我太菜了看不到?也许与/contact/
页面的别名有关?
如果有帮助的话,当我将baseURL
设置为另一个URL时,我只是从baseURL/contact/
得到404错误,而不是这个无限循环。
**编辑:**我发现正确的页面位于Netlify网站中,在这里(**编辑:错误已修复)。它被放置在/public/contact/index.html
中。
**编辑:**事实证明,当我在layouts/contact.md
的frontmatter中放置url: contact
时,当构建网站时,public/contact.html
不再存在,只剩下public/contact/index.html
。
编辑:当我在frontmatter中放置以下内容时,我得到了public/contact.html
(带有正确的内容/页面),并且相同的页面也出现在public/contact/index.html
中。
**编辑:**事实证明,在/contact
、/contact/
和contact/
之间存在一些混淆。我最初链接到的是/contact/
,修复的方法似乎只是将链接更改为/contact
。不确定为什么会导致无限重定向,但现在它可以工作了。
英文:
I have a very small Hugo site built, which I'm trying to deploy to Netlify. I'm getting an unexpected infinite loop when I build the site, though it appears to work in dev using hugo serve --ignoreCache --disableFastRender --noHTTPCache -D
.
The site is comprised of two pages:
/
/contact/
In terms of the contact page, it's directly in the content
folder, and is as follows:
---
title: "Contact"
draft: false
date: "2020-02-25"
type: "page"
layout: "page"
aliases:
- /contact.html
- /blog/contact/
- /blog/contact.html
---
{{< contactformwide >}}
I then have the contact form in themes/MYTHEME/layouts/shortcodes/contactformwide.html
.
I have a very weird bug, where when I build the site using hugo serve --ignoreCache --disableFastRender --noHTTPCache -D
it works fine. When I try to deploy it to netlify or build it locally using hugo
, the /contact/
page gets stuck in an infinite loop. No errors are thrown when building, it just seems to try and try again to load the page, to no avail.
I have tried to adjust baseURL
in my config.yaml
to the appropriate domain for production, but that didn't help.
The HTML of the page this is rendering (and re-rendering, and re-rendering) at /contact/
is as follows (this is the version that's from Netlify, hence mellifluous-marzipan
):
<!DOCTYPE html>
<html>
<head>
<title>https://mellifluous-marzipan-3893e2.netlify.app/contact/</title>
<link rel="canonical" href="https://mellifluous-marzipan-3893e2.netlify.app/contact/"/>
<meta name="robots" content="noindex">
<meta charset="utf-8" />
<meta http-equiv="refresh" content="0; url=https://mellifluous-marzipan-3893e2.netlify.app/contact/" />
</head>
</html>
This HTML bears essentially no relationship, except beyond the first four lines, to what I'm expecting to render.
I assume the issue is that <meta http-equiv="refresh" ...
, but where is that coming from? Why doesn't it appear in dev? I have only the following two http-equiv
in my usual head
, and the main site (uses the same head
) is <s>up right now</s> (EDIT: bug fixed), so you can see what the head
looks like:
<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
<meta http-equiv="content-type" content="text/html; charset=utf-8" />
EDIT: I have just noticed that the page I want (what I get as webaddress/contact
in my development environment, is stored at public/contact/contact.html
in the build.
The same result occurs whether I click a link from /
(which works fine), or manually go to /contact/
.
Anyone can help me crack this? I can provide access to repo, etc., but maybe the answers obvious and I'm just too noob to see it? Maybe it has to do with the aliases for the /contact/
page?
If it helps, when I had baseURL set to another URL, I was just getting the 404 from the baseURL/contact/
I was being redirected to, not this infinite loop.
EDIT: I found that the correct page is on the netlify site, <s>here</s> (EDIT: bug fixed). It's being placed in /public/contact/index.html
.
EDIT: It also turns out that when I place url: contact
in the frontmatter of layouts/contact.md
, public/contact.html
ceases to exist when the site is built, and just public/contact/index.html
remains.
EDIT: When I place the following in the frontmatter, I get public/contact.html
(with the correct content / page), and THE SAME PAGE ALSO in public/contact/index.html
.
EDIT: Turns out there was some confusion between /contact
, /contact/
, and contact/
. I was originally linking to /contact/
and the fix appears to have been simply changing my links to /contact
. Not sure why that resulted in an infinite redirect, but it's working now.
答案1
得分: 1
重定向循环包含两个不同的重定向:
-
/contact/
==>/contact
(响应状态码为301)$ curl -i 'https://mellifluous-marzipan-3893e2.netlify.app/contact/' HTTP/2 301 // 其他头部和响应体被截断
-
/contact
==>/contact/
(客户端重定向,使用http-equiv="refresh"
,别名使用类似的机制实现。详见别名的工作原理)。$ curl -i 'https://mellifluous-marzipan-3893e2.netlify.app/contact' HTTP/2 200 // 其他头部被截断,响应体被格式化 <!DOCTYPE html> <html> <head> <title>https://mellifluous-marzipan-3893e2.netlify.app/contact/</title> <link rel="canonical" href="https://mellifluous-marzipan-3893e2.netlify.app/contact/" /> <meta name="robots" content="noindex" /> <meta charset="utf-8" /> <meta http-equiv="refresh" content="0; url=https://mellifluous-marzipan-3893e2.netlify.app/contact/" /> </head> </html>
看起来第一个重定向是有问题的。很可能是 Netlify 的重定向规则配置有误。请检查以下文件中的规则,看是否有任何问题:
_redirects
netlify.toml
(详见重定向和重写)。
英文:
The redirect loop contains two different redirects:
-
/contact/
==>/contact
. (response status code 301)$ curl -i 'https://mellifluous-marzipan-3893e2.netlify.app/contact/' HTTP/2 301 // other headers and response body truncated
-
/contact
==>/contact/
. (client redirect withhttp-equiv="refresh"
, alias are implemented with a similar mechanism. see How Aliases Work).$ curl -i 'https://mellifluous-marzipan-3893e2.netlify.app/contact' HTTP/2 200 // other headers truncated and response body formatted <!DOCTYPE html> <html> <head> <title>https://mellifluous-marzipan-3893e2.netlify.app/contact/</title> <link rel="canonical" href="https://mellifluous-marzipan-3893e2.netlify.app/contact/" /> <meta name="robots" content="noindex" /> <meta charset="utf-8" /> <meta http-equiv="refresh" content="0; url=https://mellifluous-marzipan-3893e2.netlify.app/contact/" /> </head> </html>
It seems that it's the first one to blame. It's most likely that the netlify redirect rules are configured incorrectly. Please check the rules in the following files to see if there is anything wrong:
_redirects
netlify.toml
(see Redirects and rewrites).
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论