gohugoio / hugo

The world’s fastest framework for building websites.
https://gohugo.io
Apache License 2.0
75.38k stars 7.49k forks source link

Ugly URLs for Menu of `pageRef = '/'` Does Not Generate an Ugly URL (New Theme from `hugo new theme`) #11878

Closed matttproud closed 9 months ago

matttproud commented 9 months ago

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

% hugo version
hugo v0.119.0-b84644c008e0dc2c4b67bd69cccf87a41a03937e linux/amd64 BuildDate=2023-09-24T15:20:17Z VendorInfo=gohugoio

Does this issue reproduce with the latest release?

Yes. I the recreation steps with hugo v0.121.2-6d5b44305eaa9d0a157946492a6f319da38de154 linux/amd64 BuildDate=2024-01-05T12:21:15Z VendorInfo=gohugoio. The same misbehavior happens.

I checked specifically:

  1. New Hugo regenerating the static content from the existing Hugo workspace.
  2. Creating a new theme in the new Hugo and using that with the static content generation.

The problem happens in both cases.

Recreation Steps

  1. % hugo new site quickstart
  2. % cd quickstart
  3. % hugo new theme theme
  4. % echo "theme = 'theme'" >> hugo.toml
  5. % echo "uglyURLs = true" >> hugo.toml
  6. % hugo
  7. % less public/index.html

    Within step no. 7, verify what the menu link for Home points to. The link will have href="/", which is incorrect per having ugly URLs enabled.

What I Expect to Happen

The default template output from hugo new theme should emit output such that step no. 7 from above shows a href="/index.html" for the Home menu item instead of href="/".

Diagnosis

I am uncertain whether this is a problem with the Hugo new theme generator or the internals of Hugo itself. I suspect it is not the theme generator given that file themes/theme/hugo.toml contains:

baseURL = 'https://example.org/'
languageCode = 'en-us'
title = 'My New Hugo Site'

[[menus.main]]
name = 'Home'
pageRef = '/'
weight = 10

[[menus.main]]
name = 'Posts'
pageRef = '/posts'
weight = 20

[[menus.main]]
name = 'Tags'
pageRef = '/tags'
weight = 30

[module]
  [module.hugoVersion]
    extended = false
    min = "0.116.0"

It seems that pageRef should canonicalize to /index.html with ugly URLs enabled.

When running the Hugo development server locally, everything appears to work, as the server remaps / back to /index.html. If someone is using pure static content serving without URL rewriting (cf. Apache mod_dir with the DirectoryIndex directive), then the emitted URL is incorrect. I am in that boat.

Workaround

I found I could work around adjusting the menus.main directive in file themes/theme/hugo.toml to the following:

[[menus.main]]
name = 'Home'
url = "//index.html"
weight = 10

That doesn't seem super scalable/reliable, though.

jmooring commented 9 months ago

Duplicate of https://github.com/gohugoio/hugo/issues/7497

github-actions[bot] commented 8 months ago

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.