Skip to Content
LoadingResource Hints

Resource hints

Chrome 54+ Edge 17+ Firefox 45+ Safari 16.4+ Details

Analyzes resource hints on the page, checking for proper usage and identifying optimization opportunities. Resource hints help the browser prioritize resource loading for better performance.

Resource hint types

HintPurposeWhen to UsePriority
preloadLoad resource for current pageCritical CSS, fonts, hero images, LCPHighest
modulepreloadPreload ES modulesCritical JavaScript modulesHigh
preconnectEstablish early connectionThird-party origins you’ll fetch fromHigh
dns-prefetchResolve DNS earlyThird-party origins (fallback for preconnect)Medium
prefetchLoad resource for next navigationResources needed on likely next pageLow
prerenderPre-render entire pageHigh-probability next navigationLowest

Loading priority

preload/modulepreload → preconnect → dns-prefetch → prefetch → prerender (current page) (future navigation)

Common mistakes

MistakeImpactSolution
Preload without asBrowser can’t prioritizeAdd as="script", as="style", etc.
Font preload without crossoriginDouble fetchAlways add crossorigin for fonts
Too many preloads (> 5-6)Bandwidth competitionPrioritize only critical resources
Unused preloadsWasted bandwidthRemove or verify usage
Preconnect without usageWasted connectionOnly preconnect to used origins

Tip: Preload is for resources needed in the current page. Prefetch is for resources needed in future navigations. Don’t confuse them!

A server can send the same hints in the Link response header of the document instead of the HTML, for example Link: <https://cdn.example>; rel=preconnect. The browser acts on them before it parses the HTML, and a server or CDN can also send them as a 103 Early Hints response, while the page is still being generated.

A page cannot read the headers of its own navigation, so the snippet asks for the document again with a HEAD request and reads its Link header. Each hint says where it was declared (source: dom or header), and a hint declared in both places, or twice in one, is reported once as a duplicate. When the HEAD request fails (some servers answer it with 405), the report covers only the hints of the HTML and says so.

details.earlyHints says whether the server sent a 103 Early Hints response. The browser does not expose which hints that response carried; when it is built from the Link header of the document, as a CDN usually does, they are the hints with source: "header".

One connection per credentials mode

A preconnect opens a connection for one kind of request. Without crossorigin, it serves requests that send credentials such as cookies: images, stylesheets and classic scripts. With crossorigin, it serves requests that do not: fonts, module scripts, scripts with crossorigin and, by default, fetch(). A request of the other kind opens a connection of its own, so an origin that serves both kinds needs both preconnects:

<link rel="preconnect" href="https://cdn.example"> <link rel="preconnect" href="https://cdn.example" crossorigin>

Resource Timing does not say which kind each request was, so the snippet reads it from the element that made it: the crossorigin attribute, type="module", a font in a stylesheet. A request made with fetch() or XMLHttpRequest can be either, so a preconnect whose origin only gets requests like those is not judged: its use is unknown.

When a preconnect does not save the connection

A preconnect only pays off if the request it is for finds the connection still open. Chromium closes an unused preconnected connection after about 60 seconds, and a server or CDN can close one that carries no request much sooner. When that happens, the request opens a connection of its own: the preconnect spent a DNS lookup, a TCP and a TLS handshake early in the load, when the network is busiest, and saved nothing.

Take a page that shows ads and declares both preconnects to its ad server, because the tag script and the ad requests use different credentials modes:

<link rel="preconnect" href="https://ads.example"> <link rel="preconnect" href="https://ads.example" crossorigin> <script async src="https://ads.example/tag.js" crossorigin="anonymous"></script>
  • The tag script has crossorigin, so it uses the second connection at the start of the load. That preconnect is used.
  • The first request with credentials is the ad request, which the tag makes once the user accepts consent and the ad slot is ready: 12 seconds after the start on a slow phone. The connection of the first preconnect has been open for 12 seconds without a request, and the server has closed it. The ad request opens a new connection.

So the first preconnect costs a connection early and gives nothing back. Remove it, or add it when the page knows it is about to make the ad request.

The snippet shows this from Resource Timing. For each preconnect, firstUseConnection says what the first request of its credentials mode found: reused when it used an open connection, new when it opened its own, cache when it came from the HTTP cache, and unknown when the response has no Timing-Allow-Origin header, without which the browser hides the connection phases of a cross-origin request. A new connection gets a preconnect-not-reused warning.

In the example, the ad request is a fetch() with credentials. The snippet cannot tell the credentials mode of a fetch(), so the use of that preconnect is unknown and the snippet says nothing about its connection. Check it in the Network panel of DevTools: right-click the header of the table, add the Connection ID column, and compare the ID of the ad request with the IDs of the earlier requests to the same origin. A new ID on the first request with credentials means it opened its own connection.

Replay each scenario to see which connection every request takes, and what the Network panel and the snippet show:

Switch between scenarios to compare a preconnect used early, one the server closes before its request, one without crossorigin, and one added when the request is coming. Open demo in a new tab ↗

Why preconnect helps

preconnect pays off when a resource lives on a third-party origin: it moves the connection setup (DNS, TCP, TLS) off the critical path, so the request can start as soon as the resource is discovered. Step through the scenarios to compare connection reuse across HTTP versions and see what preconnect saves:

