HomeBlogHow to Optimize Images and Assets...

Engineering Guide · 8 min read · May 19, 2026

How to Optimize Images and Assets for Faster Websites

Uncompressed hero photos, oversized CSS bundles, and un-subsetted fonts account for most wasted megabytes on business websites. Learn a repeatable pipeline for AVIF and WebP generation, responsive srcset markup, font subsetting, and immutable CDN caching.

Author
Smit Desai
Published
May 19, 2026
Read time
8 min read
Topic
Security & SEO
Services
Custom Web · Mobile Apps · All Services

Images, fonts, and static bundles routinely make up over 75% of the bytes downloaded on a typical business homepage. When managing 300+ WordPress sites at the server level for a Finnish partner, the single quickest win on slow client domains rarely required touching PHP—it came from finding 4MB PNG product photos uploaded straight from a designer's export folder or five un-subsetted font families blocking first paint.

Compressing files one by one in an online tool before uploading does not scale once editors, marketers, or store managers add content every week. Real asset optimization needs an automated pipeline plus strict HTML delivery rules that align with Core Web Vitals and Google's SEO Starter Guide.

Picking the Right Image Format for Each Job

No single format wins across photography, crisp UI diagrams, and scalable logos. Choosing formats deliberately prevents both blurry artifacts and bloated downloads.

Asset Type Best Primary Format Fallback Format Why It Wins
Hero & editorial photos AVIF WebP / JPEG Highest compression efficiency at clean detail
UI screenshots & icons WebP (lossless) or SVG PNG Sharp edges without JPEG ringing
Logos & simple illustrations Inline or external SVG PNG Resolution-independent, tiny after SVGO
Legacy animated GIFs MP4 / WebM (<video>) Animated WebP Fraction of GIF size with hardware decode

Replacing looping 6MB hero GIFs with muted, auto-playing <video playsinline muted loop> MP4/WebM streams alone often cuts initial homepage weight by several megabytes.

Delivering Exact Pixel Dimensions with srcset

Sending a 2400px desktop hero image to a 390px smartphone wastes bandwidth and CPU decoding time. Generate width variants at build or upload time (for example 480w, 768w, 1200w, 1800w) and let the browser pick the smallest fit via <picture>, srcset, and sizes:

<picture>
  <source
    type="image/avif"
    srcset="
      /media/case-study-480.avif 480w,
      /media/case-study-960.avif 960w,
      /media/case-study-1600.avif 1600w
    "
    sizes="(max-width: 768px) 92vw, 820px"
  >
  <source
    type="image/webp"
    srcset="
      /media/case-study-480.webp 480w,
      /media/case-study-960.webp 960w,
      /media/case-study-1600.webp 1600w
    "
    sizes="(max-width: 768px) 92vw, 820px"
  >
  <img
    src="/media/case-study-960.webp"
    alt="Production dashboard architecture showing cache hit ratios"
    width="1600"
    height="900"
    decoding="async"
  >
</picture>

Guarding Hero vs. Below-the-Fold Loading

Keep loading="lazy" off any image visible inside the initial viewport; reserve loading="lazy" for cards, gallery grids, and footer logos further down the page. Always keep width and height attributes on every <img> tag so the browser calculates aspect ratio before pixels arrive, preventing layout shifts.

Automating Responsive Variants Server-Side

In Node, Laravel, or image proxy pipelines, process uploads into AVIF/WebP derivatives asynchronously in a queue worker rather than blocking the admin HTTP request. Keep a max source dimension cap (for example, 2560px) and strip EXIF camera metadata—aside from shrinking bytes, stripping GPS coordinates protects user privacy on user-uploaded photos.

Preventing Layout Shifts on Lazy-Loaded Galleries

Even properly compressed images hurt visual stability when product grids pop in without reserved vertical space. Always keep explicit width and height HTML attributes or matching CSS aspect-ratio rules on every card wrapper, and serve lightweight blurred placeholders or solid surface backgrounds while below-the-fold thumbnails decode.

Shrinking CSS, JavaScript, and Web Fonts

Once images are sized correctly, text assets and typography usually hold the remaining quick wins.

Subsetting Fonts to the Characters You Use

A full multi-script font file can weigh 180KB per weight because it ships Cyrillic, Greek, Vietnamese, and hundreds of rare glyphs your English or Nordic site never renders. Subset fonts to latin (plus latin-ext if you serve European languages) as WOFF2 files:

@font-face {
  font-family: 'Inter';
  src: url('/fonts/inter-latin-600.woff2') format('woff2');
  font-weight: 600;
  font-style: normal;
  font-display: swap;
  unicode-range: U+0000-00FF, U+0131, U+0152-0153, U+02BB-02BC, U+2000-206F;
}

