{"id":5435,"date":"2020-10-20T13:21:49","date_gmt":"2020-10-20T13:21:49","guid":{"rendered":"https:\/\/shortpixel.com/blog\/?p=5435"},"modified":"2020-12-14T14:06:57","modified_gmt":"2020-12-14T14:06:57","slug":"what-is-preconnect-and-why-you-should-implement-it-on-your-website","status":"publish","type":"post","link":"https:\/\/shortpixel.com\/blog\/what-is-preconnect-and-why-you-should-implement-it-on-your-website\/","title":{"rendered":"What is Preconnect and why you should implement it on your website"},"content":{"rendered":"\n<p>When we talk about improving your website&#8217;s performance, usually you read about minifying and combining CSS and JS files, or <a href=\"https:\/\/shortpixel.com/blog\/how-to-increase-your-wp-site-performance\/#compress\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"enabling compression (opens in a new tab)\">enabling compression<\/a>, or <a rel=\"noreferrer noopener\" aria-label=\"optimizing your images (opens in a new tab)\" href=\"https:\/\/wordpress.org\/plugins\/shortpixel-adaptive-images\/\" target=\"_blank\">optimizing your images<\/a>.<\/p>\n\n\n\n<p>However, there are many little things you can do in addition to those. One of them is implementing <strong>preconnect <\/strong>on your site.<\/p>\n\n\n\n<!--more-->\n\n\n\n<h2 class=\"wp-block-heading\">What is preconnect?<\/h2>\n\n\n\n<p>Preconnect tells the browser to <strong>establish an early connection to external domains<\/strong>, which includes the DNS lookup, TCP handshake, and optional TLS negotiation. This is time you will save when loading a page. To illustrate this, let&#8217;s do a simple test.<\/p>\n\n\n\n<p>The following screenshot shows a regular website making three calls to <code>m.stripe.com<\/code>, <code>fonts.googleapis.com<\/code> and <code>fonts.gstatic.com<\/code>. As you can see, the DNS lookup, initial connection and SSL negotation is done when the resource needs to be loaded, which is late in the loading process.<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"1024\" height=\"281\" src=\"https:\/\/shortpixel.com/blog\/wp-content\/uploads\/2020\/07\/Screenshot_2020-07-15-16_34_50-1024x281.jpg\" alt=\"\" class=\"wp-image-5459\" title=\"\" srcset=\"https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/07\/Screenshot_2020-07-15-16_34_50-1024x281.jpg 1024w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/07\/Screenshot_2020-07-15-16_34_50-300x82.jpg 300w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/07\/Screenshot_2020-07-15-16_34_50-768x211.jpg 768w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/07\/Screenshot_2020-07-15-16_34_50.jpg 1235w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>Now, if we preconnect to those external resources, we get the following:<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" width=\"1024\" height=\"275\" src=\"https:\/\/shortpixel.com/blog\/wp-content\/uploads\/2020\/07\/Screenshot_2020-07-15-16_43_09-1024x275.jpg\" alt=\"\" class=\"wp-image-5460\" title=\"\" srcset=\"https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/07\/Screenshot_2020-07-15-16_43_09-1024x275.jpg 1024w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/07\/Screenshot_2020-07-15-16_43_09-300x81.jpg 300w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/07\/Screenshot_2020-07-15-16_43_09-768x207.jpg 768w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/07\/Screenshot_2020-07-15-16_43_09.jpg 1238w\" sizes=\"(max-width: 1024px) 100vw, 1024px\" \/><\/figure>\n\n\n\n<p>The difference is clear, preconnect allows to do the DNS lookup, initial connection and SSL negotation in the background when the page is loading, so later in the loading process those 3 tasks will be already done, and the only thing the page will need to do is to load the actual content.<\/p>\n\n\n\n<p>The result is a better loading time, or in other words, <strong>you will improve your site performance<\/strong>.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">Do not preconnect everything!<\/h3>\n\n\n\n<p>You should <strong>not <\/strong>preconnect to all your external domains. <\/p>\n\n\n\n<p>It might be contradictory, considering that we just proved that preconnecting improves the loading speed. However, you should know that\u00a0<\/p>\n\n\n\n<ol class=\"wp-block-list\"><li>browsers <a rel=\"noreferrer noopener\" aria-label=\"close any connection that isn&#039;t used within a few seconds (opens in a new tab)\" href=\"https:\/\/web.dev\/preconnect-and-dns-prefetch\/\" target=\"_blank\">close any connection that isn&#8217;t used within a few seconds<\/a>. <\/li><li>there\u2019s a <a href=\"https:\/\/andydavies.me\/blog\/2019\/08\/07\/experimenting-with-link-rel-equals-preconnect-using-custom-script-injection-in-webpagetest\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"limit on the number of simultaneous connections (opens in a new tab)\">limit on the number of simultaneous connections<\/a> in browsers.<\/li><\/ol>\n\n\n\n<p>Therefore, as a rule of thumb <strong>you should only preconnect to the 3 or 4 most important external domains<\/strong>. Preconnecting to everything may actually slow down your website.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">How to implement it?<\/h2>\n\n\n\n<p>Following the previous example, the goal is to <strong>enter the following lines between the tags <code>&lt;head&gt;<\/code> and <code>&lt;\/head&gt;<\/code><\/strong> of your site.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>&lt;link rel=\"preconnect\" href=\"https:\/\/fonts.googleapis.com\">\n&lt;link rel=\"preconnect\" href=\"https:\/\/fonts.gstatic.com\" crossorigin=\"anonymous\">\n&lt;link rel=\"preconnect\" href=\"https:\/\/m.stripe.com\"><\/code><\/pre>\n\n\n\n<p>You should add a line for each external call. In our case, we implemented preconnect to 3 external domains.<\/p>\n\n\n\n<p>If you pay attention to the middle line, you&#8217;ll see the attribute <code>crossorigin<\/code>. <strong>When to add it? When the resources on the connection are downloaded using <a rel=\"noreferrer noopener\" aria-label=\"CORS (opens in a new tab)\" href=\"https:\/\/www.codecademy.com\/articles\/what-is-cors\" target=\"_blank\">CORS<\/a><\/strong>. If the assets use CORS,&nbsp;<code>crossorigin<\/code> is needed. If CORS won&#8217;t be used, <code>crossorigin <\/code>should be omitted. If you are not sure, you can try with and without, and check which one works \ud83d\ude09<\/p>\n\n\n\n<p>To add those lines, we have two methods.<\/p>\n\n\n\n<p>1 &#8211; <strong>Use a plugin<\/strong> like <a href=\"https:\/\/wordpress.org\/plugins\/insert-headers-and-footers\/\" target=\"_blank\" rel=\"noreferrer noopener\" aria-label=\"Insert Headers and Footers (opens in a new tab)\">Insert Headers and Footers<\/a>. Install, activate it, go to Settings and add the code in the first box.<br><img decoding=\"async\" width=\"800\" height=\"266\" class=\"wp-image-5461\" style=\"width: 800px;\" src=\"https:\/\/shortpixel.com/blog\/wp-content\/uploads\/2020\/07\/Screenshot-20200716-125220.jpg\" alt=\"\" title=\"\" srcset=\"https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/07\/Screenshot-20200716-125220.jpg 1114w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/07\/Screenshot-20200716-125220-300x100.jpg 300w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/07\/Screenshot-20200716-125220-768x256.jpg 768w, https:\/\/shortpixel.com\/blog\/wp-content\/uploads\/2020\/07\/Screenshot-20200716-125220-1024x341.jpg 1024w\" sizes=\"(max-width: 800px) 100vw, 800px\" \/><\/p>\n\n\n\n<p>2 &#8211; The recommended method, but for advanced users: <strong>Edit the functions.php file<\/strong> of your child theme and add the following piece of code.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>function shortpixel_preconnect() {\necho '&lt;link rel=\"preconnect\" href=\"https:\/\/fonts.googleapis.com\">\n&lt;link rel=\"preconnect\" href=\"https:\/\/fonts.gstatic.com\" crossorigin=\"anonymous\">\n&lt;link rel=\"preconnect\" href=\"https:\/\/m.stripe.com\">';\n}\nadd_action('wp_head', 'shortpixel_preconnect', 0);<\/code><\/pre>\n\n\n\n<p><\/p>\n\n\n\n<p>Is your website still slow? <a href=\"https:\/\/shortpixel.com\/lp\/wso\/?utm_source=BLOG\">Find out here why.<\/a><\/p>\n","protected":false},"excerpt":{"rendered":"<p>When we talk about improving your website&#8217;s performance, usually you read about minifying and combining CSS and JS files, or enabling compression, or optimizing your images. However, there are many little things you can do in addition to those. One of them is implementing preconnect on your site.<\/p>\n","protected":false},"author":9,"featured_media":5460,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[],"class_list":["post-5435","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-performance"],"blocksy_meta":[],"_links":{"self":[{"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/posts\/5435","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/users\/9"}],"replies":[{"embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/comments?post=5435"}],"version-history":[{"count":8,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/posts\/5435\/revisions"}],"predecessor-version":[{"id":5613,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/posts\/5435\/revisions\/5613"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/media\/5460"}],"wp:attachment":[{"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/media?parent=5435"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/categories?post=5435"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/shortpixel.com\/blog\/wp-json\/wp\/v2\/tags?post=5435"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}