MODRACXKENNETH D'SILVA

← Archive & Insights

Using Preconnect and DNS Prefetch for Faster Magento & Shopify

Learn how to leverage preconnect and dns-prefetch hints to reduce latency, improve Core Web Vitals, and boost SEO for Magento and Shopify stores.

By Kenneth D'Silva (MODRACX)

Introduction

When a browser loads a page, it must resolve DNS, establish TCP, and complete TLS handshakes for every third‑party origin. Resource hints such as preconnect and dns-prefetch allow you to tell the browser to start these steps early, shaving precious milliseconds off the page load—critical for Largest Contentful Paint (LCP) and overall SEO performance.

1. What Are Resource Hints?

Hint When to Use Effect
dns-prefetch Domains used later (e.g., analytics) Initiates DNS lookup early.
preconnect Domains serving critical resources (fonts, CDNs) Starts DNS, TCP, and TLS handshake early.
preload Critical assets that must be fetched immediately (CSS, scripts) Forces early download.

2. Identifying Critical Third‑Party Domains

Typical e‑commerce sites pull resources from: - Google Fonts (fonts.googleapis.com, fonts.gstatic.com) - CDNs for JS libraries (e.g., cdnjs.cloudflare.com) - Analytics (www.google-analytics.com) - Payment gateways (checkout.stripe.com) - Image CDNs (cdn.shopify.com, cloudflare.com)

3. Magento 2 Implementation

3.1 Add Hints in default_head_blocks.xml

<head>
    <!-- DNS prefetch for Google Analytics -->
    <link rel="dns-prefetch" href="//www.google-analytics.com" />
    <!-- Preconnect to Google Fonts -->
    <link rel="preconnect" href="https://fonts.googleapis.com" crossorigin />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <!-- Preconnect to CDN for JavaScript -->
    <link rel="preconnect" href="https://cdnjs.cloudflare.com" crossorigin />
    <!-- Preconnect to Stripe for checkout -->
    <link rel="preconnect" href="https://checkout.stripe.com" crossorigin />
</head>
  • Clear caches: bin/magento cache:flush.

3.2 Verify with Chrome DevTools

Open Network tab, look for Initiator column showing early preconnect requests before page resources load.

4. Shopify Implementation

4.1 Edit layout/theme.liquid

{% comment %} Resource hints for better performance {% endcomment %}
<link rel="dns-prefetch" href="//www.google-analytics.com" />
<link rel="preconnect" href="https://fonts.googleapis.com" crossorigin>
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
<link rel="preconnect" href="https://cdn.shopify.com" crossorigin>
<link rel="preconnect" href="https://checkout.stripe.com" crossorigin>
  • Place these tags inside the <head>.

4.2 Theme Settings for Fonts

If using Google Fonts via Shopify’s Theme Settings, the above hints will ensure the fonts are fetched instantly.

5. Performance Validation

Tool Metric Target
WebPageTest Time to First Byte (TTFB) < 200 ms
Lighthouse Performance Score > 92
Search Console Core Web Vitals – Good > 95 %

Run a before‑and‑after test to see latency improvements (often 50‑150 ms).

6. SEO Checklist

7. Common Pitfalls

Issue Symptom Fix
Over‑using preconnect on low‑traffic domains Unnecessary connections increase TCP overhead. Limit hints to truly critical origins.
Missing crossorigin on font preconnect Font loading fallback, causing flash of unstyled text (FOUT). Add crossorigin attribute.
Hints placed after critical CSS Browser can’t act early. Ensure hints are first in the <head>.

Conclusion & Call‑to‑Action

Resource hints are a low‑effort, high‑impact way to accelerate Magento and Shopify storefronts, directly improving Core Web Vitals and SEO rankings. Ready to add preconnect and DNS‑prefetch to your store? Click the “Start a project” button in the MODRACX portfolio and let’s speed up your e‑commerce experience together.


Kenneth D’Silva – Magento & Shopify specialist, MODRACX