brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: in your HTML warms up the connection to another site early

Html
~/tips/tip-html-link-preconnect

cat ~/tips/tip-html-link-preconnect.md

Lets the browser set up the connection to a critical third-party server before it is needed.

When a page loads fonts or an API from another domain, the browser first has to look up the name, connect and do the TLS handshake. That costs time before the first byte arrives. You can tell the browser to start on that early.

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

Put it in the <head>. The browser opens the connection right away, so when the real request comes, part of the work is done.

For fonts, add crossorigin:

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

web.dev is clear about why it matters here: if you leave crossorigin off, the browser only performs the DNS lookup.

For less important origins, dns-prefetch is the cheaper hint: it only resolves the name. web.dev suggests it as a fallback too, but as a separate tag. Putting both in one rel attribute triggers a Safari bug that cancels the preconnect.

Caveats. Do not preconnect to everything. MDN says doing it for many third-party domains can be counterproductive, and web.dev warns about too many origins competing for bandwidth. A preconnected connection that is not used within about 10 seconds is closed again, so it only helps for things the page needs right away. I’d keep it to the one or two origins that block the first render.

Further reading: MDN: rel=preconnect, web.dev: preconnect and dns-prefetch, On this site: fetchpriority="high" loads the hero image first

More Html tips import { format } from "dates"; width="640" height="360" poster="demo.jpg"> The HTML optgroup element puts headings inside a long dropdown srclang="en" label="English" default> An HTML noindex meta tag keeps one page out of search results All Html tips (14) All topics

Link