🚀 HTML Performance Tricks – Super-Charge Your Pages in 2025
TL;DR: Ship less, ship smarter, and tell the browser what’s critical before it has to guess.
1 · Resource Hints: Pre-connect, Preload, DNS-Prefetch
Hint | Syntax | When to Use |
---|---|---|
dns-prefetch |
<link rel="dns-prefetch" href="//fonts.gstatic.com"> |
3rd-party hosts you might hit |
preconnect |
<link rel="preconnect" href="https://cdn.myapp.com" crossorigin> |
Domains you will hit within the first 2-3 s |
preload |
<link rel="preload" as="style" href="/css/critical.css"> |
Assets needed early but discovered late (fonts, hero images, JS split-chunks) |
Pro tip: Chain them:
dns-prefetch → preconnect → preload → fetch
. Each stage builds on the last.
2 · async
vs defer
for <script>
Attribute | Blocks HTML Parsing? | Executes Order | Ideal For |
---|---|---|---|
none (default) | Yes | immediate | Critical inline JS (rare) |
async |
No | arrival order | Analytics, ads, independent widgets |
defer |
No | DOM order after parsing | Main app bundles, polyfills |
Both wait until parsing ends, but preserve order—perfect for dependency stacks.
3 · Critical CSS & Content-Visibility
3.1 Inline above-the-fold styles
3.2 content-visibility
Off-screen cards are skipped by the rendering engine until scrolled into view—huge paint savings for feed-like pages.
4 · Optimise Fonts
-
Subset & compress with woff2.
-
Preload first font file:
-
font-display:
swap; avoid FOIT (flash of invisible text).
5 · HTTP/2 & 3 Multiplexing—Fewer Domains, More Push
-
Consolidate assets under one CDN host; parallel streams remove old “six-connections” limits.
-
Server push is deprecated in HTTP/3; rely on preload instead.
6 · Image & Media Budget
-
Use AVIF/WebP + fallback
<picture>
. -
Apply
loading="lazy"
to below-the-fold. -
Always set
width
/height
to kill CLS.
7 · Compression & Caching
Technique | Typical Gain |
---|---|
Brotli level-11 for text | 15-20 % smaller than gzip |
Gzip fallback | For legacy clients |
Immutable cache | /static/js/app.ab12c3.js → Cache-Control: public,max-age=31536000,immutable |
8 · Service-Worker Micro-Cache
Instant back/forward nav and offline snapshot for repeat users.
9 · Performance Budgets & Tooling
-
Lighthouse CI – fail PRs if LCP > 2.5 s.
-
WebPageTest – test 3G, 4G, HTTP/3.
-
Chrome DevTools Performance panel – locate main-thread long tasks (< 50 ms rule).
10 · Quick Checklist ✅
-
Inline ≤ 15 KB critical CSS
-
preconnect
to CDNs & font hosts -
defer
main bundles,async
analytics -
AVIF/WebP + lazy images
-
Brotli + long-term immutable caching
-
content-visibility:auto
on heavy lists -
Lighthouse score ≥ 90 on mobile 4G
Master these tricks and your pages will feel native-fast, rank higher, and keep users engaged. Happy optimising! 🏎️
💡 Frequently Asked Questions (FAQ) – HTML Performance Tricks
# | Question | Short Answer |
---|---|---|
1 | What’s the difference between preconnect and dns-prefetch ? |
dns-prefetch only resolves the domain name; preconnect goes further—opening the TCP/TLS handshake—so the first request starts faster. Use preconnect for certain early-use hosts, dns-prefetch for possible later hosts. |
2 | Should I inline all my CSS for speed? | No. Inline just the critical (above-the-fold) styles—≈10-15 KB max—then load the full stylesheet with rel="preload" onload="this.rel='stylesheet'" so subsequent pages can cache it. |
3 | async or defer —which is better? |
Use defer for scripts that depend on order or touch the DOM (main bundles). Use async for independent scripts (analytics, ads) where execution order doesn’t matter. |
4 | Does loading="lazy" harm SEO? |
No—Googlebot now supports native lazy-loading. Just be sure critical (above-the-fold) images stay eager so Largest Contentful Paint (LCP) isn’t delayed. |
5 | Is Brotli worth enabling if I already use gzip? | Yes. Brotli level-11 can shrink text assets ~15–20 % more than gzip. Keep gzip as fallback for older browsers; servers auto-negotiate. |
6 | What happened to HTTP/2 Server Push? | It’s deprecated in Chrome and absent in HTTP/3. Use <link rel="preload"> instead—gives similar hints without the push-cache bloat risk. |
7 | How big should my JavaScript bundle be? | Aim for < 150 KB (gzipped) initial bundle on mobile. Split less-critical code with dynamic import() and load when needed. |
8 | Can content-visibility:auto break layouts? |
Rarely; but you must supply contain-intrinsic-size so the browser reserves space. Test in Safari (partial support) and provide fallbacks if content pops. |
9 | Do I still need WebP if I’m serving AVIF? | Yes—Safari (pre-17) and older browsers lack AVIF. Offer <source type="image/avif"> , then WebP, then JPEG/PNG fallback. |
10 | How often should I run Lighthouse? | Automate! Run Lighthouse CI (or PageSpeed Insights API) on every pull-request or at least nightly; set budgets for LCP, TBT, CLS to catch regressions early. |