Page Speed Optimization: A Guide to Images, Fonts and JavaScript
Page speed optimization resource by resource: load images in the right format and priority, slim down fonts, defer JavaScript and speed up server response.
Page speed optimization is the set of image, font, JavaScript and server-side improvements that make a web page show its content sooner and respond to user interaction faster. You need to start with the heaviest resource: on most sites, that is the main image on the first screen and the scripts in the head. Finding the resource that holds the page back and fixing it delivers lasting results, unlike installing a random speed plugin.
I covered how speed is measured and the thresholds for LCP, INP and CLS in detail in my improve Core Web Vitals post. This post is one step more concrete: I explain which setting to change at the image, font, JavaScript, CSS and server layers, and why, with code examples. For the overall framework of technical SEO, see what is technical SEO.
Why does page speed matter, and how does it affect SEO?
Page speed is a core experience factor that decides whether a visitor arriving from search goes back before seeing the page. According to Google’s Search Central documentation, page experience is not a single ranking signal; however, the core ranking systems aim to reward content that offers a good page experience.[1] The same document also reminds us that relevance always comes first.[1]
That is why I see speed not as a ranking lever, but as an obstacle in front of good content. Speed costs money in two separate places. On the organic side, a slow page loses the visitor even after winning the click. On the ad side, a paid click lands on a landing page that never opens. I covered the structure of pages that receive ad traffic separately in my how to create a landing page post.
Speed is not a single number either. Phrases like “page load speed”, “page loading time” and “site speed” usually ask the same question: when did the user see the main content, and when did the page respond to a click? In measurement, I look at LCP (Largest Contentful Paint) and INP (Interaction to Next Paint), the metrics that answer these two questions.
How do you measure page speed?
Page speed is measured by reading real user data and lab tests together. The Core Web Vitals report in Search Console shows which URL group is slow. PageSpeed Insights and Chrome DevTools show why a page in that group is slow, at the resource level.
Three habits make measurement easier for me:
- Thinking at the template level: Product pages, blog posts and category pages are different templates. A problem in one template repeats on every page that uses it.
- Testing on mobile: In lab tests, the mobile profile simulates a slow processor and a slow connection. Problems you would miss on desktop show up here.
- Looking at the Network tab: The Network tab and Performance panel in DevTools show which file was downloaded when, and with which priority. The real diagnosis comes from here.
For details on reading the Search Console report, see my how to use Google Search Console post.
How do you optimize images?
Short answer
How do you optimize images for page speed?
Image optimization consists of loading an image at the right size, in a modern format and with the right priority. The main image on the first screen should download first, while images outside the screen should load as the user approaches them. If you also define dimensions, the page layout does not shift while the image arrives.
- Produce the image close to the size it is displayed at
- Use AVIF or WebP for photos
- Let the browser pick the right version with srcset and sizes
- Give the image width and height
- Add fetchpriority="high" to the main image on the first screen
- Give loading="lazy" to images outside the first screen
Images are the largest group of files on most pages, and the LCP element is often an image too. That is why image optimization is the first place I look in speed work. The work comes down to three decisions: how big is the file, what format is it in, and when is it downloaded?
Size and format
Producing an image close to the width it is displayed at saves more than changing the format. Sending a 2,000-pixel photo to an area displayed at 400 pixels wide forces the user to download pixels they will never see. For photos, AVIF and WebP usually produce smaller files than JPEG. For logos, icons and simple drawings, SVG is a better fit.
A responsive image is a technique that offers the browser versions of the same image at different widths and lets it pick the one that suits the device. The srcset attribute defines the versions, and the sizes attribute defines how wide the image will be on screen. You can find the details in MDN’s responsive images guide.
Priority and lazy loading
The main image on the first screen should download before other resources. According to web.dev’s Fetch Priority guide, the fetchpriority="high" attribute tells the browser that this image is important and can bring LCP forward.[3] It also matters that the LCP image appears directly in the HTML. If the image is a CSS background or is added later with JavaScript, the browser discovers it late.[2]
Lazy loading does the opposite. The browser’s built-in loading="lazy" attribute makes images outside the screen download only when the user approaches them.[4] The same guide clearly states that images visible on the first screen should not be lazy loaded.[4]
<!-- Main image on the first screen: no lazy, high priority, known dimensions -->
<img
src="/img/hero-800.avif"
srcset="/img/hero-400.avif 400w, /img/hero-800.avif 800w, /img/hero-1600.avif 1600w"
sizes="(max-width: 768px) 100vw, 800px"
width="800" height="450"
fetchpriority="high"
alt="Wide-angle photo of a clinic waiting room">
<!-- Image further down the page: the browser downloads it on approach -->
<img src="/img/ekip-600.webp" width="600" height="400"
loading="lazy" decoding="async" alt="Team members sitting at a table in a meeting">
The width and height attributes are needed not only for speed but also for visual stability. The browser calculates the aspect ratio from these values and reserves the space before the image arrives. That way, the text does not shift down.
Mistakes I often see
- loading="lazy" on the main image
- Main image defined as a CSS background
- Desktop-sized image sent to phones
- Missing width and height
- First slider frame loaded with JavaScript
The right setup
- fetchpriority="high" on the main image
- Main image in the HTML as an img tag
- Device-specific versions via srcset and sizes
- Dimensions defined on every image
- First frame delivered as HTML from the server
How do web fonts slow down a page?
Web fonts slow down a page by keeping text invisible until the browser downloads the font file, or by making text shift afterwards. Because the font file is defined inside CSS, the browser only discovers it after processing the CSS. If it comes from another domain, an extra connection setup is added on top.
web.dev’s font guide recommends several concrete ways to shorten this delay:[5]
- Use WOFF2: WOFF2 is the web font format with the best compression and broad browser support. Removing older formats from the list lowers file size.
- Use subset files: If the site only uses Latin and Turkish characters, there is no need to send a file that contains characters for every language. The
unicode-rangedescriptor tells the browser which file is needed for which characters. - Reduce the number of fonts: Each weight and style is a separate file. Two or three weights are enough for most designs. If many weights are needed, a single variable font file can be more efficient.
- Set up the connection early, or host fonts yourself: With a third-party font service,
preconnectshortens connection time. Serving fonts from your own domain removes this step entirely.
font-display determines how text looks while it waits for the font. According to MDN, the swap value shows text immediately in a fallback font and replaces it once the web font arrives. To reduce the shift this swap causes, you need to pick a fallback font whose metrics are close to the web font.
@font-face {
font-family: "Inter";
src: url("/fonts/inter-latin-ext.woff2") format("woff2");
font-weight: 400 700;
font-display: swap;
unicode-range: U+0000-00FF, U+011E-011F, U+0130-0131, U+015E-015F;
}
When creating a subset for Turkish sites, you need to make sure the characters ğ, ı, İ and ş stay in the file. If these letters are missing, the browser draws them with the fallback font, and a different typeface appears in the middle of a word.
How does JavaScript affect page speed?
JavaScript affects page speed both by delaying the first render while it downloads and runs, and by keeping the main thread busy so responses to interaction slow down. When the browser sees a script in the head without defer or async, it stops processing the HTML and waits for the script. The Lighthouse documentation flags these files as “render-blocking resources”.
Here is the order I follow to speed up the first render:
- Remove unused code
The DevTools Coverage tab shows the share of JavaScript and CSS that is downloaded but never runs on the page. Old campaign scripts and unused plugins are the first candidates.
- Give the remaining scripts defer
defer downloads the script in the background while the HTML is processed and runs it in order after the page is parsed. For independent scripts that do not depend on each other, async can also be used.
- Split the code
Instead of sending the whole site as one large bundle, send each page only the code it needs. Load components such as modals, maps and chat windows when the user opens them.
- Audit third-party tags
Review every pixel in the tag manager one by one. Remove the unused ones and limit triggers to only the pages that need them.
- Use a facade for heavy embeds
Show a lightweight preview first instead of embeds such as video players and chat widgets. Let the actual content load when the user clicks.
Third-party scripts need separate attention. web.dev’s guide to third-party JavaScript recommends loading these scripts with async or defer, setting up connections early and lazy loading embeds. Measurement and ad tags create a dilemma I run into often here: the marketing team wants data, while the page gets a little slower with every new tag.
What can be done on the CSS and server side?
Optimization on the CSS and server side lets the browser receive the first byte earlier and process the stylesheet it needs to paint the page faster. After images and JavaScript are fixed, the remaining delay usually shows up in these two layers.
CSS
The browser does not paint the page before it downloads and processes all stylesheets. That is why keeping CSS small directly speeds up the first render. Critical CSS is the technique of writing the styles needed to paint the first screen inline in the HTML and loading the rest later. If styles needed only under certain conditions, such as printing, are separated with the media attribute, the browser does not wait for them.
Server and caching
Time to First Byte (TTFB) is the time between the browser sending a request and receiving the first byte of the response from the server. web.dev’s LCP guide states that this time is the first subpart of LCP; if the server responds late, every following step is delayed too.[2] These are the areas I check on the server side:
| Layer | What to do | What it speeds up |
|---|---|---|
| Caching | Page and database caching, static generation | Stops the server from rebuilding the page on every request |
| CDN | Distributes content from servers close to the user | Shortens connection time for visitors in distant markets |
| Compression | Brotli or gzip for HTML, CSS and JavaScript | Lowers the size of transferred text files |
| HTTP cache headers | Long Cache-Control lifetime for static files | Prevents files from being downloaded again on repeat visits |
| Redirects | Reduces chained redirects to a single step | Removes the round trip each extra redirect adds |
A CDN is especially important for sites that receive international traffic. A visitor in Europe or the Middle East to a site hosted in Turkey travels a longer network path on every request. I covered the effect of redirect chains on speed and crawling separately in my 301 redirect post.
The choice of infrastructure also determines the outcome from the start. I generated this site statically with Astro; because pages are not built at request time, there is no database query to wait for on the server side. I explained the SEO side of this approach in my Astro SEO post.
In what order should page speed optimization be done?
Page speed optimization should start with the template that affects the most pages, and with the heaviest resource in that template. When prioritizing, I look at two questions: how many URLs does this change affect, and how much effort does it take? Adding fetchpriority to the main image is a one-line change but can improve thousands of product pages at once. Restructuring a JavaScript bundle, on the other hand, can take days.
I use the list below when auditing a template:
After every change, you need to re-measure the same page with the same tool. The lab result changes immediately. Field data in Search Console, on the other hand, improves gradually because it is based on a rolling window of real user data.
Conclusion
Page speed optimization consists of a series of decisions about which resource loads when, and with which priority. Bringing the main image on the first screen forward, lazy loading what is off screen, slimming down fonts and deferring render-blocking JavaScript deliver the biggest gains on most sites. Server response, caching and a CDN make sure those gains reach every visitor.
As a next step, find the slow template in Search Console that affects the most URLs and apply the list above to that template. If you want to handle speed work together with crawling, indexing and structured data, I explained how I work on the SEO services page.
Key terms
- Page speed optimization
- Page speed optimization is the set of technical improvements made at the image, font, JavaScript, CSS and server layers so that a web page shows its content sooner and responds to user interaction faster. Its effect is measured with the Core Web Vitals metrics.
- Lazy loading
- Lazy loading is a loading technique that downloads resources such as images and iframes only when the user approaches them on screen. Browsers support it natively through the loading="lazy" attribute, and it should only be applied to resources outside the first screen.
- fetchpriority
- fetchpriority is an HTML attribute that tells the browser the relative download priority of an image, script or other resource. It accepts high, low and auto; its most common use is giving the LCP image fetchpriority="high" so it downloads before other resources.
- font-display
- font-display is an @font-face descriptor that determines how the browser shows text while a web font has not yet downloaded. The swap value shows text immediately in a fallback font and replaces it when the web font arrives, which shortens the time text stays invisible.
- Render-blocking resource
- A render-blocking resource is a CSS or JavaScript file that the browser must download and process before it can paint the page. Scripts in the head without defer or async, and all stylesheets, fall into this group; they delay the first render.
Frequently asked questions
How do you do page speed optimization?
First, use PageSpeed Insights and Search Console to find which template is slow. Then start with the heaviest resource: usually the LCP image, followed by fonts and render-blocking JavaScript. Finally, check server response, caching and compression, and re-measure every change with the same tool.
Does page speed affect SEO?
Google says page experience is not a single ranking signal, but its core ranking systems aim to reward content that offers a good page experience. Relevance always comes first. The real effect of speed is that a visitor arriving from search does not bounce back before the page loads.
What is the best image format?
For photographic images, AVIF and WebP usually produce smaller files than JPEG and PNG. For drawings such as logos and icons, SVG is a better fit. More important than the format is serving the image close to the size it is displayed at and letting the browser pick the right version with srcset.
Should lazy loading be used on every image?
No. Lazy loading should only be used on images outside the first screen. Lazy loading the main image on the first screen, which is usually the LCP element, delays its load. That image should get fetchpriority="high" instead.
Does Google Tag Manager slow down a site?
The tag manager itself is lightweight, but every pixel and third-party script added to it runs separately in the browser. Removing unused tags, narrowing triggers and moving suitable tags to the server side reduces the load on the main thread.
Sources
The full topic: Technical SEO guide ↗