惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

F
Fortinet All Blogs
有赞技术团队
有赞技术团队
量子位
N
Netflix TechBlog - Medium
博客园 - 叶小钗
博客园 - 三生石上(FineUI控件)
Google DeepMind News
Google DeepMind News
aimingoo的专栏
aimingoo的专栏
GbyAI
GbyAI
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
Blog — PlanetScale
Blog — PlanetScale
奇客Solidot–传递最新科技情报
奇客Solidot–传递最新科技情报
月光博客
月光博客
Martin Fowler
Martin Fowler
Y
Y Combinator Blog
宝玉的分享
宝玉的分享
博客园 - 司徒正美
云风的 BLOG
云风的 BLOG
V
Visual Studio Blog
V
V2EX
IT之家
IT之家
L
LangChain Blog
大猫的无限游戏
大猫的无限游戏
freeCodeCamp Programming Tutorials: Python, JavaScript, Git & More

Example blog

Ghost routing explained with routes.yaml examples — routes, collections & taxonomies Ghost redirects explained with practical redirects.yaml examples How to Serve JSON or XML Data in Ghost How to Change Your Default Post Template in Ghost Ghost Custom Theme Settings How to add Google Fonts to Ghost Themes How to Configure Ghost with Mailgun Ghost Theme Structure & Organization How to Make Custom Templates in Ghost Themes How to Install Ghost CMS via Ghost-CLI How to Add Syntax Highlighting to Ghost The best Ghost CMS hosting platforms Ghost 6.0 — Social Web and Native Ghost Analytics Ghost CMS Social web beta, email 2FA and more author social links How to keep custom design settings when updating your Ghost theme How to use the public preview card in Ghost How to add members-specific sections in Ghost How to customize the default content CTA in Ghost How to build a custom homepage in Ghost An overview of Ghost editor cards and and how they work Ghost CMS News: One-time payments & collecting sales tax How to customize the private site access template in Ghost How to add custom fonts in Ghost themes Ghost CMS News: Additional payment methods & internal linking How to use Code Injection in Ghost How to set meta data of your custom routes & collections in Ghost Ghost CMS News: TK reminders, ActivityPub and improvements How to format dates and use the date helper in Ghost Ghost CMS internationalization, custom fonts & spam protection Understanding and optimizing post and page settings in Ghost CMS
How to Self-Host Google Fonts in Ghost CMS
Bright Themes · 2022-03-20 · via Example blog

Google Fonts are widely used nowadays, many Ghost Themes have such fonts integrated by default and this can be an issue because Google Fonts is subject to GDPR regulations (General Data Protection Regulation).

A German court recently found that websites that have embedded Google Fonts violate GDPR because when a font is requested by the user’s browser, the user’s IP address is recorded by Google and used for analytical purposes.

To be GDPR compliant, you have several options:

  • stop using Google Fonts altogether
  • self-host the fonts

In the next part, we will focus on the second option and the steps you have to do, to use Google fonts in GDPR compliant manner (self-host) in your Ghost Theme.

  1. Remove google font requests
  2. Download the font files
  3. Generate the CSS rules
  4. Integrate the font files and CSS rules

Remove google font requests

Generally, to add google fonts you have to add the following code (example with the Inter font):

<link rel="preconnect" href="https://fonts.googleapis.com" />
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
<link
  href="https://fonts.googleapis.com/css2?family=Inter:wght@400;500;600;700;800;900&display=swap"
  rel="stylesheet"
/>

To avoid any outgoing requests towards the Google servers, you have to look for this code and remove it from your template. The most common place where this is placed is in the default.hbs file, but if it's not there, try searching within the theme directory for fonts.g

After you removed the <link> elements you can double-check in DevTools that there are no outgoing requests, by going to the Network tab and the Font section.

Download the font files

After the previous step is done, and your website doesn't have requests towards the google servers, you should see the fallback fonts loaded instead, which in most cases will be browser fonts.

If you still want to retain the same look and feel, you have to self-host the fonts. You have to download the font files and add them to your theme. Fonts can be downloaded following the steps below (or do it via google-webfonts-helper, see next chapter for more details)

  • Go to fonts.google.com
  • Search for the font family you want to download
  • Select the styles (font weights)
  • Finally, press Download all to get the zip file

After downloading, you have to place the font files in your Ghost theme. The ideal place would be in the assets/fonts directory, if the fonts directory doesn't exist you can create it.

When all this is done, you are ready to self-host the fonts.

Generate the CSS rules

When using custom fonts, you must define the @font-face rules, which specify a custom font with which to display text. The default Google fonts integration handles this automatically, but in case you want to self-host you have to do it yourself.

Luckily there is a handy tool (google-webfonts-helper) that generates these rules for us (see the github project). The process is done in several steps:

  1. Select charsets (default is latin)
  2. Select style (font weights)
  3. Copy CSS (modern browsers is enough in most cases)
  4. Download files (as mentioned previously the files have to be placed in the assets directory)

One important aspect is the option to change the directory prefix before generating the CSS rules.

Caution

For the fonts to work, it's important that the CSS rules have the correct path to the font files.

Integrate the font files and CSS rules

As already mentioned, the font files have to be placed within the theme (ideally in the assets/fonts directory). When this is done, the last step is adding the CSS.

CSS can be added either into a CSS file or directly using the Code Injection area available in Ghost Admin. Let's see an example.

Assuming we want to integrate the Inter font, after selecting the styles and downloading the font files, we have the following in the /assets/fonts directory:

  • inter-v8-lating-700.woff
  • inter-v8-lating-700.woff2
  • inter-v8-lating-800.woff
  • inter-v8-lating-800.woff2
  • inter-v8-lating-regular.woff
  • inter-v8-lating-regular.woff2

And the following CSS rules:

/* inter-regular - latin */
@font-face {
  font-family: 'Inter';
  font-style: normal;
  font-weight: 400;
  src: local(''),
    url('/assets/fonts/inter-v8-latin-regular.woff2') format('woff2'), /* Chrome 26+, Opera 23+, Firefox 39+ */
      url('/assets/fonts/inter-v8-latin-regular.woff') format('woff');
  /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
}

/* inter-700 - latin */
@font-face {
  font-family: 'Inter';
  font-style: normal;
  font-weight: 700;
  src: local(''), url('/assets/fonts/inter-v8-latin-700.woff2') format('woff2'),
    /* Chrome 26+, Opera 23+, Firefox 39+ */
      url('/assets/fonts/inter-v8-latin-700.woff') format('woff');
  /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
}

/* inter-800 - latin */
@font-face {
  font-family: 'Inter';
  font-style: normal;
  font-weight: 800;
  src: local(''), url('/assets/fonts/inter-v8-latin-800.woff2') format('woff2'),
    /* Chrome 26+, Opera 23+, Firefox 39+ */
      url('/assets/fonts/inter-v8-latin-800.woff') format('woff');
  /* Chrome 6+, Firefox 3.6+, IE 9+, Safari 5.1+ */
}

Warning

Note that the path to the font files /assets/fonts/ has to match where the font files actually are (if you placed the font files in the assets directory, the @font-face url needs that path)

Add the font rules, inside <style> tag in Code Injection. Now that this is done, the only thing remaining is making sure the website uses the correct font. This may vary based on how it is set in your theme, but adding the following rule in code injection should work:

body * {
  font-family: 'Inter';
}

That's it, you are now self-hosting google fonts within your Ghost theme.