I'm setting up a new static website using Hugo and Wowchemy. Wowchemy uses the font Roboto, and I want to provide it locally with my website.
That's what I did:
- I installed the font files (TTF; WOFF) in
static/fonts
- I changed the value for
fonts
inparams.yaml
toRoboto
(Yes, that was a desperate move).
Nothing changed when re-starting the server. Using an add on for firefox, I could see that it is still loading the font from the remote site.
So what changes do I have to add in order to let the browser load my local copy of the fonts?
Steps to go:
tmp
....).toml
file defining the template's fonts./static/fonts
./assets/scss
, changing their suffix to.scss
custom.scss
which@import
s the font definitions. Remember that@import
is deprecated and will have to be replaced by@use
directives in the future.Long story:
In the beginning, we have to find the template files which are downloaded as modules. The easiest way is to call
hugo server
to make sure the modules have been downloaded, and then go to the path defined inassets/jsconfig.json
. The path is relative to the file's location and points straight to the corresponding templare directory. In my case, the string value is"../../../../../tmp/hugo_cache/modules/filecache/modules/pkg/mod/github.com/wowchemy/wowchemy-hugo-themes/modules/[email protected]/assets/*"
From now on, if we refer to thetemplate directory
, use the parent directory where thisassets
directory is located in.Next step: Find the
.toml
file defining the fonts. Go to your hugo build directory and have a look at the configuration fileparams.yaml
(located inconfig/_default
. Find the key forappearance/font
, e.g. looking like that:Now find the corresponding font file in the theme directory
data/fonts/<....>.toml
. This is the original file which makes sure the fonts are downloaded from google. You will find a parameter defining a call to google, looking like this:The link tells you which fonts are downloaded and with which weights. Make a note about these values, then create a minimal
data/fonts/<...>.toml
file in your build directory which overwrites the theme's data. For our purpose, it is enough to make sure that no google font is loaded, so we just use this file:Next, download the fonts with the with the appropriate sizes using the tool https://gwfh.mranftl.com/fonts. It's pretty straightforward, and we can use the built-in value for the directory where the fonts are located in. Download the fonts and unpack the archive in
static/fonts
.Then, save the CSS for the downloaded fonts in the directory
assets/<...>.scss
, where<...>
has to replaced by the font name, likeroboto
. In each SCSS file, also add the following directive to each font declaration:Now create a file
custom.scss
in the same directory which adds your custom font declaration. That can look like this:That's it! Restart the server and check that the fonts are downloaded. In Firefox, I use the built-in WebDevTools and check what files are loaded.