All Categories Webpage Health Check Preconnect to required origins

Preconnect to required origins

By Chip Cox
July 31, 2023

Preconnecting to required origins is a technique used to tell the browser that your webpage intends to establish a connection to another domain and that the process should start as soon as possible. This helps to reduce the latency in network requests, leading to quicker resource fetching and an enhanced user experience.

Here's a step-by-step guide on how to preconnect to required origins:

1. Identify the Required Origins

First, determine the external domains your site will connect to. These could include CDNs, image servers, third-party APIs, or other resources that your site relies on.

2. Use the Preconnect Hint

You can indicate to the browser that you wish to preconnect to an origin by using the <link> element with the rel attribute set to "preconnect". Place this in the <head> section of your HTML document.

Here's an example of preconnecting to a CDN:

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

3. Optionally Include Cross-Origin Attribute

If you're preconnecting to a third-party origin that includes credentials like cookies, you'll need to include the crossorigin attribute:

<link rel="preconnect" href="https://third-party.example.com" crossorigin>

4. Limit the Number of Preconnects

Preconnecting establishes connections in advance, which can consume resources. It's wise to only preconnect to critical domains that are essential to your site's performance. Too many preconnects can overwhelm the browser and might have the opposite of the intended effect.

5. Test Performance Impact

After implementing preconnects, test the loading performance of your website using tools like Google PageSpeed Insights or WebPageTest.

6. Monitor for Changes

If the third-party resources or the structure of your website changes, the required origins might also change. Regularly review and update the preconnect hints as necessary.

Conclusion

Preconnecting to required origins is a valuable optimization technique that can shave off valuable milliseconds (or even seconds) from your site's load time. It's relatively simple to implement but should be done judiciously, with careful consideration of the origins to preconnect to and regular monitoring to ensure ongoing effectiveness.

Was this article helpful?

Thanks for your feedback!