Switch between HTTP/1.1, keep-alive, HTTP/2, and HTTP/2 + preconnect to compare how each affects the request waterfall. Open demo in a new tab ↗

Snippet

Run it from an AI agent: the prompt uses the CLI to measure any URL and report the result.

// Resource Hints Analysis
// https://webperf-snippets.net

(async () => {
  // @shared isOwnRequest
  // True for the Resource Timing entry of a request a snippet made with ownRequestUrl: the page
  // did not make it, so no snippet counts it.
  function isOwnRequest(entry) {
    return entry.name.endsWith("#webperf-snippets");
  }
  // @end-shared isOwnRequest

  // @shared ownRequestUrl
  // The URL a snippet requests when it reads a file of the page itself (fetch, a HEAD request,
  // new Image()). The fragment never reaches the server or the HTTP cache key, so the file still
  // comes from the cache, but it stays in the name of the Resource Timing entry of the request,
  // which is how isOwnRequest leaves these requests out of what the page loaded.
  function ownRequestUrl(url) {
    try {
      const parsed = new URL(url, location.href);
      if (!/^https?:$/.test(parsed.protocol)) return url;
      parsed.hash = "webperf-snippets";
      return parsed.href;
    } catch {
      return url;
    }
  }
  // @end-shared ownRequestUrl

  // @shared parseLinkHeader
  // The hints of a Link header: `<url>; rel=preconnect; crossorigin, </font.woff2>; rel="preload"; as=font`.
  // A comma or a semicolon inside <...> or quotes does not split, a rel with several tokens gives one
  // hint per token, and URLs resolve against the page. crossorigin is read as the crossOrigin property
  // of a <link> reads it: "" when missing, "use-credentials", or "anonymous" for any other value.
  function parseLinkHeader(value) {
    const parts = [];
    let current = "";
    let inUrl = false;
    let inQuote = false;
    for (const ch of value || "") {
      if (ch === "<" && !inQuote) inUrl = true;
      else if (ch === ">" && !inQuote) inUrl = false;
      else if (ch === '"' && !inUrl) inQuote = !inQuote;
      if (ch === "," && !inUrl && !inQuote) {
        parts.push(current);
        current = "";
      } else current += ch;
    }
    parts.push(current);

    const hints = [];
    for (const part of parts) {
      const match = part.match(/^\s*<([^>]*)>([\s\S]*)$/);
      if (!match) continue;
      const params = {};
      for (const param of match[2].split(/;(?=(?:[^"]*"[^"]*")*[^"]*$)/)) {
        const [rawKey, ...rest] = param.split("=");
        const key = rawKey.trim().toLowerCase();
        if (key && !(key in params)) params[key] = rest.join("=").trim().replace(/^"([\s\S]*)"$/, "$1");
      }
      let href;
      try {
        href = new URL(match[1].trim(), location.href).href;
      } catch {
        continue;
      }
      const crossorigin = !("crossorigin" in params)
        ? ""
        : params.crossorigin.toLowerCase() === "use-credentials" ? "use-credentials" : "anonymous";
      for (const rel of (params.rel || "").toLowerCase().split(/\s+/).filter(Boolean)) {
        hints.push({ rel, href, as: params.as || "", crossorigin, fetchpriority: params.fetchpriority || "", type: params.type || "", media: params.media || "" });
      }
    }
    return hints;
  }
  // @end-shared parseLinkHeader

  // @shared readHeaderHints
  // The hints of the Link header of the document. The page cannot read the headers of its own
  // navigation, so it asks for them again with a HEAD request; null when that request fails.
  async function readHeaderHints() {
    try {
      const response = await fetch(ownRequestUrl(location.href), { method: "HEAD", credentials: "include" });
      if (!response.ok) return null;
      return parseLinkHeader(response.headers.get("link"));
    } catch {
      return null;
    }
  }
  // @end-shared readHeaderHints

  // @shared formatBytes
  function formatBytes(bytes) {
    if (bytes === null || bytes === undefined || Number.isNaN(bytes)) return "-";
    if (bytes === 0) return "0 B";
    const units = ["B", "KB", "MB", "GB"];
    const i = Math.max(0, Math.min(Math.floor(Math.log(bytes) / Math.log(1024)), units.length - 1));
    return (bytes / Math.pow(1024, i)).toFixed(1) + " " + units[i];
  }
  // @end-shared formatBytes

  // How many preconnects a page should have: each one is a connection that competes for the network
  // at the start of the load, so a handful for the earliest third-party origins is the usual advice
  const PRECONNECT_BUDGET = 4;

  // Resource hint definitions
  const hintTypes = [
    {
      rel: "preload",
      icon: "⚡",
      description: "Critical resource for current page",
      requiresAs: true,
    },
    {
      rel: "modulepreload",
      icon: "📦",
      description: "ES module for current page",
      requiresAs: false,
    },
    {
      rel: "preconnect",
      icon: "🔌",
      description: "Early connection to origin",
      requiresAs: false,
    },
    {
      rel: "dns-prefetch",
      icon: "🔍",
      description: "DNS lookup for origin",
      requiresAs: false,
    },
    {
      rel: "prefetch",
      icon: "📥",
      description: "Resource for future navigation",
      requiresAs: false,
    },
    {
      rel: "prerender",
      icon: "🖼️",
      description: "Pre-render page",
      requiresAs: false,
    },
  ];

  // The hints of the Link header come first, as the browser gets them before the HTML
  const headerHints = await readHeaderHints();
  const declaredHints = (headerHints || [])
    .filter((h) => hintTypes.some((t) => t.rel === h.rel))
    .map((h) => ({ ...h, element: null, source: "header" }));
  hintTypes.forEach((type) => {
    document.querySelectorAll(`link[rel="${type.rel}"]`).forEach((link) => {
      declaredHints.push({
        rel: type.rel,
        href: link.href || link.getAttribute("href") || "",
        as: link.getAttribute("as") || "",
        crossorigin: link.crossOrigin || link.getAttribute("crossorigin") || "",
        fetchpriority: link.fetchPriority || link.getAttribute("fetchpriority") || "",
        type: link.type || "",
        media: link.media || "",
        element: link,
        source: "dom",
      });
    });
  });

  // Two hints are the same when the browser would do the same work for both: the same connection
  // for a preconnect (origin and crossorigin), the same lookup for a dns-prefetch, the same fetch otherwise
  const hintKey = (h) => {
    let origin = h.href;
    try {
      origin = new URL(h.href).origin;
    } catch {}
    if (h.rel === "dns-prefetch") return `${h.rel} ${origin}`;
    if (h.rel === "preconnect") return `${h.rel} ${origin} ${h.crossorigin}`;
    return `${h.rel} ${h.href} ${h.as} ${h.crossorigin}`;
  };
  const sourceLabel = { dom: "the HTML", header: "the Link header" };

  const allHints = [];
  const issues = [];
  const firstByKey = new Map();
  // Hints with a URL that is not http or https, such as those a browser extension injects
  // (chrome-extension://), are not the page's
  let ignoredHints = 0;

  if (headerHints === null) {
    issues.push({
      hint: { rel: "Link header", href: "" },
      code: "header-hints-unavailable",
      issue: "Could not read the Link header of the document (the HEAD request failed)",
      severity: "info",
      fix: "Hints sent only in the Link header or as 103 Early Hints are not in this report",
    });
  }

  declaredHints.forEach((declared) => {
    const type = hintTypes.find((t) => t.rel === declared.rel);
    const href = declared.href;
    let protocol = "";
    try {
      protocol = new URL(href, location.href).protocol;
    } catch {}
    if (protocol !== "http:" && protocol !== "https:") {
      ignoredHints++;
      return;
    }
    let hostname = "";
    try {
      hostname = new URL(href, location.origin).hostname;
    } catch {}

    const hint = {
      ...declared,
      icon: type.icon,
      shortHref: href.length > 50 ? "..." + href.slice(-47) : href,
      hostname,
    };

    allHints.push(hint);

    const first = firstByKey.get(hintKey(hint));
    if (first) {
      issues.push({
        hint,
        code: "duplicate-hint",
        issue: first.source === hint.source
          ? `Declared twice in ${sourceLabel[hint.source]}`
          : "Declared both in the Link header and in the HTML",
        severity: "info",
        fix: "Keep one of them",
      });
    } else {
      firstByKey.set(hintKey(hint), hint);
    }

    // Check for issues
    if (type.requiresAs && !hint.as) {
      issues.push({
        hint,
        code: "preload-missing-as",
        issue: "Missing 'as' attribute (required for preload)",
        severity: "error",
        fix: `Add as="script|style|font|image|fetch"`,
      });
    }

    if (hint.as === "font" && !hint.crossorigin) {
      issues.push({
        hint,
        code: "font-preload-no-crossorigin",
        issue: "Font preload without crossorigin (causes double fetch)",
        severity: "error",
        fix: 'Add crossorigin="anonymous"',
      });
    }
  });

  // Check for unused preloads
  const preloads = allHints.filter((h) => h.rel === "preload");
  const loadedResources = performance.getEntriesByType("resource").filter((e) => !isOwnRequest(e));

  preloads.forEach((preload) => {
    const wasUsed = loadedResources.some((r) => r.name === preload.href);
    if (!wasUsed) {
      issues.push({
        hint: preload,
        code: "unused-preload",
        issue: "Preloaded resource not used within page load",
        severity: "warning",
        fix: "Remove unused preload or verify it's needed",
      });
    }
  });

  // Preconnects and dns-prefetches, for the origins that need no other hint
  const preconnects = allHints.filter(
    (h) => h.rel === "preconnect" || h.rel === "dns-prefetch"
  );

  const originOf = (url) => {
    try {
      return new URL(url, location.href).origin;
    } catch {
      return "";
    }
  };
  const withoutHash = (url) => {
    try {
      const parsed = new URL(url, location.href);
      parsed.hash = "";
      return parsed.href;
    } catch {
      return url;
    }
  };

  // The credentials mode of a request decides which connection it can use: a preconnect without
  // crossorigin opens one for requests that send credentials ("include"), such as images, stylesheets
  // and classic scripts; one with crossorigin opens one for requests that do not ("anonymous"), such
  // as fonts, module scripts and scripts with crossorigin. Resource Timing does not report the mode,
  // so it is read from the element that made the request. It is "unknown" when nothing says it: a
  // fetch() or an XMLHttpRequest can go either way, and a script no longer in the DOM can be either.
  const modeOfCrossorigin = (crossorigin) => (crossorigin && crossorigin !== "use-credentials" ? "anonymous" : "include");
  const elementsByUrl = (selector, urlOf) => {
    const map = new Map();
    document.querySelectorAll(selector).forEach((el) => {
      const url = urlOf(el);
      if (url && !map.has(withoutHash(url))) map.set(withoutHash(url), el);
    });
    return map;
  };
  const scriptsByUrl = elementsByUrl("script[src]", (el) => el.src);
  const linksByUrl = elementsByUrl("link[href]", (el) => el.href);
  const imagesByUrl = elementsByUrl("img", (el) => el.currentSrc || el.src);
  const headerHintsByUrl = new Map(allHints.filter((h) => h.source === "header").map((h) => [withoutHash(h.href), h]));
  const FONT_FILE = /\.(woff2?|ttf|otf|eot)(\?|#|$)/i;

  function credentialsMode(entry) {
    const url = withoutHash(entry.name);
    switch (entry.initiatorType) {
      case "script": {
        const script = scriptsByUrl.get(url);
        if (!script) return "unknown";
        // A module script is fetched with CORS and sends no credentials to another origin
        if (script.type === "module") return script.crossOrigin === "use-credentials" ? "include" : "anonymous";
        return modeOfCrossorigin(script.crossOrigin);
      }
      case "link":
      case "early-hints": {
        const link = linksByUrl.get(url) || headerHintsByUrl.get(url);
        if (!link) return "unknown";
        const crossorigin = link.crossOrigin ?? link.crossorigin;
        if (link.rel === "modulepreload") return crossorigin === "use-credentials" ? "include" : "anonymous";
        return modeOfCrossorigin(crossorigin);
      }
      case "img": {
        // An image sends credentials unless it has crossorigin; one that is not in the DOM, such as a
        // tracking pixel made with new Image(), almost never has it
        const img = imagesByUrl.get(url);
        return modeOfCrossorigin(img ? img.crossOrigin : "");
      }
      case "css":
        // A font of a stylesheet is always fetched with CORS; its images and imports send credentials
        return FONT_FILE.test(url) ? "anonymous" : "include";
      default:
        return "unknown";
    }
  }

  // Whether a request opened a connection of its own ("new"), found one open ("reused"), came from the
  // HTTP cache ("cache") or does not say ("unknown"): a cross-origin response without
  // Timing-Allow-Origin reports its connection phases as 0
  const connectionOf = (r) => {
    if (r.requestStart === 0) return "unknown";
    if (r.transferSize === 0 && r.decodedBodySize > 0) return "cache";
    return r.connectEnd > r.connectStart ? "new" : "reused";
  };

  // The requests to each origin, in the order they started, with their credentials mode
  const requestsByOrigin = new Map();
  const requestsByCredentials = { include: 0, anonymous: 0, unknown: 0 };
  loadedResources.forEach((r) => {
    const origin = originOf(r.name);
    if (!origin) return;
    const mode = credentialsMode(r);
    if (origin !== location.origin) requestsByCredentials[mode]++;
    if (!requestsByOrigin.has(origin)) requestsByOrigin.set(origin, []);
    requestsByOrigin.get(origin).push({ url: r.name, startTime: r.startTime, mode, connection: connectionOf(r) });
  });
  requestsByOrigin.forEach((requests) => requests.sort((a, b) => a.startTime - b.startTime));

  // Each preconnect is used by the first request of its mode to its origin. Without one, it is
  // "unknown" when a request of unknown mode went to the origin, and "unused" otherwise.
  const modeOfHint = (h) => modeOfCrossorigin(h.crossorigin);
  const preconnectsByOrigin = new Map();
  allHints.filter((h) => h.rel === "preconnect").forEach((h) => {
    const origin = originOf(h.href);
    const requests = requestsByOrigin.get(origin) || [];
    const first = requests.find((r) => r.mode === modeOfHint(h));
    h.use = first ? "used" : requests.some((r) => r.mode === "unknown") ? "unknown" : "unused";
    h.firstUse = first || null;
    if (!preconnectsByOrigin.has(origin)) preconnectsByOrigin.set(origin, []);
    preconnectsByOrigin.get(origin).push(h);
  });

  const MODE_WORDS = {
    anonymous: "without credentials (fonts, module scripts, scripts with crossorigin)",
    include: "with credentials (images, stylesheets, scripts without crossorigin)",
  };
  preconnectsByOrigin.forEach((hints, origin) => {
    const requests = requestsByOrigin.get(origin) || [];
    const explained = new Set();
    // Requests of one mode with no preconnect of that mode: one issue for the origin
    [
      { mode: "anonymous", code: "preconnect-no-crossorigin", add: `<link rel="preconnect" href="${origin}" crossorigin>`, change: "Add crossorigin to this preconnect" },
      { mode: "include", code: "preconnect-crossorigin-only", add: `<link rel="preconnect" href="${origin}">`, change: "Remove crossorigin from this preconnect" },
    ].forEach(({ mode, code, add, change }) => {
      if (!requests.some((r) => r.mode === mode) || hints.some((h) => modeOfHint(h) === mode)) return;
      const hint = hints.find((h) => h.use === "unused") || hints[0];
      hints.filter((h) => h.use === "unused").forEach((h) => explained.add(h));
      issues.push({
        hint,
        code,
        issue: `Requests ${MODE_WORDS[mode]} to ${origin} cannot use a preconnect ${mode === "anonymous" ? "without" : "with"} crossorigin`,
        severity: "warning",
        fix: hint.use === "unused" ? change : `Add a second preconnect: ${add}`,
      });
    });
    // The first request of its mode opened a connection of its own: the preconnected one was closed
    // before it (by the server, or by the browser after its idle time) or was not ready
    hints.forEach((h) => {
      if (h.firstUse?.connection !== "new" || firstByKey.get(hintKey(h)) !== h) return;
      issues.push({
        hint: h,
        code: "preconnect-not-reused",
        issue: `The first request ${MODE_WORDS[modeOfHint(h)].split(" (")[0]} to ${origin} started at ${Math.round(h.firstUse.startTime)} ms and opened a new connection, so the preconnected one was not used`,
        severity: "warning",
        fix: "A server or CDN can close a connection that carries no request, and Chrome closes an unused one after about 60 s: preconnect only to origins the page requests early, or remove the hint",
      });
    });
    hints.forEach((h) => {
      if (h.use !== "unused" || explained.has(h) || firstByKey.get(hintKey(h)) !== h) return;
      issues.push({
        hint: h,
        code: "unused-preconnect",
        issue: requests.length === 0
          ? `No resources loaded from ${origin}`
          : `No request ${modeOfHint(h) === "anonymous" ? "without" : "with"} credentials to ${origin}; its requests use another connection`,
        severity: "warning",
        fix: "Remove unused preconnect or verify it's needed for future resources",
      });
    });
  });

  // A dns-prefetch only resolves the host name, which any request to it uses
  allHints.filter((h) => h.rel === "dns-prefetch").forEach((pc) => {
    const hasResources = loadedResources.some((r) => {
      try {
        return new URL(r.name).hostname === pc.hostname;
      } catch {
        return false;
      }
    });
    if (!hasResources && pc.hostname && firstByKey.get(hintKey(pc)) === pc) {
      issues.push({
        hint: pc,
        code: "unused-preconnect",
        issue: `No resources loaded from ${pc.hostname}`,
        severity: "warning",
        fix: "Remove unused preconnect or verify it's needed for future resources",
      });
    }
  });

  // Check for too many preloads
  if (preloads.length > 6) {
    issues.push({
      hint: { rel: "preload", href: `${preloads.length} preloads` },
      code: "too-many-preloads",
      issue: `Too many preloads (${preloads.length}) - they compete for bandwidth`,
      severity: "warning",
      fix: "Limit to 5-6 most critical resources",
    });
  }

  // Get third-party origins that could benefit from preconnect
  const thirdPartyOrigins = new Map();
  // Third-party resources without Timing-Allow-Origin report every size as 0
  let sizeUnknownCount = 0;
  loadedResources.forEach((r) => {
    try {
      const url = new URL(r.name);
      if (url.hostname !== location.hostname) {
        if (!thirdPartyOrigins.has(url.origin)) {
          thirdPartyOrigins.set(url.origin, { count: 0, size: null, types: new Set(), firstStart: Infinity, modes: new Set() });
        }
        const data = thirdPartyOrigins.get(url.origin);
        data.count++;
        data.firstStart = Math.min(data.firstStart, r.startTime);
        const mode = credentialsMode(r);
        if (mode !== "unknown") data.modes.add(mode);
        // The size stays null while no resource of the origin has a known one
        if (r.transferSize === 0 && r.encodedBodySize === 0 && r.decodedBodySize === 0 && r.duration > 0) sizeUnknownCount++;
        else data.size = (data.size ?? 0) + (r.transferSize || 0);
        data.types.add(r.initiatorType);
      }
    } catch {}
  });

  // Find origins without preconnect
  const preconnectedOrigins = new Set(
    [...preconnects].map((p) => {
      try {
        return new URL(p.href).origin;
      } catch {
        return "";
      }
    })
  );

  // A preconnect helps an origin the page needs to render: one whose first request starts before
  // the LCP (the FCP when there is no LCP; every origin when there is neither). The earliest ones get
  // the preconnects left in the budget, and the rest of the early ones a dns-prefetch.
  const lcpEntry = PerformanceObserver.supportedEntryTypes?.includes("largest-contentful-paint")
    ? await new Promise((resolve) => {
        const entries = [];
        const observer = new PerformanceObserver((list) => entries.push(...list.getEntries()));
        observer.observe({ type: "largest-contentful-paint", buffered: true });
        setTimeout(() => {
          observer.disconnect();
          resolve(entries.at(-1) ?? null);
        }, 100);
      })
    : null;
  const fcpEntry = performance.getEntriesByType("paint").find((e) => e.name === "first-contentful-paint");
  const cutoff = lcpEntry
    ? { metric: "lcp", ms: Math.round(lcpEntry.startTime) }
    : fcpEntry
      ? { metric: "fcp", ms: Math.round(fcpEntry.startTime) }
      : { metric: null, ms: null };

  // The budget counts connections: one per origin and credentials mode, as the browser opens them
  const preconnectBudget = {
    max: PRECONNECT_BUDGET,
    used: new Set(allHints.filter((h) => h.rel === "preconnect").map((h) => `${originOf(h.href)} ${modeOfHint(h)}`)).size,
  };
  const earlyOrigins = [];
  thirdPartyOrigins.forEach((data, origin) => {
    if (preconnectedOrigins.has(origin)) return;
    if (cutoff.ms !== null && data.firstStart >= cutoff.ms) return;
    earlyOrigins.push({ origin, ...data });
  });
  earlyOrigins.sort((a, b) => a.firstStart - b.firstStart);
  // The crossorigin of each preconnect an origin needs, one per credentials mode of its requests. An
  // origin whose requests do not say it (fetch, XMLHttpRequest) gets crossorigin, as a fetch() sends
  // no credentials to another origin by default.
  earlyOrigins.forEach((o) => {
    o.crossorigin = o.modes.size ? [...o.modes].map((m) => (m === "anonymous" ? "anonymous" : "")).sort() : ["anonymous"];
  });
  // The earliest origins while their preconnects fit in the budget; the next ones get a dns-prefetch
  let left = Math.max(0, preconnectBudget.max - preconnectBudget.used);
  const fitting = earlyOrigins.findIndex((o) => (left -= o.crossorigin.length) < 0);
  const missingPreconnects = fitting === -1 ? earlyOrigins : earlyOrigins.slice(0, fitting);
  const dnsPrefetchCandidates = earlyOrigins.slice(missingPreconnects.length);
  const preconnectTags = (o) =>
    o.crossorigin.map((c) => `<link rel="preconnect" href="${o.origin}"${c ? " crossorigin" : ""}>`);

  // Display results
  console.group("%c🔗 Resource Hints Analysis", "font-weight: bold; font-size: 14px;");

  // Summary
  console.log("");
  console.log("%cSummary:", "font-weight: bold;");

  const summary = hintTypes.map((type) => {
    const count = allHints.filter((h) => h.rel === type.rel).length;
    return { type: type.rel, count, icon: type.icon };
  });

  summary.forEach((s) => {
    const status = s.count > 0 ? "🟢" : "⚪";
    console.log(`   ${status} ${s.icon} ${s.type}: ${s.count}`);
  });

  console.log(`   Total hints: ${allHints.length}`);

  // Details by type
  hintTypes.forEach((type) => {
    const hints = allHints.filter((h) => h.rel === type.rel);
    if (hints.length === 0) return;

    console.log("");
    console.group(
      `%c${type.icon} ${type.rel} (${hints.length})`,
      "color: #3b82f6; font-weight: bold;"
    );

    const tableData = hints.map((h) => {
      const row = {
        Resource: h.shortHref,
        Source: h.source === "header" ? "Link header" : "HTML",
      };

      if (type.rel === "preload" || type.rel === "modulepreload") {
        row.As = h.as || "⚠️ missing";
        row.Crossorigin = h.crossorigin || "-";
        if (h.fetchpriority) row.Priority = h.fetchpriority;

        // Check if used
        const used = loadedResources.some((r) => r.name === h.href);
        row.Used = used ? "✅" : "⚠️ No";
      }

      if (type.rel === "preconnect" || type.rel === "dns-prefetch") {
        row.Origin = h.hostname;
        row.Crossorigin = h.crossorigin || "-";

        // Check resources from this origin
        const originResources = loadedResources.filter((r) => {
          try {
            return new URL(r.name).hostname === h.hostname;
          } catch {
            return false;
          }
        });
        row["Resources Loaded"] = originResources.length;
        if (type.rel === "preconnect") {
          row.Used = { used: "✅", unused: "⚠️ No", unknown: "? fetch/XHR" }[h.use];
          row["First use"] = h.firstUse ? `${Math.round(h.firstUse.startTime)} ms` : "-";
          row.Connection = h.firstUse ? { reused: "✅ reused", new: "⚠️ new", cache: "cache", unknown: "? no TAO" }[h.firstUse.connection] : "-";
        }
      }

      return row;
    });

    console.table(tableData);

    // Elements for inspection
    console.log("%c🔎 Elements:", "font-weight: bold;");
    hints.forEach((h, i) => {
      console.log(`${i + 1}.`, h.element ?? `${h.href} (Link header)`);
    });

    console.groupEnd();
  });

  // Issues
  const errors = issues.filter((i) => i.severity === "error");
  const warnings = issues.filter((i) => i.severity === "warning");
  const infos = issues.filter((i) => i.severity === "info");

  if (issues.length > 0) {
    console.log("");
    console.group(
      `%c⚠️ Issues Found (${errors.length} errors, ${warnings.length} warnings, ${infos.length} info)`,
      "color: #ef4444; font-weight: bold;"
    );

    if (errors.length > 0) {
      console.log("");
      console.log("%c🔴 Errors:", "font-weight: bold; color: #ef4444;");
      errors.forEach((issue) => {
        console.log(`   ${issue.hint.rel}: ${issue.issue}`);
        console.log(`      Resource: ${issue.hint.href}`);
        console.log(`      Fix: ${issue.fix}`);
      });
    }

    if (warnings.length > 0) {
      console.log("");
      console.log("%c🟡 Warnings:", "font-weight: bold; color: #f59e0b;");
      warnings.forEach((issue) => {
        console.log(`   ${issue.hint.rel}: ${issue.issue}`);
        if (issue.hint.href) {
          console.log(`      Resource: ${issue.hint.href}`);
        }
        console.log(`      Fix: ${issue.fix}`);
      });
    }

    if (infos.length > 0) {
      console.log("");
      console.log("%cℹ️ Info:", "font-weight: bold; color: #3b82f6;");
      infos.forEach((issue) => {
        console.log(`   ${issue.hint.rel}: ${issue.issue}`);
        if (issue.hint.href) {
          console.log(`      Resource: ${issue.hint.href}`);
        }
        console.log(`      Fix: ${issue.fix}`);
      });
    }

    console.groupEnd();
  } else {
    console.log("");
    console.log(
      "%c✅ No issues found with resource hints.",
      "color: #22c55e; font-weight: bold;"
    );
  }

  // Recommendations
  if (missingPreconnects.length > 0) {
    console.log("");
    console.group("%c💡 Optimization Opportunities", "color: #3b82f6; font-weight: bold;");

    console.log("");
    console.log("%cThird-party origins without preconnect:", "font-weight: bold;");
    console.log(
      cutoff.metric
        ? `Their first request starts before the ${cutoff.metric.toUpperCase()} (${cutoff.ms} ms), earliest first, within a budget of ${preconnectBudget.max} preconnects (${preconnectBudget.used} used):`
        : `Earliest first, within a budget of ${preconnectBudget.max} preconnects (${preconnectBudget.used} used):`
    );
    console.log("");

    console.table(missingPreconnects.map((o) => ({
      Origin: o.origin,
      "First request": `${Math.round(o.firstStart)} ms`,
      Resources: o.count,
      Size: formatBytes(o.size),
      Types: Array.from(o.types).join(", "),
    })));

    console.log("");
    console.log("%cAdd preconnect for these origins:", "font-weight: bold;");
    missingPreconnects.flatMap(preconnectTags).forEach((tag) => {
      console.log(`%c${tag}`, "font-family: monospace; color: #22c55e;");
    });
    if (missingPreconnects.some((o) => o.modes.size === 0)) {
      console.log("Requests made with fetch() or XMLHttpRequest do not say whether they send credentials: crossorigin fits their default, which sends none. Drop it if they send credentials.");
    }

    if (dnsPrefetchCandidates.length > 0) {
      console.log("");
      console.log("%cOver the budget, a dns-prefetch is enough:", "font-weight: bold;");
      dnsPrefetchCandidates.slice(0, 5).forEach((o) => {
        console.log(`%c<link rel="dns-prefetch" href="${o.origin}">`, "font-family: monospace; color: #22c55e;");
      });
    }

    console.groupEnd();
  }

  // Best practices
  console.log("");
  console.group("%c📝 Best Practices", "color: #3b82f6; font-weight: bold;");
  console.log("");
  console.log("%cPreload (current page critical resources):", "font-weight: bold;");
  console.log(
    '%c<link rel="preload" href="/font.woff2" as="font" type="font/woff2" crossorigin>',
    "font-family: monospace; color: #22c55e;"
  );
  console.log(
    '%c<link rel="preload" href="/hero.webp" as="image" fetchpriority="high">',
    "font-family: monospace; color: #22c55e;"
  );
  console.log("");
  console.log("%cPreconnect (third-party origins):", "font-weight: bold;");
  console.log(
    '%c<link rel="preconnect" href="https://fonts.googleapis.com">',
    "font-family: monospace; color: #22c55e;"
  );
  console.log(
    '%c<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>',
    "font-family: monospace; color: #22c55e;"
  );
  console.log("");
  console.log("%cPrefetch (next page resources):", "font-weight: bold;");
  console.log(
    '%c<link rel="prefetch" href="/next-page.js" as="script">',
    "font-family: monospace; color: #22c55e;"
  );
  console.groupEnd();

  console.groupEnd();

  const navigationEntry = performance.getEntriesByType("navigation")[0];

  // The list keeps 50 hints: those with an error or a warning first, then in document order; count and details cover all of them
  const MAX_ITEMS = 50;
  const hintRank = (h) => (issues.some((i) => i.hint === h && i.severity === "error") ? 0 : issues.some((i) => i.hint === h) ? 1 : 2);
  const rankedHints = allHints
    .map((h, index) => ({ h, index }))
    .sort((a, b) => hintRank(a.h) - hintRank(b.h) || a.index - b.index)
    .map(({ h }) => h);
  return {
    script: "Resource-Hints",
    status: "ok",
    count: allHints.length,
    corsLimitedAnalysis: sizeUnknownCount > 0,
    details: {
      sizeUnknownCount,
      ignoredHints,
      // The requests to other origins by credentials mode, inferred from the DOM; "unknown" ones
      // (fetch, XMLHttpRequest) leave the preconnects of their origin unjudged
      requestsByCredentials,
      // null when the Link header could not be read
      headerHints: headerHints === null ? null : allHints.filter((h) => h.source === "header").length,
      // Whether the server sent a 103 Early Hints response; null where the browser does not say
      earlyHints: typeof navigationEntry?.firstInterimResponseStart === "number" ? navigationEntry.firstInterimResponseStart > 0 : null,
      byType: Object.fromEntries(hintTypes.map(t => [t.rel, allHints.filter(h => h.rel === t.rel).length])),
      // The third-party origins without a hint whose first request starts before the cutoff
      missingPreconnectsCount: earlyOrigins.length,
      missingPreconnectsCutoff: cutoff,
      preconnectBudget,
      missingPreconnects: missingPreconnects.map((o) => ({
        origin: o.origin,
        firstRequestStartMs: Math.round(o.firstStart),
        requestCount: o.count,
        sizeBytes: o.size,
        resourceTypes: Array.from(o.types),
        credentialsModes: [...o.modes].sort(),
        recommendedHint: "preconnect",
        crossorigin: o.crossorigin,
      })),
      dnsPrefetchCandidates: dnsPrefetchCandidates.slice(0, 20).map((o) => ({
        origin: o.origin,
        firstRequestStartMs: Math.round(o.firstStart),
        requestCount: o.count,
      })),
    },
    items: rankedHints.slice(0, MAX_ITEMS).map(h => ({ rel: h.rel, href: h.href, source: h.source, as: h.as, crossorigin: h.crossorigin, fetchpriority: h.fetchpriority, use: h.use ?? null, firstUseUrl: h.firstUse?.url ?? null, firstUseMs: h.firstUse ? Math.round(h.firstUse.startTime) : null, firstUseConnection: h.firstUse?.connection ?? null })),
    issues: issues.map(i => ({ code: i.code, severity: i.severity, message: `${i.hint.rel}: ${i.issue}. Fix: ${i.fix}`, ...(i.hint.source ? { target: i.hint.href } : {}) })),
  };
})();

Understanding the results

Summary section

  • Count of each resource hint type
  • Green () = hints present, Grey () = no hints of this type
  • Hints whose URL is not http or https, such as those a browser extension injects (chrome-extension://), are not the page’s and are left out

Details by type

For preload/modulepreload:

ColumnDescription
ResourceURL (truncated if long)
SourceHTML or Link header
AsResource type (script, style, font, image)
CrossoriginCORS setting
Priorityfetchpriority if set
UsedWhether the resource was actually loaded

For preconnect/dns-prefetch:

ColumnDescription
ResourceURL (truncated if long)
SourceHTML or Link header
OriginDomain being connected to
CrossoriginCORS setting
Resources LoadedCount of resources from this origin

Issues detected

IssueSeverityImpact
Preload missing asErrorBrowser can’t prioritize correctly
Font preload without crossoriginErrorResource fetched twice
Requests without credentials and no crossorigin preconnectWarningFonts and modules open a connection of their own
Requests with credentials and only a crossorigin preconnectWarningImages and classic scripts open a connection of their own
Unused preloadWarningWasted bandwidth
Preconnect no request of its mode usedWarningWasted connection
First request opened a new connectionWarningThe preconnected connection was closed before it
Too many preloads (> 6)WarningBandwidth competition
Hint declared twiceInfoNothing, but one of them can go
Link header not readableInfoHints sent only in the header are not in the report

Optimization opportunities

Each preconnect opens a connection that competes for the network at the start of the load, so the snippet suggests only a few, for the origins the page needs to render:

  • Third-party origins without a hint whose first request starts before the LCP (the FCP when the page has no LCP), earliest first.
  • The <link> tags each origin needs: with crossorigin for requests without credentials (fonts, module scripts), without it for requests with credentials (images, classic scripts), or both. An origin that only gets fetch() requests gets crossorigin, the default of fetch().
  • As many as fit in a budget of 4 preconnects, counting those the page already has. An origin that needs both kinds takes two.
  • A dns-prefetch for the early origins over the budget. Origins first requested after the LCP get no suggestion: a connection opened at the start of the load would wait unused for them.

Priority guidelines

Resource TypeSuggested Hint
LCP imagepreload with fetchpriority="high"
Critical fontspreload with crossorigin
Critical CSSpreload
Third-party scriptspreconnect to origin
Below-fold imagesNone (use loading="lazy")
Next page resourcesprefetch

Browser support

The snippet needs these features to run.

FeatureChromeEdgeFirefoxSafari
PerformanceResourceTiming.transferSize (opens in a new tab) 54 17 45 16.4

The snippet reports on these features. It runs without them, and its advice on each one applies where it is supported.

FeatureChromeEdgeFirefoxSafari
<link rel=“preload”> (opens in a new tab) 50 79 85 11.1
<link rel=“preconnect”> (opens in a new tab) 46 79 39 11.1
<link rel=“dns-prefetch”> (opens in a new tab) 46 79 127 5
<link rel=“modulepreload”> (opens in a new tab) 66 79 115 17

Source: MDN browser compatibility data (opens in a new tab), version 8.1.10.

Further reading

Last updated on