Before a request to a new origin can go out, the browser looks up the name, opens a connection, and negotiates TLS: up to three round trips. <link rel="preconnect"> starts that work early. Write preconnectHints(pageUrl, resourceUrls, options), which takes the page's URL and the URLs the page requests, in the order it requests them, and returns the preconnect links to put in the head, each as an object of attributes.
Only other origins need one: requests to the page's own origin go over the connection the page came on. Get each URL's origin, its scheme, host, and port, with new URL(url, pageUrl).origin, so a relative URL is the page's own; a data: URL's origin is "null", and it needs no connection at all. List each origin once, in the order of its first request, and stop after options.max of them, 4 when it isn't given. Lighthouse warns when a page has more than 4 preconnects, and web.dev's advice is to preconnect only to the few origins the page will use soon, since the browser closes a connection that goes unused for about 10 seconds.
Each link is { rel: "preconnect", href }, where href is the origin. Add crossorigin: "" when the origin is in options.cors, the origins that serve fonts or answer CORS requests (written as an origin, or as any URL on it). Those requests are sent without credentials, and the browser keeps them off connections opened for requests with credentials, so their preconnect needs crossorigin to open a connection they can use.
preconnectHints("https://www.leafandkettle.com/menu", [
"/css/menu.css",
"https://fonts.googleapis.com/css2?family=Lora",
"https://fonts.gstatic.com/s/lora/v32/lora.woff2",
"https://cdn.leafandkettle.com/teas/green.avif",
"https://www.leafandkettle.com/js/menu.js",
"https://fonts.gstatic.com/s/lora/v32/lora-italic.woff2",
], { cors: ["https://fonts.gstatic.com"] });
// [
// { rel: "preconnect", href: "https://fonts.googleapis.com" },
// { rel: "preconnect", href: "https://fonts.gstatic.com", crossorigin: "" },
// { rel: "preconnect", href: "https://cdn.leafandkettle.com" },
// ]
const hints = preconnectHints("https://www.leafandkettle.com/menu", [
"/css/menu.css",
"https://fonts.googleapis.com/css2?family=Lora",
"https://fonts.gstatic.com/s/lora/v32/lora.woff2",
"https://cdn.leafandkettle.com/teas/green.avif",
"https://www.leafandkettle.com/js/menu.js",
"https://fonts.gstatic.com/s/lora/v32/lora-italic.woff2",
"https://cdn.leafandkettle.com/teas/mint.avif",
]);
expect(hints).toEqual([
{ rel: "preconnect", href: "https://fonts.googleapis.com" },
{ rel: "preconnect", href: "https://fonts.gstatic.com" },
{ rel: "preconnect", href: "https://cdn.leafandkettle.com" },
]);
const hints = preconnectHints("https://www.leafandkettle.com/menu?tea=green", [
"/js/menu.js",
"reviews.json",
"https://www.leafandkettle.com/img/logo.svg",
"//www.leafandkettle.com/css/cart.css",
]);
expect(hints).toEqual([]);
const hints = preconnectHints("https://www.leafandkettle.com/", [
"https://www.leafandkettle.com:8443/api/stock",
"https://leafandkettle.com/logo.svg",
"https://static.leafandkettle.com/menu.css",
]);
expect(hints.map((hint) => hint.href)).toEqual(["https://www.leafandkettle.com:8443", "https://leafandkettle.com", "https://static.leafandkettle.com"]);
const hints = preconnectHints("https://www.leafandkettle.com/", [
"data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg'/%3E",
"https://cdn.leafandkettle.com/teas/green.avif",
]);
expect(hints).toEqual([{ rel: "preconnect", href: "https://cdn.leafandkettle.com" }]);
const urls = ["fonts.googleapis.com", "fonts.gstatic.com", "cdn.leafandkettle.com", "api.leafandkettle.com", "maps.example.com", "chat.example.com"].map((host) => "https://" + host + "/file");
expect(preconnectHints("https://www.leafandkettle.com/", urls).map((hint) => hint.href)).toEqual([
"https://fonts.googleapis.com",
"https://fonts.gstatic.com",
"https://cdn.leafandkettle.com",
"https://api.leafandkettle.com",
]);
expect(preconnectHints("https://www.leafandkettle.com/", urls, { max: 2 }).map((hint) => hint.href)).toEqual(["https://fonts.googleapis.com", "https://fonts.gstatic.com"]);
expect(preconnectHints("https://www.leafandkettle.com/", urls, { max: 0 })).toEqual([]);
const hints = preconnectHints(
"https://www.leafandkettle.com/menu",
[
"https://fonts.googleapis.com/css2?family=Lora",
"https://fonts.gstatic.com/s/lora/v32/lora.woff2",
"https://api.leafandkettle.com/v1/stock?tea=green",
"https://cdn.leafandkettle.com/teas/green.avif",
],
{ cors: ["https://fonts.gstatic.com/", "https://api.leafandkettle.com/v1/"] },
);
expect(hints).toEqual([
{ rel: "preconnect", href: "https://fonts.googleapis.com" },
{ rel: "preconnect", href: "https://fonts.gstatic.com", crossorigin: "" },
{ rel: "preconnect", href: "https://api.leafandkettle.com", crossorigin: "" },
{ rel: "preconnect", href: "https://cdn.leafandkettle.com" },
]);
expect("crossorigin" in hints[0]).toBe(false);
expect(preconnectHints("https://www.leafandkettle.com/", [])).toEqual([]);
expect(preconnectHints("https://www.leafandkettle.com/", ["/js/menu.js", "/js/menu.js"])).toEqual([]);
Run tests runs every test on your code, here in your browser. The problem is solved when they all pass.
Ctrl + Enter runs the tests (⌘ + Enter on a Mac).
I'm here to help with Choose origins to preconnect. I give hints, never the solution, so you get to solve it yourself. Ask for a hint, select code to ask about it, or press Ask Tuto why on a failing test.
Your first 3 answers need no account. for free to keep asking.