Write a function that builds the preload links a page needs.
The tea shop's build knows which files each page needs early that the preload scanner can't find in its HTML: a font named in the stylesheet, a hero set as a CSS background, a script that a loader adds. Write preloadLinks(resources, options), which returns the <link rel="preload"> tags for the page's head, each as an object of its attributes. resources lists the files as { url, as, type, crossorigin, media }, in the order the page needs them; only url and as are always there.
Each link is { rel: "preload", href, as }, with href the resource's url as written, plus type, media, and crossorigin when the resource has them, and no key for an attribute it doesn't have. A font's link always has crossorigin: "", whatever the resource says: browsers fetch fonts in CORS mode without credentials, and a preload that doesn't match the font's own request is fetched twice. A fetch link gets the resource's crossorigin, or "" when it has none, since fetch() requests are CORS requests too.
A file listed twice is preloaded once, as its first listing says. Two URLs are the same file when they resolve to the same address: on this page, /fonts/lora.woff2 and https://example.test/fonts/lora.woff2 are one. options.lcp is the URL of the page's LCP image, compared the same way. When a resource with as: "image" is that image, its link gets fetchpriority: "high", since a preloaded image otherwise keeps an image's low priority, and goes first; the others keep their order. When options.lcp is missing or names no image in the list, no link gets fetchpriority. Return at most options.max links, every link when it isn't given, cutting from the end, so the LCP image is the last to go.
preloadLinks(
[
{ url: "/fonts/lora.woff2", as: "font", type: "font/woff2" },
{ url: "/js/reviews.js", as: "script" },
{ url: "/img/mint-hero.avif", as: "image", type: "image/avif" },
{ url: "https://example.test/fonts/lora.woff2", as: "font", type: "font/woff2" },
],
{ lcp: "/img/mint-hero.avif", max: 2 },
);
// [
// { rel: "preload", href: "/img/mint-hero.avif", as: "image", type: "image/avif", fetchpriority: "high" },
// { rel: "preload", href: "/fonts/lora.woff2", as: "font", type: "font/woff2", crossorigin: "" },
// ]
solution.jsfunction preloadLinks(resources, options = {}) {
// Today: one link per resource, in the order given, with only its href and as.
return resources.map((resource) => ({ rel: "preload", href: resource.url, as: resource.as }));
}
- one link per file, with its type and media, and no keys it doesn't have
- every font gets an empty crossorigin, even on the page's own origin
- a fetch keeps its crossorigin or gets an empty one, and other kinds have one only when given
- a file listed twice is preloaded once, as first listed, even when written another way
- the LCP image goes first with fetchpriority high, and no other link gets it
- only an image can be the LCP image
- keeps at most max links, cutting from the end, so the LCP image stays
The answer
Write it yourself first. Then compare it with a reference solution and the checks it passes.
Checking your sign-in…