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

[BUG] img icon in header only renders on landing page #622

Closed
LeonardAukea opened this issue Nov 8, 2021 · 5 comments
Closed

[BUG] img icon in header only renders on landing page #622

LeonardAukea opened this issue Nov 8, 2021 · 5 comments
Labels
bug Something isn't working

Comments

@LeonardAukea
Copy link

Assume that you reference an image icon in config.yaml like so:

  label:
    text: home
    icon: img/logo-icon.svg
    iconHeight: 35

It will only render on the landing page as relativeURL's are not used in the header.html partial:

<img src="{{- .Site.Params.label.icon -}}" alt="logo" aria-label="logo"
height="{{- .Site.Params.label.iconHeight | default " 30px" -}}">

Therefore, you will end up looking for the file in the wrong location if you are on another site, as an example:

GET http://localhost:1313/posts/test/img/logo-icon.svg 404 (Not Found)

@LeonardAukea LeonardAukea added the bug Something isn't working label Nov 8, 2021
@adityatelange
Copy link
Owner

  label:
    text: home
--  icon: img/logo-icon.svg
    iconHeight: 35
  label:
    text: home
++  icon: /img/logo-icon.svg
    iconHeight: 35

I hope this should solve the issue.

@LeonardAukea
Copy link
Author

LeonardAukea commented Nov 10, 2021

```diff
  label:
    text: home
--  icon: img/logo-icon.svg
    iconHeight: 35
  label:
    text: home
++  icon: /img/logo-icon.svg
    iconHeight: 35

I hope this should solve the issue.

How will this prevent the following request to be made GET http://localhost:1313/posts/test/img/logo-icon.svg 404 (Not Found) ?

@danielfdickinson
Copy link
Contributor

danielfdickinson commented Nov 10, 2021 via email

@passw0rd123
Copy link

The leading slash resolves the issue as long as you have deployed the hugo website in the domain root.

Otherwise, if the website is served from a subfolder you have to add the subfolder also in the icon path.

baseURL: 'https://example.com/subfolder/'
params:
  label:
    icon: /subfolder/img/logo-icon.svg

@moomop
Copy link

moomop commented Nov 21, 2021

Hi - I think the answer to this is to use absURL something like:

diff --git a/layouts/partials/header.html b/layouts/partials/header.html
index f53ab52b..cc636598 100644
--- a/layouts/partials/header.html
+++ b/layouts/partials/header.html
@@ -46,7 +46,7 @@
             {{- if .Site.Title }}
             <a href="{{ "" | absLangURL }}" accesskey="h" title="{{ $label_text }} (Alt + H)">
                 {{- if .Site.Params.label.icon }}
-                <img src="{{- .Site.Params.label.icon -}}" alt="logo" aria-label="logo"
+                <img src="{{- .Site.Params.label.icon | absURL -}}" alt="logo" aria-label="logo"
                     height="{{- .Site.Params.label.iconHeight | default " 30px" -}}">
                 {{- end -}}
                 {{- $label_text -}}

That works for me for base URLs with and without subfolders.

The suggestion above to add the subfolder in the icon path does work, but is inconvenient if you frequently want to change the subfolder (e.g. for deploying to a test site I specify a different baseURL using --baseURL on the hugo command line).

Note that absURL will only keep the subfolder in https://example.com/subfolder/ if the icon path doesn't start with a slash (so use something like icon: img/logo-icon.svg rather than icon: /img/logo-icon.svg. Otherwise absURL will generate a URL from the server root (i.e. won't include the subfolder)

kylethedeveloper pushed a commit to kylethedeveloper/hugo-PaperMod that referenced this issue Feb 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants