Skip to main content

preload_font

Function preload_font 

Source
pub fn preload_font(href: impl AsRef<str>) -> Element
Expand description

A font preload: <link rel="preload" as="font" type="…" href="…" crossorigin>.

Fonts are fetched in anonymous CORS mode even from the same origin, so a font preload without crossorigin does not match the request the page makes later and the browser downloads the file twice. That is how the web platform behaves; this crate’s tests check the markup, not the browser. The helper always writes the attribute, so it cannot be forgotten.

type is taken from the extension of href, ignoring a query string or fragment: woff2, woff, ttf or otf. Any other extension, or none, leaves type out. The href goes through the URL policy like any other.

§Examples

use molt::prelude::*;

assert_eq!(
    preload_font("/fonts/inter.woff2").render(),
    r#"<link rel="preload" as="font" type="font/woff2" href="/fonts/inter.woff2" crossorigin>"#
);