Skip to article
Documentation

src/lib/perf/font-preloads.test.ts

Linked repository file from this build.

import { expect, test } from "bun:test";
import { fontPreloads } from "./font-preloads";

test("counts fonts in HTTP Link headers even when HTML contains no hints", () => {
  expect(
    fontPreloads(
      "<h1>Home</h1>",
      '</a.woff2>; rel=preload; as="font"; crossorigin="", </b.woff2>; as="font"; rel="preload"',
    ),
  ).toEqual(["/a.woff2", "/b.woff2"]);
});

test("deduplicates HTML/header hints and excludes images, scripts and prefetch", () => {
  const html =
    '<link href="/a.woff2" as="font" rel="preload"><link rel="preload" as="script" href="/app.js"><link href="/b.woff2" rel="prefetch" as="font">';
  expect(
    fontPreloads(html, '</a.woff2>; rel=preload; as="font", </image.png>; rel=preload; as="image"'),
  ).toEqual(["/a.woff2"]);
});