Hugo: 在构建时添加 http-equiv=”refresh” content=”0。如何停止它?

huangapple go评论96阅读模式
英文:

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: &quot;Contact&quot;
draft: false
date: &quot;2020-02-25&quot;
type: &quot;page&quot;
layout: &quot;page&quot;
aliases:
- /contact.html
- /blog/contact/
- /blog/contact.html
---

{{&lt; contactformwide &gt;}}

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):

    &lt;!DOCTYPE html&gt;
    &lt;html&gt;
        &lt;head&gt;
            &lt;title&gt;https://mellifluous-marzipan-3893e2.netlify.app/contact/&lt;/title&gt;
            &lt;link rel=&quot;canonical&quot; href=&quot;https://mellifluous-marzipan-3893e2.netlify.app/contact/&quot;/&gt;
            &lt;meta name=&quot;robots&quot; content=&quot;noindex&quot;&gt;
            &lt;meta charset=&quot;utf-8&quot; /&gt;
            &lt;meta http-equiv=&quot;refresh&quot; content=&quot;0; url=https://mellifluous-marzipan-3893e2.netlify.app/contact/&quot; /&gt;
        &lt;/head&gt;
    &lt;/html&gt;

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 &lt;meta http-equiv=&quot;refresh&quot; ..., 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:

  &lt;meta http-equiv=&quot;X-UA-Compatible&quot; content=&quot;IE=edge,chrome=1&quot;&gt;
  &lt;meta http-equiv=&quot;content-type&quot; content=&quot;text/html; charset=utf-8&quot; /&gt;

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 &#39;https://mellifluous-marzipan-3893e2.netlify.app/contact/&#39;
    HTTP/2 301 
    // other headers and response body truncated
    
  • /contact ==> /contact/. (client redirect with http-equiv=&quot;refresh&quot;, alias are implemented with a similar mechanism. see How Aliases Work).

    $ curl -i &#39;https://mellifluous-marzipan-3893e2.netlify.app/contact&#39;
    HTTP/2 200
    // other headers truncated and response body formatted
    
    &lt;!DOCTYPE html&gt;
    &lt;html&gt;
      &lt;head&gt;
        &lt;title&gt;https://mellifluous-marzipan-3893e2.netlify.app/contact/&lt;/title&gt;
        &lt;link
          rel=&quot;canonical&quot;
          href=&quot;https://mellifluous-marzipan-3893e2.netlify.app/contact/&quot;
        /&gt;
        &lt;meta name=&quot;robots&quot; content=&quot;noindex&quot; /&gt;
        &lt;meta charset=&quot;utf-8&quot; /&gt;
        &lt;meta
          http-equiv=&quot;refresh&quot;
          content=&quot;0; url=https://mellifluous-marzipan-3893e2.netlify.app/contact/&quot;
        /&gt;
      &lt;/head&gt;
    &lt;/html&gt;
    

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).

huangapple
  • 本文由 发表于 2023年4月2日 08:23:02
  • 转载请务必保留本文链接:https://go.coder-hub.com/75909529.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定