Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Different behaviour between "hugo" & "hugo server" for baseURL #7376

Closed
divinerites opened this issue Jun 10, 2020 · 11 comments
Closed

Different behaviour between "hugo" & "hugo server" for baseURL #7376

divinerites opened this issue Jun 10, 2020 · 11 comments

Comments

@divinerites
Copy link

divinerites commented Jun 10, 2020

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

$ hugo version
Hugo Static Site Generator v0.72.0/extended darwin/amd64 BuildDate: unknown

Does this issue reproduce with the latest release?

yes

I just noticed that I had a different behaviour between hugo server and hugo when it comes to (I think) multi-language and trailing /.

So i created a minimal repo (https://github.com/divinerites/mytest) where you can see it happening. I also commited the /public folder so you can also see the problem on the generated HTML.

  • my baseURL is : baseURL = "https://www.mysite.org"
  • there is one section highres in 3 languages.
  • When you run hugo server it works fine. You can click and link to all my pages and translations.

The line showing this problem is :@

{{ .Lang }} : <a href="{{ site.BaseURL | absURL }}{{ .Lang }}">{{ site.BaseURL | absURL }}{{ .Lang }}</a><br>

In this (right) "live server" generated code you can see that i get a / after the absURL.

en : <a href="http://localhost:1313/en">http://localhost:1313/en</a><br>
nl : <a href="http://localhost:1313/nl">http://localhost:1313/nl</a><br>

When I generate the HTML using just hugo, the / after the absURL is not generated

This comes from the public folder :

en : <a href="https://www.mysite.orgen">https://www.mysite.orgen</a><br>
nl : <a href="https://www.mysite.orgnl">https://www.mysite.orgnl</a><br>

I have this live on one of my site for this specific code.
All the remaining (complex) code works fine.

For the record this is my real code :

<a class="btn btn-main" href="{{ .Site.GetPage "section" "highres" | relLangURL }}highres/">{{ i18n "highres" }} <i class="tf-ion-android-open"></i></a>
@divinerites
Copy link
Author

I just tested with the dev version who takes care of this (#7357) but same behavior.

@khayyamsaleem
Copy link
Contributor

khayyamsaleem commented Jun 10, 2020

yep. not just trailing slash as well; if you have a baseURL like http://abc.com/test/, only /test is retained in the baseURL.

@divinerites
Copy link
Author

divinerites commented Jun 11, 2020

yep. not just trailing slash as well; if you have a baseURL like abc.com/test/, only /test is retained in the baseURL.

I do not see this behaviour (0.72.0). i get abc.com/test (and the original problem of course)

hugo server :

en : <a href="http://localhost:1313/test/en">http://localhost:1313/test/en</a><br>

hugo :

en : <a href="https://www.mysite.org/testen">https://www.mysite.org/testen</a><br>

@khayyamsaleem
Copy link
Contributor

@divinerites correct, i was mistaken.

@divinerites
Copy link
Author

divinerites commented Jun 12, 2020

And to be clear, the point is not "do I have a trailing / or not on baseURL or relLangURL", whatever rule is fine by me. I remember some talk/change (?) about this some months ago.

But why do we have a different behaviour between hugo server and hugo, so I think is a non expected behaviour.

@bep bep added this to the v0.73 milestone Jun 12, 2020
@bep bep modified the milestones: v0.73, v0.74 Jun 23, 2020
@bep bep modified the milestones: v0.74, v0.75 Jul 13, 2020
@bep bep modified the milestones: v0.75, v0.76 Sep 14, 2020
@bep bep modified the milestones: v0.76, v0.77 Oct 6, 2020
@bep bep modified the milestones: v0.77, v0.78 Oct 30, 2020
@bep bep modified the milestones: v0.78, v0.83 Apr 23, 2021
@bep bep modified the milestones: v0.83, v0.84 May 3, 2021
@bep bep modified the milestones: v0.84, v0.85 Jun 18, 2021
@bep bep modified the milestones: v0.85, v0.86 Jul 5, 2021
@bep bep modified the milestones: v0.86, v0.87, v0.88 Jul 26, 2021
@bep bep removed this from the v0.88 milestone Sep 2, 2021
@bep bep modified the milestones: v0.111.0, v0.110.0 Jan 26, 2023
@bep bep modified the milestones: v0.111.0, v0.112.0 Feb 15, 2023
@bep bep modified the milestones: v0.112.0, v0.113.0 Apr 15, 2023
@jmooring jmooring changed the title Different behaviour between "hugo" & "hugo server" for baseURL and multi-language (since recently ?) Different behaviour between "hugo" & "hugo server" for baseURL May 20, 2023
@jmooring
Copy link
Member

Simple example:

{{ warnf "%s" site.BaseURL }}

Then run:

hugo        --baseURL http://example.org/docs   --> http://example.org/docs
hugo server --baseURL http://example.org/docs   --> http://example.org:1313/docs/  # trailing slash

This behavior has been around for years, at least as far back as v0.54.0, and probably much earlier.

Avoid this by always including a trailing slash in site configuration. Per the docs:

baseURL

The absolute URL (protocol, host, path, and trailing slash) of your published site (e.g., https://www.example.org/docs/).

I wish site.BaseURL wasn't exposed to the templates (this approach is fragile), but that ship sailed a long time ago. Site and theme authors should be using absURL and absLangURL instead, or using .GetPage / resources.Get / .ResourcesGet and then reference the .Permalink value.

Adding a trailing slash, if missing, when running hugo will cause undesirable side effects for some sites.

Removing the trailing slash when running hugo server seems like a better idea, but that may break some stuff too.

@bep
Copy link
Member

bep commented May 20, 2023

wish site.BaseURL wasn't exposed to the templates

I agree, but once we get a grip of what is the actual API and generate reference docs from it, we can at least start by hiding stuff that should not be exposed in the first place, e.g. baseURL.

@bep bep modified the milestones: v0.113.0, v0.115.0 Jun 13, 2023
@bep bep modified the milestones: v0.115.0, v0.116.0 Jun 30, 2023
@bep bep modified the milestones: v0.116.0, v0.117.0 Aug 1, 2023
@bep bep modified the milestones: v0.117.0, v0.118.0 Aug 30, 2023
@bep bep modified the milestones: v0.118.0, v0.119.0 Sep 15, 2023
@bep bep modified the milestones: v0.119.0, v0.120.0 Oct 4, 2023
@bep bep modified the milestones: v0.120.0, v0.121.0 Oct 31, 2023
@jmooring
Copy link
Member

Closing as duplicate of #11669.

Copy link

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 github-actions bot locked as resolved and limited conversation to collaborators Dec 17, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

No branches or pull requests

5 participants