Search code examples
faviconhugo

Hugo site favicon shows locally, but cannot be found in deployment


This is my first time using Hugo. I am using the Bento theme for my Hugo site which did not come with favicon support out of the box (I searched the entire code base and it is nowhere to be found). Consequently, I added the necessary HTML tags to the partials > head.html. When I run locally with hugo server -D or npm run dev, the favicon shows up fine.

I think there is an issue with how my relative href is written, however, whenever I change it, it breaks it locally. I am using AWS Amplify for deployment with auto cloudfront invalidation so that is not the issue.

When I inspect the page source of the deployed site, I get this for the favicons:

<link rel="apple-touch-icon" sizes="180x180" href="favicon%20not%20found%25!%28EXTRA%20string=apple-touch-icon.png%29">

You can see the issue in the href= field

Here is how my favicons are implemented in partial/head.html:

<link rel="icon" type="image/png" sizes="32x32" href="{{ "/img/favicon-32x32.png" | relURL }}">
<link rel="icon" type="image/png" sizes="16x16" href="{{ "/img/favicon-16x16.png" | relURL }}">
<link rel="apple-touch-icon" sizes="180x180" href="{{ "/img/apple-touch-icon.png" | relURL }}">

I also tried to implement this by adding the links in the config.toml and using Site.Params, but no luck with that either.


Solution

  • The below is how you would do it:

    <link ... href="/img/favicon-32x32.png">
    <link ... href="/img/favicon-16x16.png">
    <link ... href="/img/apple-touch-icon.png">
    

    So you don't have to use the hugo/Go {{}} tag (that would be for a site variable like {{.section }} - you can just put in the relative url which would simply be "/img/favicon-32x32...etc" Just make sure your image is in the corresponding folder.

    I tested on my install of hugo and as long as your favicon is in the right place - that handles your problem.

    • Also note: If you would like to use a site variable {{ $.Site.Params etc...}} such as a href="{{ #.Site.Params etc...}} you would add the variable name and value to your config file. See below for reference on this from hugodocs: Site Variables