Tips for loading Google fonts quickly using Font Face Observer

Upon reading an enlightening article on the optimal methods for serving web fonts, I was eager to employ the innovative javascript library known as Font Face Observer to asynchronously load Google fonts on my website.

The documentation states that "fonts can be supplied by [...] a font service such as Google Fonts", yet fails to provide concrete examples of how to implement this. Despite having reviewed the suggested resources on Font Face Observer's official website, all demonstrations only covered using self-hosted fonts.

Has anyone successfully utilized Font Face Observer to load Google fonts? Alternatively, could you direct me to any documentation illustrating how to tackle this task?

Answer ā„–1

Utilizing a similar method has enabled me to implement lazy-loading of fonts. It is important to ensure that your Google font is included in the stylesheet link. Once done, simply utilize FontFaceObserver in the usual manner - passing the Google font name as an argument to create a new FontFaceObserver instance. For managing my Google fonts, I found a useful tool called google-fonts.

This is what a code sample might appear like:

import googleFonts from 'google-fonts';
import FontFaceObserver from 'fontfaceobserver';

loadGoogleFont(fontName) {
    googleFonts.add({[fontName]: true}); // or other options if needed
    const fontObserver = new FontFaceObserver(fontName);
    return fontObserver.load()
        .catch(err => console.log(fontName, err));
}

Answer ā„–2

Here is a straightforward method I have utilized, although its efficiency is still uncertain. This particular approach is advantageous because the font loading process does not hinder rendering due to having the script located in my JavaScript file towards the end. Essentially, the font begins loading as soon as my JS file starts parsing, allowing it to be rendered promptly upon completion.

import FontFaceObserver from 'fontfaceobserver'
import loadCSS from './util/loadCSS'

loadCSS('//fonts.googleapis.com/css?family=Open+Sans:300,400,400i,600,700')
const opensans = new FontFaceObserver('Open Sans')
opensans.load().then(() => document.body.classList.add('fonts--loaded'))

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Use regular expressions to split a number string based on the individual digit values

Is it possible to use regex to split a string that consists only of numbers based on matching digit values? Take for instance this input: "11222344" The expected output should be: ["11","222","3","44"] ...

Connect the plotly library to interact with the data in a Vue.js application through

I'm currently working on a project that involves using vue.js and the plot.ly JavaScript graph library. I am trying to figure out how to bind "pts" to the data's "TestSentences" in Vue. Below is my code snippet, thanks to everyone who has provide ...

Three.js - demonstrating the play of light and shadows across various perspectives

This particular example showcasing multiple views involves a complex implementation of shadows, rather than opting for the simpler method of just setting an object's property castShadow = true. Is it impossible to use the straightforward approach for ...

Getting the project path in the Sonarqube JavaScript Extension is a straightforward process

I am currently developing a custom rules plugin for the SonarQube Javascript Plugin. I have encountered an issue where I need to disable certain checks in specific directories, such as the bin path. My main question is: how can I obtain the file path rela ...

Tips for transferring an array of objects from an input field to a new array in JavaScript

In my current React project, I am facing a challenge with a text input that needs to accept values multiple times. The interface on the screen is set up like this: https://i.sstatic.net/Q19Tp.png Upon clicking "Next", the value entered will be submitted ...

Exploring the Power of an Enumerator in JavaScript

I'm in the process of converting the following VBScript code to JavaScript: Sub GxUIProxyVB_OnLogon Dim EntityProxy For Each EntityProxy In GxUIProxyVB.ListEntityProxy MsgBox EntityProxy.Name Next End Sub This code is an e ...

When using Chart JS, is there a way to create a line graph without including any labels at all?

Currently, I am facing a challenge with my Chart JS line graph. It needs to pull data from a backend and display it on a webpage. However, the chart has close to 1000 points to plot, making it impossible for me to provide labels for each point on both the ...

Using Handlebars to incorporate altered ajax information into a template

Currently, I am exploring ways to utilize manipulated data from an ajax request in conjunction with Handlebars. Within my database, the data is stored as an object like so: {"1 year":"$4.95", "2 years":"$3.95"} My objective now is to make use of this d ...

Is there a way to ensure that the header is centered at the top and the footer is centered at the bottom of every printed page, even when

Currently, I am utilizing spatie/browsershot to generate PDF documents within a Laravel project. While it offers convenient methods such as headerHtml and footerHtml for customization, there seems to be an issue with handling images. Specifically, only bas ...

Establishing a pre-selected option in a drop-down menu

I am working on a dropdown list and my goal is to have the default option selected when the page loads <b>Filter Within Months:</b> <select class="btn green" ng-model="myOptions" ng-options="i.label for i in items track by i.id" ng-change= ...

Tips for modifying the HTML generated on the server side before it is rendered on the client side in Quasar

Just starting out with Vue.js and looking to implement Vue I18n. However, I'm facing an issue setting the locale on Vue I18n and also setting the HTML lang attribute to the same locale. The challenge is that I can't access the document object in ...

What are some solutions for resolving differences in the display of pseudo elements between Macbooks and Windows desktops?

Greetings! I successfully converted an XD file to HTML and CSS code. I tested it on my Windows PC, but I haven't had the chance to test it on a Macbook yet. Details: I implemented a button with a pseudo element to show a right arrow on the right side ...

Creating an if statement that validates whether all variables have non-null values

I am still getting the hang of javascript and working on some coding projects from my textbooks. The current task involves creating an if statement to check if the values of the elements referenced by the names fname, lname, and zip are all not null. Here ...

Tips for ensuring the middle row remains sandwiched between the header and footer rows in Bootstrap

After adding styles for img with max-height:100% and max-width:100%, the display looked fine with just images. However, upon further inspection, I noticed that if the middle row (which can contain one or two columns side by side) has multiple images or an ...

PowerShell is having trouble detecting the Application during the packaging process

After completing the coding for a beginner Desktop Application in electron, I encountered an issue when trying to package it into an executable .exe file. The error message displayed was: Command failed: powershell.exe -nologo -noprofile -command "& { ...

JavaScript's Functions and Objects: An Overview

Create a function called 'transformFirstAndLast' which takes an array as input and outputs an object with: The first element of the array as the key of the object. The last element of the array as the value of that key. For example, if the inpu ...

Guide to generating a dynamic manifest.json file for Progressive Web Apps using ReactJS

I am working on a project that requires me to generate a dynamic manifest.json file for my PWA (ReactJS). Below are the relevant code snippets: app.service.js: function fetchAppData() { const requestOptions = { method ...

Insert a point onto the SVG polygon along the nearest line to the click event

In my React app, there's an SVG polygon element that initially appears as a square but can have new points added by double clicking inside the polygon area. I'm trying to ensure that the point is added to the side of the polygon closest to the p ...

Reducing the size of a Form Fieldset text once an option has been selected

Iā€™m brand new to the world of Javascript and Jquery and I'm currently working on designing a unique questionnaire-style webpage. My goal is to pose a question, then once an answer has been selected, shrink the text and display the next question in ...

Achieving flexbox's equal height for Bootstrap's Navbar and content

I am faced with the challenge of designing a layout where a content grid occupies the entire remaining page, while also incorporating a navigation bar. To achieve this, my approach involves placing the navigation bar within a flex container and the conten ...