Web Performance Tips: 13 Ways to Speed Up Your Site
Contents
- 1. Measure before you optimize
- 2. Use WebP or AVIF images
- 3. Lazy-load images below the fold
- 4. Prioritize the LCP image with fetchpriority
- 5. Always set width and height on images
- 6. Serve responsive images with srcset
- 7. Load only the characters you need from Google Fonts
- 8. Self-host fonts and preload the important one
- 9. Defer your JavaScript
- 10. Preconnect to third-party origins
- 11. Cache static files for a long time and bust the cache on change
- 12. Skip rendering offscreen sections with content-visibility
- 13. Avoid redirect chains
- Where to start
- FAQ
You should care about web performance. A fast site is better for your users, it helps your SEO, and it increases the conversion rate. And we all want more profit, right?
I share a lot of web performance tips, and this article is the archive for them. Every tip comes with code you can copy, and where it applies, I note how I do it on this site.
The order matters: measure first, then fix the biggest problem. Usually that’s images, then fonts, then JavaScript.
1. Measure before you optimize
Run your page through PageSpeed Insights. The top section, “Discover what your real users are experiencing”, is field data from real Chrome users over the last 28 days. That’s what Google uses. The Lighthouse score below it is a lab test of one page load on a simulated device: useful for debugging, but not what Google ranks you on.
Focus on the three Core Web Vitals:
- LCP (Largest Contentful Paint): how fast the main content shows up. Good is 2.5 s or less.
- INP (Interaction to Next Paint): how fast the page responds to clicks and taps. Good is 200 ms or less. It replaced FID in March 2024.
- CLS (Cumulative Layout Shift): how much the layout jumps while loading. Good is 0.1 or less.
Most of the tips below fix one of these three. I note which one.
2. Use WebP or AVIF images
Use the .webp image format to make images smaller and boost the performance of your website. Fixes: LCP. For icons and logos, inline SVG is smaller still and stays sharp at any size.
Every modern browser supports WebP now, so a plain <img src="photo.webp"> is fine. Use <picture> when you want to serve AVIF, which is often even smaller, with WebP as the fallback:
<picture>
<!-- load .avif if supported -->
<source srcset="hero.avif" type="image/avif">
<!-- fallback for browsers without AVIF support -->
<img src="hero.webp" alt="Hero image" width="1200" height="630">
</picture>
3. Lazy-load images below the fold
You can use the loading="lazy" attribute to defer loading an image until the user scrolls near it. Visitors who never scroll that far never download it, and the images on screen don’t have to share bandwidth with it.
Don’t lazy-load the image at the top of the page (your hero or featured image). That’s usually your LCP element, and lazy-loading it makes LCP slower. Lazy-load everything below it.
<!-- Below the fold: lazy -->
<img src="screenshot.webp" loading="lazy" alt="Alternative Text" width="800" height="450">
<!-- Works for iframes too (YouTube embeds, maps) -->
<iframe src="https://www.youtube.com/embed/VIDEO_ID" loading="lazy" title="Video title"></iframe>
4. Prioritize the LCP image with fetchpriority
Tip 3 delays offscreen images. This one does the opposite: it tells the browser to download the most important image first. Fixes: LCP.
<img src="hero.webp" fetchpriority="high" alt="Hero image" width="1200" height="630">
5. Always set width and height on images
Without dimensions, the browser doesn’t know how much space an image needs until it downloads, so the text below it jumps down. Set width and height to the image’s real size. The browser reserves the space from that aspect ratio, and CSS can still make the image responsive. Fixes: CLS.
For embeds and placeholders without width and height, reserve the space with aspect-ratio.
<img src="photo.webp" width="1600" height="900" alt="Alternative Text">
<style>
img {
max-width: 100%;
height: auto; /* keeps the aspect ratio from width/height */
}
</style>
6. Serve responsive images with srcset
A phone doesn’t need your 2400px desktop image. Give the browser several sizes and let it pick the smallest one that still looks sharp. Fixes: LCP on mobile.
If you’re on WordPress, the_post_thumbnail() and the image block output srcset for you.
<img
src="photo-800.webp"
srcset="photo-400.webp 400w, photo-800.webp 800w, photo-1600.webp 1600w"
sizes="(max-width: 768px) 100vw, 800px"
width="1600" height="900"
alt="Alternative Text">
7. Load only the characters you need from Google Fonts
You can add a text= value to your Google Fonts request URL, and Google returns a font file with only those characters. According to Google, this can cut the file size by up to 90%. Super useful when you use a web font only in a logo or a heading.
<!-- Instead of loading the whole font -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Comfortaa">
<!-- Load only what you will use -->
<link rel="stylesheet" href="https://fonts.googleapis.com/css2?family=Comfortaa&text=Hello">
8. Self-host fonts and preload the important one
Self-hosting your fonts saves the browser a connection to another server. A variable .woff2 file holds every weight, so one file replaces a separate file per weight. font-display: swap shows text in a fallback font right away instead of hiding it while the font loads.
On this site: I self-host variable woff2 fonts (Inter Tight for text and headings, JetBrains Mono for code), all with font-display: swap. Only Inter Tight is preloaded, because it’s in the first thing you see. Preload only the font the first screen needs: every preload competes for bandwidth with your LCP image.
<!-- In <head>: crossorigin is required for font preloads -->
<link rel="preload" as="font" type="font/woff2" crossorigin href="/fonts/InterTight-Variable.woff2">
<style>
@font-face {
font-family: 'Inter Tight';
font-weight: 100 900; /* variable font: all weights in one file */
font-display: swap;
src: url('/fonts/InterTight-Variable.woff2') format('woff2');
}
</style>
9. Defer your JavaScript
A plain <script src> in the <head> stops the HTML parser until the script has downloaded and run. defer downloads the script in parallel and runs it after the HTML is parsed, in order. async runs it as soon as it arrives, in any order, so use it only for independent scripts like analytics. Once your scripts run, schedule visual updates with requestAnimationFrame instead of setTimeout to keep animations smooth. Fixes: LCP.
Module scripts (type="module") are deferred by default. That’s how this site loads its own JavaScript.
<!-- Blocks parsing -->
<script src="app.js"></script>
<!-- Runs after parsing, keeps order -->
<script src="app.js" defer></script>
<!-- Runs whenever it arrives: only for independent scripts -->
<script src="analytics.js" async></script>
10. Preconnect to third-party origins
Every new domain costs a DNS lookup, a TCP connection and a TLS handshake before the first byte arrives. If you know you’ll load something from another origin early (a CDN, Google Fonts), start that connection right away. Use it only for the one or two origins the first screen needs: an unused preconnect wastes a connection.
<link rel="preconnect" href="https://fonts.googleapis.com">
<!-- Font files come from a different origin and need crossorigin -->
<link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
11. Cache static files for a long time and bust the cache on change
For a returning visitor, the fastest request is the one that never happens. Send your CSS, JS and fonts with a long cache lifetime, for example Cache-Control: public, max-age=31536000, immutable. When a file changes, change its URL so browsers fetch the new version.
On this site: the theme adds the file’s last-modified time to the URL of its CSS and JS bundles (app.css?v=1791708662). When I rebuild, the timestamp changes and so does the URL, so I never bump a version number by hand. In WordPress it looks like this:
<?php
$path = get_stylesheet_directory() . '/public/css/app.css';
$url = get_stylesheet_directory_uri() . '/public/css/app.css';
?>
<link rel="stylesheet" href="<?php echo esc_url( $url . '?v=' . filemtime( $path ) ); ?>">
12. Skip rendering offscreen sections with content-visibility
On long pages, content-visibility: auto lets the browser skip layout and paint for sections until they get close to the viewport. contain-intrinsic-size gives each section a placeholder height so the scrollbar doesn’t jump. All major browsers support it since 2024.
Use it on big, independent blocks below the fold (comment lists, long article sections, footers), not on small elements.
.article-section {
content-visibility: auto;
contain-intrinsic-size: auto 800px; /* estimated height */
}
13. Avoid redirect chains
Redirects cost time too. Every hop is another round trip before the page even starts loading, so it pays to know your HTTP status codes. Link straight to the final URL (trailing slash, https://, www or not), and if /a redirects to /b and /b to /c, point /a straight at /c.
You can check a URL from the terminal. Each HTTP/ line is one hop:
curl -sIL http://example.com | grep -iE "^(HTTP/|location:)"
Where to start
If you only have an hour: run PageSpeed Insights, find your LCP element, and fix it first (tips 2, 4 and 6). Then add width and height to your images and defer to your scripts. That’s usually the biggest win for the least work.
I add new tips here as I share them, so bookmark this page.
FAQ
What are the Core Web Vitals?
Three metrics Google uses to measure page experience: LCP (loading, good is 2.5 s or less), INP (responsiveness, good is 200 ms or less) and CLS (visual stability, good is 0.1 or less). Google measures them on real Chrome users.
Should I lazy-load all images?
No. Lazy-load images below the fold, but not the image at the top of the page. That’s usually your LCP element, and lazy-loading it makes LCP slower. Give it fetchpriority="high" instead.
Do I still need a fallback for WebP images?
Not anymore. Every modern browser supports WebP. Use <picture> only if you want to serve AVIF with WebP as the fallback.
Why is my Lighthouse score different from the field data in PageSpeed Insights?
Lighthouse is a lab test: one page load on a simulated device and network. Field data comes from real Chrome users over the last 28 days. Google uses the field data, so use Lighthouse to debug and field data to judge.