Prefer variable WOFF2 fonts when your design needs four or more weights—one 55KB variable file often beats four separate 35KB static files.

Trimming Unused CSS and SVG Bloat

Run Tailwind or PurgeCSS treeshaking in production so unused utility classes are stripped, and inline only critical above-the-fold styles if your stylesheet blocks rendering. For SVG icons, run svgo during build to remove editor metadata, hidden layers, and redundant decimal precision, and combine recurring UI icons into an SVG <symbol> sprite instead of repeating multi-kilobyte <path> markup twenty times in a list.

Configuring Compression and Long-Lived Cache Headers

Even tiny assets slow down repeat visits if browsers revalidate them on every navigation. Hash static filenames at build time (app.8f21c9.css, hero-960.3a91e.avif) and configure Nginx or your CDN to serve them with immutable one-year caching and Brotli compression:

location ~* \.(?:avif|webp|woff2|svg|css|js)$ {
    add_header Cache-Control "public, max-age=31536000, immutable";
    brotli on;
    brotli_comp_level 6;
    gzip on;
    gzip_vary on;
}

Never apply immutable to unhashed URLs like /css/style.css, or visitors will stay stuck on stale stylesheets after deployments. On dynamic HTML pages, keep Cache-Control: public, max-age=0, must-revalidate or short CDN s-maxage rules paired with cache purging. Also validate SVG uploads strictly—raw user-uploaded SVGs can contain <script> tags and XSS payloads, an issue highlighted in OWASP Top Ten and in my Laravel security audit checklist.

If an older CMS or legacy storefront forces manual resizing or loads unbundled vendor scripts across every template, you can read how I handle fleet-wide server pipelines in managing 300+ WordPress sites at server level or plan a cleaner media architecture when upgrading old software.

How SmitDev Builds Fast Asset Pipelines

Across 170+ shipped websites and web applications, I set up automated image optimization pipelines, clean font loading, and CDN caching rules so non-technical teams can upload photos safely without breaking page speed.

  • 7-day fast business website ($399): Clean, asset-budgeted marketing website with automated WebP/AVIF delivery and strong Core Web Vitals.
  • Dedicated engineering ($55/day): 8–9 hours/day refactoring heavy asset pipelines, Nginx/CDN headers, and front-end bundles.
  • Monthly product delivery ($1,999/month): Ongoing engineering for custom web application development, asset pipelines, and performance governance.

Check my services or send your site URL via the contact page for a practical asset weight and speed audit.

Written by Smit Desai · Principal Full-Stack & FDSE

8+ years shipping production systems · 171 case studies · 300+ WordPress servers managed for Finland partners.

Next Steps and Recommended Resources

Next Steps · Recommended Resources

Pillar 1 · Infrastructure Proof

300+ WordPress & Cloud Server Fleet

From server-level management of 300+ WordPress sites for a Finland client to PostgreSQL query tuning (3,982 -> 52 queries).

Pillar 2 · Legacy Rescue

Upgrade Old Software (10 Hrs Free)

Audit bottlenecks, eliminate security vulnerabilities, and stabilize production servers—starting with 10 free engineering hours.

01 — Frequently asked questions

security & seo

Should I convert every website image to AVIF instead of WebP?

AVIF achieves smaller file sizes at comparable visual quality, especially on photography, while WebP encodes faster and handles lossless graphics cleanly. Serving AVIF first with a WebP fallback inside a <picture> tag gives the best balance.

Why do compressed images still fail PageSpeed 'Properly size images' checks?

Compression lowers bytes per pixel, not pixel dimensions. If you upload a 3000px-wide image and display it inside a 420px product card without srcset variants, mobile browsers still download and decode millions of extra pixels.

How many web fonts are safe to load on a marketing site?

Aim for one or two font families with two to three weights (such as 400, 600, and 700) stored as subsetted WOFF2 files. Loading six weights across three families adds hundreds of kilobytes and causes visible text flash.

Is Gzip compression enough for CSS, JS, and SVG files?

Gzip works everywhere as a fallback, but Brotli (level 5-6 dynamic or level 11 pre-compressed at build time) produces meaningfully smaller text assets over HTTPS.

03 — Have an engineering need?

hire the right expertise

Let's talk tech.

Deciding between an embedded forward deployed engineer or a senior full stack developer? Share your technical context and timeline.

Solitaire Corporate Park, Makarba, Ahmedabad, Gujarat 380015, India · IST (UTC+5:30) · --:-- IST · Mon–Fri 09:00–18:00 IST · US & EU overlap daily

No newsletter, no CRM. Just a reply.