Vanta Logo
SPONSOR
Automate SOC 2 & ISO 27001 compliance with Vanta. Get $1,000 off.
Published
4 min read
Up to date

Trevor I. Lasn

Staff Software Engineer, Engineering Manager

Speed Up Your Website With rel='preconnect' and increase PageSpeed Insights Score

Using link rel='preconnect' can improve your website's performance by reducing connection setup times to key external domains.

Improving website performance often comes down to making things faster for the user. One simple yet effective technique to do this is using preconnect. Preconnect allows your browser to establish a connection to external domains before they’re needed, reducing wait times for key resources.

What is preconnect?

When your website loads resources from external domains—whether it’s for images, analytics, or embedding third-party content—the browser has to establish a connection with that domain. This involves multiple steps like DNS lookups, establishing a secure connection, and other network handshakes. These steps take time, especially on slower networks.

By using <link rel="preconnect">, you can tell the browser to start setting up these connections early, before they’re needed. This way, when the actual resource is requested (like an image or script), the connection is already in place.

Here’s an example where I use preconnect for three key services:

<link rel="preconnect" href="https://trevorlasn.substack.com/embed" />
<link rel="preconnect" href="https://res.cloudinary.com" />
<link rel="preconnect" href="https://www.googletagmanager.com" />
  • https://trevorlasn.substack.com/embed Preconnect to load my Substack newsletter embed faster.
  • https://res.cloudinary.com Preconnect to speed up loading images hosted on Cloudinary.
  • https://www.googletagmanager.com Preconnect to ensure analytics scripts load quickly.

By adding these preconnect tags, the browser will start the connection process to these domains early, improving page performance when the content from these domains is required.

Why preconnect Helps

Without preconnect, the browser waits until the resource (like an image or script) is requested before establishing the connection. This can add several hundred milliseconds of delay—time spent waiting for DNS lookups, SSL handshakes, and other connection steps.

With preconnect, you’re telling the browser, “Hey, I’m going to need this domain soon, so go ahead and start the connection now.” When the browser later requests the actual resource, the connection is already in place, and the resource loads almost immediately.

With preconnect, here’s what happens:

  1. The browser sets up the connection to Substack, Cloudinary, and Google Tag Manager in advance.
  2. When the page tries to load your newsletter embed, the images, or the tracking script, the connection is already established.
  3. The result? Faster load times, reduced latency, and a smoother user experience.

preconnect vs. preload

You might wonder how preconnect is different from preload. Preload is used when you know exactly which resource you’ll need and you want to start downloading it right away. Preconnect, on the other hand, is about setting up the connection early without fetching the resource itself immediately.

Use preconnect when you know which domain your resources will come from but don’t know exactly which resources will be requested (like with third-party APIs or embeds).

Example


<!-- Preconnect to the domain -->
<link rel="preconnect" href="https://res.cloudinary.com" />
<!-- Preload a specific resource -->
<link rel="preload" href="https://res.cloudinary.com/images/sample.jpg" as="image" />

Preconnect saves you time on the connection setup, while preload fetches the resource itself as early as possible. Both can be used together in certain cases, but for establishing connections ahead of time, preconnect is the way to go.

Browser Support

Preconnect is supported in most modern browsers, so you can use it confidently:

  • Chrome: 61+
  • Edge: 79+
  • Firefox: 58+
  • Safari: 11+

It’s a widely supported feature, meaning you can add it to your pages without worrying about compatibility issues for most users.

Best Practices for Preconnect

  • Use Preconnect for Critical Domains: Focus on domains that have a big impact on your page load, like those hosting fonts, media, or analytics. Preconnect can speed up these connections significantly.

  • Avoid Overusing Preconnect: Each preconnect takes a small amount of CPU and bandwidth. If the connection isn’t used within 10 seconds, the browser closes it, so don’t overuse preconnect tags for domains that aren’t critical to the page load.

  • Combine with DNS Prefetch: For older browsers that don’t support preconnect, consider adding <link rel="dns-prefetch"> as a fallback to at least handle the DNS lookup early.

Example


<link rel="preconnect" href="https://res.cloudinary.com" />
<link rel="dns-prefetch" href="https://res.cloudinary.com" />

This way, if the browser doesn’t support preconnect, it will at least prefetch the DNS information, improving load time.

If you found this article helpful, you might enjoy my free newsletter. I share developer tips and insights to help you grow your skills and career.


More Articles You Might Enjoy

If you enjoyed this article, you might find these related pieces interesting as well. If you like what I have to say, please check out the sponsors who are supporting me. Much appreciated!

Webdev
3 min read

CVE-2025-29927 - Next.js Middleware Bypass Explained In Simple Terms

The vulnerability skips Next.js middleware security checks by adding a single HTTP header

Apr 6, 2025
Read article
Webdev
3 min read

CSS Supports Nesting Now

CSS nesting is finally supported in all major browsers. Write cleaner, organized stylesheets without Sass or Less

Dec 6, 2024
Read article
Webdev
4 min read

Programming Trends to Watch in 2020 and Beyond

Here are my bets on the programming trends

Jul 19, 2019
Read article
Webdev
4 min read

Explicit is better than implicit

Clarity is key: being explicit makes your code more readable and maintainable.

Sep 4, 2024
Read article
Webdev
7 min read

How to Land Your First Tech Job

A developer's guide to tech interviews - from someone who sits on both sides of the table

Oct 24, 2024
Read article
Webdev
4 min read

Next.js — React Server-Side Rendering Done Right

We’re officially in the age of server-side rendered react apps

Nov 2, 2017
Read article
Webdev
7 min read

What Does an Entry-Level Programmer Need to Know Exactly?

Expectations for entry-level programmers

Nov 6, 2019
Read article
Webdev
5 min read

WebAssembly (Wasm): When (and When Not) to Use It

Understanding the real use cases for WebAssembly beyond the performance hype

Nov 25, 2024
Read article
Webdev
6 min read

Inside the CSS Engine: CSSOM Explained

A deep dive into how browsers parse and manipulate CSS, its impact on web performance, and why it matters

Oct 25, 2024
Read article

Become a better engineer

Here are engineering resources I've personally vetted and use. They focus on skills you'll actually need to build and scale real projects - the kind of experience that gets you hired or promoted.

Many companies have a fixed annual stipend per engineer (e.g. $2,000) for use towards learning resources. If your company offers this stipend, you can forward them your invoices directly for reimbursement. By using my affiliate links, you support my work and get a discount at the same!


This article was originally published on https://www.trevorlasn.com/blog/preconnect-to-required-origins. It was written by a human and polished using grammar tools for clarity.