Await Document Fonts With fonts.ready

Gate rendering on document.fonts.ready, request specific faces with fonts.load, and probe availability synchronously with fonts.check.

TypescriptData Fetching

Code

typescriptsnippet
20 lines
// document.fonts is a FontFaceSet: promise-based readiness for the fonts
// the document uses, plus targeted loads for faces a widget needs first.
async function prepareCatalogFonts(): Promise<void> {
  await document.fonts.ready; // resolves when pending document fonts settle
  console.log('initial fonts settled');

  // load() fetches a specific face before the first paint that needs it
  const faces = await document.fonts.load('600 16px "IBM Plex Sans"', 'Aa 123');
  console.log('faces loaded:', faces.length);

  // loadingdone fires whenever a batch of document font loads finishes
  document.fonts.addEventListener('loadingdone', (event) => {
    console.log('just loaded:', event.fontfaces.map((face) => face.family));
  });

  // check() answers synchronously whether a face is already available,
  // optionally limited to the glyphs of a given text
  console.log('600 face ready:', document.fonts.check('600 16px "IBM Plex Sans"'));
  console.log('cjk glyphs ready:', document.fonts.check('600 16px "IBM Plex Sans"', '目录'));
}

Inside the snippet

Await Document Fonts With fonts.ready comes down to the function prepareCatalogFonts.

The async parts await their results, which keeps ordering predictable.

What to watch for

  • The snippet leans on document — browser-only surfaces a bare Node process cannot provide.

Putting it to use

Signature: async function prepareCatalogFonts()

The demo’s lines are annotated (initial fonts settled, faces loaded:, just loaded:) — one annotation per operation the file performs.

Lines that do the work

document.fonts.addEventListener('loadingdone', (event) => {
  console.log('just loaded:', event.fontfaces.map((face) => face.family));
});
const faces = await document.fonts.load('600 16px "IBM Plex Sans"', 'Aa 123');
await document.fonts.ready; // resolves when pending document fonts settle
console.log('initial fonts settled');

Interactive Tools for This Topic

FAQ

Does Await Document Fonts With fonts.ready run in plain Node.js?
No — it touches `document`, which exist only in the browser. Run it in a page (or the DevTools console) instead of a bare Node process.