On this page
A preconnect hint can look correct in your HTML and still point to the wrong place. If its scheme, hostname, or port differs from the resource request’s origin, it may prepare a connection that the request cannot use. This guide shows how to spot a preconnect hint wrong origin problem in browser developer tools and check the hint’s credential mode.
Key Takeaways
- Compare the resolved preconnect URL with the resource’s actual request URL, including its final destination after any redirects.
- An origin consists of a scheme, hostname, and port. A path is not part of the origin; default ports are normalized.
- For resources fetched with CORS, check whether the hint’s
crossoriginsetting matches the request’s credential mode. The attribute iscrossorigin="use-credentials", notcredentials="use-credentials". - Preconnect is a hint, not a guarantee. An unused or mismatched hint can consume resources without helping the request.
What origin does the browser use?
The browser resolves a preconnect link’s href as a URL relative to the document’s base URL. An absolute URL makes the intended destination easier to review:
<link rel="preconnect" href="https://static.example.test">
The connection target is the URL’s origin: its scheme, hostname, and port. For example, https://static.example.test/assets/site.css has the origin https://static.example.test. The path identifies a resource, not a separate connection origin. The hostnames in these examples are fictional.
These are different origins:
https://static.example.testandhttp://static.example.test: different schemes.https://static.example.testandhttps://cdn.example.test: different hostnames.https://static.example.testandhttps://static.example.test:8443: different ports.
An explicit default port is not a mismatch: https://static.example.test:443 and https://static.example.test have the same origin. A matching origin is necessary for the intended hint, but it does not guarantee that the browser will open or reuse a connection.
Advertisement
Reproduce a port mismatch locally
This example uses two local HTTP servers and a browser with developer tools. It requires Python 3. The commands and example have not been executed for this article; verify them in your environment.
Create a directory with these two files. In index.html, the hint points to port 8000, while the stylesheet request points to port 8001:
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<title>Preconnect origin check</title>
<link rel="preconnect" href="http://127.0.0.1:8000">
<link rel="stylesheet" href="http://127.0.0.1:8001/style.css">
</head>
<body>
<p>Check the stylesheet request in developer tools.</p>
</body>
</html>
Save this as style.css in the same directory:
body {
font-family: sans-serif;
}
From that directory, start each server in a separate terminal:
python3 -m http.server 8000 --bind 127.0.0.1
python3 -m http.server 8001 --bind 127.0.0.1
If a port is already in use, stop the conflicting process or choose different ports and update both URLs in the HTML. Open http://127.0.0.1:8000/ in the browser, then:
- Open developer tools and select Network. Enable preservation of the log and disable the cache if your browser offers those controls.
- Reload the page and select the
style.cssrequest. In its headers or details, find the request URL: it should use port8001. - In Elements or Inspector, find the
rel="preconnect"link. Itshrefuses port8000. Compare the resolved scheme, hostname, and port with the stylesheet request URL. - Change the preconnect
hrefinindex.htmltohttp://127.0.0.1:8001, save, and reload. Repeat the comparison. The origins should now match.
Stop both servers with Ctrl+C when finished. This local example demonstrates origin comparison, not a reliable performance improvement: the browser may already have connections open, or may decide not to act on the hint.
Check a real page before release
For a production page, inspect the rendered link in the Elements panel, especially if templates, JavaScript, or a document base URL can change how href resolves. Then find the relevant font, stylesheet, script, image, or API request in Network. Compare the request URL with the hint—not just the hostname visible in source code.
If the resource redirects, inspect the redirect chain and the final request. A hint to the initial origin might help the initial request but does not automatically prepare a connection to a different final origin. Check other candidates too: a CSS file and the font it loads can come from different origins.
A request’s timing details can help investigate connection setup, but they do not by themselves prove that a particular hint was used. Cache state, existing connections, browser scheduling, and connection reuse can affect the result. Repeat observations in a fresh browser context and compare with the hint removed before attributing a change to preconnect.
Advertisement
Check CORS and credential mode
The link attribute for selecting the credentialed CORS mode is:
<link rel="preconnect" href="https://assets.example.test" crossorigin="use-credentials">
Use that form only when it matches the resource fetch you intend to support. credentials="use-credentials" is not the corresponding link attribute. For a cross-origin font fetched without cross-origin credentials, a common hint is instead:
<link rel="preconnect" href="https://fonts.example.test" crossorigin>
The bare crossorigin attribute specifies the anonymous mode. Do not add use-credentials merely because a hostname is third-party: the eventual request’s mode matters, and a mismatched hint may not prepare a reusable connection. The resource request still needs an appropriate CORS configuration; a preconnect hint does not grant access or change the request’s authorization.
The MDN documentation and HTML Living Standard were not available for consultation in this run. Check their current guidance, as well as behavior in your target browsers, before relying on a particular attribute or connection-reuse detail.
Common Mistakes
- Matching only the hostname. A scheme or non-default port difference changes the origin.
- Checking only the URL in source code. Relative URLs, redirects, and runtime changes can make the actual request destination different.
- Preconnecting to a page rather than the asset origin. A third-party page, its stylesheets, and its fonts may use separate origins.
- Using the wrong attribute.
credentials="use-credentials"is not a substitute forcrossorigin="use-credentials"on the link. - Treating the hint as proof of a speedup. The browser can ignore it, and a connection can be reused for reasons unrelated to the hint.
- Adding hints for every possible provider. Unused connection setup can consume network and browser resources without benefiting the page.
Best Practices
Keep hints for origins needed early and frequently enough to justify them. Inspect actual requests for each important page or route, since resources can differ by page, user state, or deployment. Remove stale hints when an asset provider or hostname changes.
After correcting a hint, compare behavior with and without it under representative conditions. Treat developer-tools timing as diagnostic evidence, not a guarantee that every visitor will see the same result.
Conclusion
Before shipping, compare each important preconnect hint’s resolved origin and credential mode with the request it is meant to help. Fix mismatches, remove unused hints, and verify the effect in the browsers and page flows that matter to your release.
What would your domain sell for?
A free TidyURLs account unlocks the Domain Estimator. It prices a name from 191,011 real domain sales.