withastro/astro · error · AstroError

CannotDetermineWeightAndStyleFromFontFile

CannotDetermineWeightAndStyleFromFontFile

Error message

An error occurred while determining the `weight` and `style` from local family "${family}" font file: ${url}.

What it means

When a font family variant omits `weight`/`style`, the fonts pipeline tries to read them from the file itself using `fontace(readFileSync(url))` (packages/astro/src/assets/fonts/infra/fontace-font-file-reader.ts:16). If the file cannot be read or parsed (corrupt file, HTML error page saved with a font extension, unsupported table layout), the failure is wrapped as `CannotDetermineWeightAndStyleFromFontFile` with the parse error as `cause`.

Solutions

  1. Set `weight` and `style` explicitly on that family/variant in the `fonts` config so Astro never needs to read them from the file.
  2. Verify the file is a real font: `file my-font.woff2` or open it in a font tool; re-download or replace corrupt files.
  3. Confirm the URL returns the font bytes (not an HTML error page) — check with `curl -L <url> | file -`.

Example fix

// astro.config.mjs — before (weight/style inferred from the file)
{ family: 'My Font', url: './fonts/my-font.woff2' }

// astro.config.mjs — after (declared explicitly)
{ family: 'My Font', url: './fonts/my-font.woff2', weight: 400, style: 'normal' }
Defensive patterns

Strategy: validation

Validate before calling

// always declare weight/style in the fonts config so the file is never parsed for them
const family = {
  family: 'My Font',
  variants: [{ weight: 400, style: 'normal', url: './fonts/my-font-400.woff2' }],
};

Prevention

When it happens

Trigger: A `fonts` config entry without explicit `weight` and `style` whose `url` points to a file that fontace cannot parse — corrupt downloads, a CDN 404 page saved as .woff2, or an unusual/legacy font binary.

Common situations: Fonts downloaded via curl from redirecting CDNs; minimal system fonts with stripped tables; switching a family's files while keeping the old config.

Related errors


AI-assisted analysis of withastro/astro@52e6c34790 (2026-08-18). Data as JSON: /api/errors/cfbf053143ced1d0. Report an issue: GitHub.

Appendix: source

Thrown at packages/astro/src/assets/fonts/infra/fontace-font-file-reader.ts:16

import { readFileSync } from 'node:fs';
import { fontace } from 'fontace';
import { AstroError, AstroErrorData } from '../../../core/errors/index.js';
import type { FontFileReader } from '../definitions.js';
import type { Style } from '../types.js';

export class FontaceFontFileReader implements FontFileReader {
	extract({ family, url }: { family: string; url: string }): { weight: string; style: Style } {
		try {
			const data = fontace(readFileSync(url));
			return {
				weight: data.weight,
				style: data.style as Style,
			};
		} catch (cause) {
			throw new AstroError(
				{
					...AstroErrorData.CannotDetermineWeightAndStyleFromFontFile,
					message: AstroErrorData.CannotDetermineWeightAndStyleFromFontFile.message(family, url),
				},
				{ cause },
			);
		}
	}
}

View on GitHub (pinned to 52e6c34790)