Skip to content
This repository has been archived by the owner on Jan 4, 2023. It is now read-only.

Font URL is relative to css directory #67

Closed
rnewman opened this issue Jan 6, 2022 · 7 comments
Closed

Font URL is relative to css directory #67

rnewman opened this issue Jan 6, 2022 · 7 comments

Comments

@rnewman
Copy link

rnewman commented Jan 6, 2022

This stylesheet:

https://github.com/zwbetz-gh/cupper-hugo-theme/blob/master/assets/css/template-styles.css#L4

refers to a font in path css/fonts/:

/* Fonts */
@font-face {
    font-family: 'Miriam Libre';
    src: url('{{ "css/fonts/miriamlibre-bold.woff2" | absURL }}') format('woff2'), url('{{ "css/fonts/miriamlibre-bold.woff" | absURL }}') format('woff');
    font-weight: bold;
    font-style: normal;
}

This is built by Hugo into public/css/styles.css, where it looks like this:

/* Fonts */
@font-face {
    font-family: 'Miriam Libre';
    src: url('css/fonts/miriamlibre-bold.woff2') format('woff2'), url('css/fonts/miriamlibre-bold.woff') format('woff');
    font-weight: bold;
    font-style: normal;
}

and Hugo puts the font directory next to it:

$ find public/css
public/css
public/css/styles.css
public/css/images
public/css/images/icon-tick.svg
public/css/images/stripe.svg
public/css/images/arrow_effect.svg
public/css/prism.css
public/css/search.fe0cd54a21628574bff49d721c827d1bb165ab56b0f22dd55ae78addbe61c309.css
public/css/fonts
public/css/fonts/miriamlibre-bold.woff2
public/css/fonts/miriamlibre-bold.woff

However, CSS paths are relative to the CSS file. That @font-face.src thus causes the browser to load css/css/fonts/miriamlibre-bold.woff2, which fails with a 404:

Screen Shot 2022-01-05 at Jan 5 17 05 11

This does not occur using hugo server.

I'm not sure if this is a Hugo bug (because it's clearly processing the CSS and the font) or a bug in the theme, but inserting / to turn it into url('/css/fonts…') or removing css/ altogether from the url both cause correct CSS to be generated but break the behavior when loaded via hugo server.

To Reproduce

  • Build an example Hugo site with this theme.
  • Deploy it to Netlify or use a local HTTP server.

Expected behavior

The generated CSS refers to a correct path.

@zwbetz-gh
Copy link
Owner

Thanks for reporting this @rnewman. Am going to do some digging.

@zwbetz-gh
Copy link
Owner

Hmm. I am not able to reproduce this.

For example, I have a demo of the theme deployed to Netlify. When I view the devtools for a sample post, all is fine.

https://cupper-hugo-theme.netlify.app/cupper-shortcodes/


Then when I inspect the CSS, it is not as you say in your report. Instead, I see the full url for each font as expected.

https://cupper-hugo-theme.netlify.app/css/styles.css

/* Fonts */
@font-face {
    font-family: 'Miriam Libre';
    src: url('https://cupper-hugo-theme.netlify.app/css/fonts/miriamlibre-bold.woff2') format('woff2'), url('https://cupper-hugo-theme.netlify.app/css/fonts/miriamlibre-bold.woff') format('woff');
    font-weight: bold;
    font-style: normal;
}

For reference, the demo is currently built with hugo version 0.83.1

HUGO_VERSION = "0.83.1"

@rnewman
Copy link
Author

rnewman commented Jan 6, 2022

I wonder if this is a consequence of the relativeURLs setting?

@zwbetz-gh
Copy link
Owner

zwbetz-gh commented Jan 6, 2022 via email

rnewman added a commit to rnewman/hugo-example that referenced this issue Jan 6, 2022
@rnewman
Copy link
Author

rnewman commented Jan 6, 2022

OK, I built a repro, but I think I also figured this out.

hugo server replaces your baseURL (which causes no end of confusion for users).

It's not documented what the right value should be when deploying for multiple names (as is the case when deploying to staging, or to Netlify with and without an external DNS name) or without hardcoding a particular base URL: advice varies between the empty string, a slash, or using scripts to override it when building.

I had chosen the empty string, and everything works except for this.

Using baseURL: "/" makes my simple case work.

However, it's still not clear what the right thing should be if I were to deploy into a path, and thus need real relative URL support.

Changing the stylesheet to:

/* Fonts */
@font-face {
    font-family: 'Miriam Libre';
    src: url('{{ "../css/fonts/miriamlibre-bold.woff2" | absURL }}') format('woff2'), url('{{ "../css/fonts/miriamlibre-bold.woff" | absURL }}') format('woff');
    font-weight: bold;
    font-style: normal;
}

makes both "" and "/" work as the value of baseURL; perhaps consider changing this?

https://inspiring-franklin-01af8a.netlify.app/ is a Netlify deploy of my repro case with that commit stacked on top. The font is correct in both hugo server and in the deploy.

@zwbetz-gh
Copy link
Owner

Am glad to hear adjusting your baseURL got you further.

But we are beginning to get into a land that is out of scope for what is a "theme issue" vs what is a "general hugo issue".

In the deployed url you linked, one issue is that when you have a page that is nested a few dirs deep, the ../css/fonts/miriamlibre-bold.woff2 will no longer work.

If you'd like to debug your particular scenario further, please open a thread over at https://discourse.gohugo.io/

@rnewman
Copy link
Author

rnewman commented Jan 6, 2022

Yes, agreed. No need to debug further — I sadly don't have time to try to futz with fundamentals of Hugo — but I thought it was worth writing up the situation for closure.

Thanks for the responses!

@rnewman rnewman closed this as completed Jan 6, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants