|
| 1 | +--- |
| 2 | +title: Install a custom font into a Netlify Docker Container |
| 3 | +type: article |
| 4 | +author: James Doc |
| 5 | +date: 2022-11-05 |
| 6 | +intro: This weekend I explored getting open graph images custom generated for each blog post and discovered that I'd need to install a custom font into my Netlify Docker container… |
| 7 | +tags: ["tech", "11ty", "code"] |
| 8 | +themeColor: hsl(180, 60%, 49%) |
| 9 | +--- |
| 10 | + |
| 11 | +This weekend I've been implementing automatically generated opengraph images for blog posts as a quick experiment for a side project I've been planning. There are number of methods to achieve this, Bernard Nijenhuis' [Eleventy only solution is really neat](https://bnijenhuis.nl/notes/automatically-generate-open-graph-images-in-eleventy/)[^1]. I've followed that implementation ([related commit](https://github.com/jamesdoc/jamesdoc.com/commit/a153a4c965d356e3a2b9c99ab933186caa42101e)) and it's working really nicely[^2]. |
| 12 | + |
| 13 | +The issue is that my modifications (and design for the social graphic) relies on having a custom font install at build time. That's not something that Netlify has in place. |
| 14 | + |
| 15 | +So… how do you get around this minor issue? You install the font. |
| 16 | + |
| 17 | +Netlify spins up a Docker container to run your build, this means that you can run a bash script in the container: |
| 18 | + |
| 19 | +```bash |
| 20 | +#!/bin/sh |
| 21 | + |
| 22 | +# Make a local font folder |
| 23 | +mkdir ~/.local/share/fonts |
| 24 | + |
| 25 | +# Copy the font from the repo to the right place |
| 26 | +cp src/_assets/fonts/space400.ttf ~/.local/share/fonts/space400.ttf |
| 27 | + |
| 28 | +# Reload the font cache |
| 29 | +fc-cache -f -v |
| 30 | +``` |
| 31 | + |
| 32 | +Then, in `package.json` file for the script that Netlify runs we can trigger that bash script to be run before anything else (such as the 11ty build) kicks off. |
| 33 | + |
| 34 | +```json |
| 35 | + "scripts": { |
| 36 | + "dev": "ELEVENTY_ENV=development eleventy --serve --incremental & mix watch", |
| 37 | + "netlify": "bash utils/fontInstall.sh && ELEVENTY_ENV=production eleventy && mix --production" |
| 38 | + }, |
| 39 | +``` |
| 40 | + |
| 41 | +Magically the custom font is installed and is now available for any requirements you have later on, like generating a whole bunch of social media graphics. |
| 42 | + |
| 43 | +The tweaks to make this work are all in [a commit for reference](https://github.com/jamesdoc/jamesdoc.com/commit/1eb5efb491fb07c5b4b780378fc69288c067d6b0). Many thanks to [Bernard](https://bnijenhuis.nl/) for open sourcing [his 11ty site](https://github.com/bnijenhuis/bnijenhuis-nl). |
| 44 | + |
| 45 | +[^1]: The basic summary is that 11ty can be used to generate more than `html` files. Eg, it can generate `svg` files, and then the 11ty Image plugin can convert an `svg` to a `jpg`… Pull this together and you can get a unique `svg` for each blog post or page, and then that svg can be turned into an image ready to be served up as an open graph graphic. |
| 46 | +[^2]: When I say really nicely… all the images are generated, however it turns out that I'm generating a lot of images which has slightly slowed down a fresh build (thank you 11ty caching for saving the day there). |
0 commit comments