hugo icon indicating copy to clipboard operation
hugo copied to clipboard

site.GetPage outputs a wrong link to a tag when the page is not found

Open anirbanbasu opened this issue 2 years ago • 3 comments

What version of Hugo are you using (hugo version)?

$ hugo version
hugo v0.102.1+extended darwin/arm64 BuildDate=unknown

Does this issue reproduce with the latest release?

Yes, it does.

According to the .GetPage documentation at https://gohugo.io/functions/getpage/:

.GetPage returns a page of a given path. Both Site and Page implements this method. The Page variant will, if given a relative path – i.e. a path without a leading / – try look for the page relative to the current page.

According to the discussion (https://github.com/gohugoio/hugo/issues/8494#issuecomment-830683069), I had modified site_footer.html (see: https://github.com/anirbanbasu/anirbanbasu.github.io/blob/master/layouts/partials/site_footer.html) to work around the issue. However, Site.GetPage returns a link to a certain tag when the page is not found. To reproduce this problem for my website, I inserted the following in my site_footer.html:

    <p>
      <code>site.GetPage "/privacy.md"</code>: <i>{{(site.GetPage "/privacy.md")}}</i><br/>
      <code>.GetPage "/privacy.md"</code>: <i>{{(.GetPage "/privacy.md")}}</i>
    </p>

This results in an output, the text of which looks like:

site.GetPage "/privacy.md": Page(/tags/privacy) .GetPage "/privacy.md": nopPage

This output should be visible (while this issue is being addressed) at the bottom of the page at the mirror website https://anirbanbasu.netlify.app that is deployed from the experimental branch of my repository (https://github.com/anirbanbasu/anirbanbasu.github.io/tree/experimental). The problem only happens because there is actually a valid tag called privacy on my website, see https://anirbanbasu.netlify.app/tag/privacy/. If such a tag does not exist, this problem will not show up, e.g., https://www.magcamit.com/ which has no tag called privacy (https://www.magcamit.com/tags/). I am assuming that the problem is not with the term privacy only and any other tags could create this sort of wrong hyperlinks.

The expectation is that both of the above calls to .GetPage should return nopPage. Note that Page(/tags/privacy) is also wrong because the correctly available URL is /tag/privacy, i.e., tag without its plural.

anirbanbasu avatar Sep 02 '22 02:09 anirbanbasu

Note that Page(/tags/privacy) is also wrong because the correctly available URL is /tag/privacy, i.e., tag without its plural.

I believe this is happened because .GetPage method sees the content structure, not the published structure where /tag/privacy/ target path is produced by permalinks config.

You can try call .RelPermalink of that returned Page object, it will returns /tag/privacy/


The expectation is that both of the above calls to .GetPage should return nopPage.

The behavior still correct, site.GetPage method will match to any Page. Taxonomies also living under content structure, Even though they're not exists in your local filesystem.

content/
  posts/
    first-post.md
  tags/       (Taxonomy virtual path)
    privacy/  (Term virtual path)          
  • posts is a Page
  • first-post.md is a Page
  • tags is a Page
  • privacy is a Page

But, actually i am little bit suprised, because user being explicit here by calling /privacy.md (a filename with extension), turns out it doesnt make Hugo being more strict when matching the Page.

Especially, when we adding extra metadata to Term Kind Page we need to construct this structure:

content/
  posts/
    first-post.md
  tags/
    _index.md
    privacy/
      _index.md          

I have not tested it yet, but i assume site.GetPage "/privacy.md" call will return to /tags/privacy/?


Currently developing a theme, so thanks to this issue :+1: , i think i need to create extra check whether the returned Page of explicit call (/filename.ext) equals with .File.LogicalName.

pamubay avatar Sep 02 '22 06:09 pamubay

@pamubay is (mostly, I haven't read all) right in his observations. I know there are some issues with GetPage, and that comes mostly from a poor definition of what a page path is in Hugo. I have done some work on cleaning this up in a soon to be released version of Hugo. I'm not saying that that will fix all/your problem, but it will certainly make for a better specification.

bep avatar Sep 02 '22 07:09 bep

@pamubay Thanks for the explanation. I understand what you are saying regarding finding privacy at different parts of the content tree including virtual branches. What is concerning is that I am searching for something specific, i.e., /privacy.md and yet it matches stuff where privacy is found. Thanks for this:

Currently developing a theme, so thanks to this issue 👍 , i think i need to create extra check whether the returned Page of explicit call (/filename.ext) equals with .File.LogicalName.

@bep Thanks for including this in the v.0.103.0 milestone. If, at least, the specification/documentation of the GetPage function is clear about what it may return then theme developers can perform further checks to ensure correct output.

anirbanbasu avatar Sep 02 '22 13:09 anirbanbasu

Resolved with https://github.com/gohugoio/hugo/pull/11894

jmooring avatar Jan 31 '24 19:01 jmooring

This issue has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.

github-actions[bot] avatar Feb 22 '24 01:02 github-actions[bot]