Add Preloaded Fonts in Vartheme
Following the new way in the new Drupal default front end theme Olivero
{% include '@olivero/includes/preload.twig' with { olivero_path: olivero_path } only %}
Check the code on Olivero
The html.html.twig file https://git.drupalcode.org/project/drupal/-/blob/10.1.x/core/themes/olivero/templates/layout/html.html.twig#L43
The preload.twig file https://git.drupalcode.org/project/drupal/-/blob/10.1.x/core/themes/olivero/templates/includes/preload.twig
Having the following in Vartheme BS5. And the include.
Custom Vartheme Sub Themes are able to add their preloaded assets on demand.
Steps to Add a New Web Font
1. Add Web Font Files in the Fonts Folder
Example: The Lora font
Go to the Lora web font page and Download the family.
Download directly from fonts.google.com or from the source in github.com
Check the license before using the web font
These fonts are licensed under the Open Font License.
You can use them freely in your products & projects - print or digital, commercial or otherwise.
This isn't legal advice, please consider consulting a lawyer and see the full license for all details.
Not recommended to use not licensed font!
Buy a license if it was possible and budget for a unique branded font.
Or hire a typography font designer.
Copy the list of web font files to
"PROJECT_PATH/themes/custom/MYTHEME/fonts/lora/webfonts"
Create the
"PROJECT_PATH/themes/custom/MYTHEME/fonts/lora/css/lora.css"
fileDefine the
@font-face
for the Lora font.
Having more file formats if that was available by web font conversion tools
For sure including a remote web font, But issues with preloading may start follow up
2. Add the @font-face CSS File to a Library
Example: The Lora font
List the
"PROJECT_PATH/themes/custom/MYTHEME/fonts/lora/css/lora.css"
file in libraries to be activated in the theme
3. Add the Font to The preload.twig File
Recommended to add for faster loading of font in pages
Edit the
"PROJECT_PATH/themes/custom/MYTHEME/templates/includes/preload.twig"
file. Add the link rel preload and the patch to the font file withas="font"
andtype
4. Clear Cache And Test The Font
Content of the Preload.twig File
Change the logic in this file in the generated theme for the project. To match the need of your website.
Available Variables
vartheme_bs5_path
: Returns the path to the Vartheme BS5 theme. In the generated cloned Vartheme theme, the variable could bemythemename_path
html_dir
: contains the language direction. It will either be 'ltr' or 'rtl'.
Example of logic:
Multilingual websites may use a language selection for preloaded assets.
Sections of the website may have a custom prelead assets for CSS/JS.
Have a look at "Preload critical assets to improve loading speed" https://web.dev/preload-critical-assets/ for more ideas on what/when/where to use preload.
Links to Learn by Example
Check the following links:
Vartheme Fonts folder:
https://git.drupalcode.org/project/vartheme_bs5/-/tree/3.0.x/fonts
Vartheme html.html.twig file: https://git.drupalcode.org/project/vartheme_bs5/-/blob/3.0.x/templates/system/html.html.twig#L50
Vartheme preload.twig file: https://git.drupalcode.org/project/vartheme_bs5/-/blob/3.0.x/templates/includes/preload.twig
Last updated