Don't Let Images Sink Your Page: Image Optimization in Practice for Your Vibe Project
Format choices, responsive images, lazy loading and placeholders, Sharp pipelines, CDN and cache headers: a fictional same-photo comparison from PNG 4,800KB to AVIF 260KB, a 12-image page cut from 7.4MB and 11.9s to 0.76MB and 1.2s, plus a 12-item checklist.

Two things up front, so you don't read the wrong guide. First, this guide is not about caching strategy — the site already has "Vibe Project Caching Strategy," which covers the full caching approach from browsers to AI results; read that one for caching. This guide mentions cache headers for images in a single line only and does not go further. Second, this guide is not about offline-first and PWA — the site already has "Adding Offline-First and PWA Ability to Your Vibe Project," which covers whether a product still works with no connection; this guide is only about images themselves: which format to pick, how to size them, how to load them, and how to compress them. The whole guide uses one fictional example to make the ideas concrete: the example product is "Mountain Wind Album," a small app that lets users upload travel photos and turn them into albums by route; the example author is "Xiao Bei," an office worker who likes hiking and photography; the numbers, conversations, and scenes in the example are entirely fictional exercises, written to make the ideas concrete, not a real product or real events. The format comparison table and the cost math later in the guide are also fictional samples, used to do the math, not measured benchmarks.
How one 4.8MB original photo wrecked a page
In the first week after Xiao Bei's album app launched, the feedback from friends was strikingly consistent: the photos were beautiful, but the page would not open. On a phone, opening an album meant watching a spinner for more than ten seconds while photos trickled out one by one, and sometimes, halfway down the page, the whole page would jump and push the photo being viewed off the screen. On his own computer it had tested fast — broadband downloaded the whole page in a second.
The problem was one photo. User-uploaded travel photos arrived as 2400×1600 PNG originals, 4.8MB each, and an album page carried 12 of them, sent to the browser with nothing compressed at all. The hero image on the home page was also an untouched original. Opening it on mobile data meant downloading more than 7MB before the page could be seen, and slower still on a weak connection. Worse, the image tags carried no width or height, so the browser had no idea how much space each image would take; the moment an image arrived, it shoved the text below it downward and the page kept jumping.
The first lesson of image optimization is this: the image a browser wants and the image a camera produces are not the same thing. A camera wants to keep as much detail as possible; a browser wants the picture to look clear enough at the current screen size while staying as small as possible. Your job is to build a bridge between the two: cut the same photo into different sizes and formats for each use, make small things small, lazy-load what can wait, and reserve space in advance. Once that lesson was applied, Xiao Bei's album page went from 7.4MB to 0.76MB, and loading time from 11.9 seconds to 1.2 seconds. The numbers are a fictional sample; the cost section later in this guide shows the math step by step.
Choosing a format: five formats, one comparison table
Before picking a format, learn the temperament of the five common ones. JPEG is the veteran, suited to photos: lossy compression with a good balance between size and quality, but no transparency. PNG is the first choice for transparency and sharp lines — icons, screenshots, images with transparent backgrounds — but photos stored in it come out absurdly large. WebP is the modern general-purpose format: smaller than JPEG for photos, cheaper than PNG for transparency, able to do animation too, and browser support is already broad. AVIF is the newest: it compresses photos smaller than WebP, but encodes slowly and has slightly weaker support on older devices. SVG is vector graphics, suited to icons and simple illustrations: it scales without losing sharpness, and its size depends only on how complex the shapes are — it has nothing to do with photos.
Xiao Bei ran one comparison on the same 2400×1600 travel photo, and the numbers are a fictional sample used to show the order of magnitude: stored as PNG it is 4800KB, as JPEG at quality 82 it is 620KB, as WebP at quality 80 it is 380KB, and as AVIF at quality 62 it is 260KB. Do the math: JPEG is only 12.9% of the PNG, WebP is 61.3% of the JPEG, AVIF is 68.4% of the WebP, and AVIF is only 5.4% of the PNG — switching from PNG to AVIF saves 4540KB, or 94.6%. The point of this set of numbers is not that "AVIF is always best," but that "picking the wrong format can change the size by an order of magnitude."
The selection rules can be written as four lines. First, use WebP or AVIF for photos, with JPEG as the fallback: serve WebP or AVIF to modern browsers and fall back to JPEG for older ones. Second, use SVG for icons, logos, and simple illustrations — never cut them as PNGs. Third, use WebP or PNG for photographic images that need transparency, and PNG is also fine for small flat-color transparent images. Fourth, use PNG for screenshots and images containing text, because JPEG's lossy compression blurs text edges. Format is not a matter of newer always being better; it depends on the content. Photos, transparency, and vectors are three kinds of content with three paths, and picking the wrong path cannot be rescued by tuning parameters afterwards.
One more detail: a higher quality setting is not always better. JPEG at quality 82 and quality 95 look almost identical, but the file size can double; the same holds for WebP at quality 80 versus 90. The way to tune is to take one representative photo, test from low to high, find the point where "any lower looks visibly blurry, any higher only adds size for nothing," and set that point as the team's default in the build script instead of tuning every image by feel.
Responsive images: stop sending phones desktop-sized pictures
A 2400-pixel-wide image displayed at 400 pixels wide on a phone wastes the other 2000 pixels. Responsive images fix exactly this: prepare several widths of the same photo and let the browser pick one based on screen size.
The method is srcset plus sizes. Srcset lists several versions and their real widths — for example 480w, 800w, 1200w, and 1920w; sizes tells the browser how wide the image will be on the page, such as full screen (100vw) on a phone and 800 pixels on a desktop. Holding those two pieces of information, plus the screen's pixel density, the browser picks the most suitable version to download. Three things matter when writing them. First, the w descriptor is the file's real pixel width, not its display width — write it wrong and the browser picks the wrong version. Second, sizes must match the real layout: writing 100vw when the image actually takes up only half the page makes the browser download a picture twice as big as needed. Third, always keep an ordinary src as a fallback for older browsers that do not support srcset.
The picture element is a different tool, for when "different screens need different pictures," not just "the same picture at different sizes." Use it when a phone needs a vertical crop and a desktop needs the full horizontal composition, or when you want to serve AVIF to modern browsers and JPEG to older ones: wrap several source elements in a picture and keep an img as the fallback. Do not wrap every image in picture: for the same image at several resolutions, srcset is enough. Picture is reserved for changing the content, the crop, or the format.
That is how Xiao Bei changed his album pages: every photo is generated in four widths — 480, 800, 1200, and 1920 — phones take the 480 or 800 version, desktops take the 1200 or 1920 version, and thumbnails get a separate 400-pixel-wide version. After the change, the volume downloaded by phone users dropped by two thirds. The page was the same page, and the photos looked no different at all.
Lazy loading and placeholders: show something first, fill in the pictures slowly
An album page has 12 pictures, but users can only see the top two or three at first glance; the rest can wait until they scroll down. That is lazy loading: add loading="lazy" to images below the fold, and the browser only downloads them when they are about to come into view. Never lazy-load first-screen images: the hero image and the first photo must load immediately, and you can even use fetchpriority="high" to tell the browser to fetch them first. Lazy-loading a first-screen image, on the other hand, makes users stare at blank space — a serious mistake.
The other half of lazy loading is placeholders. Before an image arrives, the page should already reserve its space; otherwise, when the image lands, it shoves the content below it downward and the page jumps. There are three ways to reserve space. The first is to write fixed width and height attributes: with width and height on the img tag, the browser can calculate the space from the ratio before the image arrives — the simplest and most reliable method. The second is CSS aspect-ratio, writing the ratio in the stylesheet, which works the same way and suits cases where size is controlled by styles. The third is the blur-up placeholder: send a tiny blurred version of a few kilobytes first, then fade in the sharp image when it arrives. It gives the best user experience but is the most complex to build; a small site should get the first two right before anything else.
One detail is easy to miss: lazy-loaded images need dimensions too. Many people leave width and height off lazy images, thinking the space does not matter since the image has not loaded yet — and then, when the user scrolls down, the image arrives and the page still jumps. The rule is one line: whether lazy or eager, every image gets dimensions. Reserving space is standard equipment for image optimization, not an option.
Compression pipelines: Sharp and Squoosh, build time versus run time
Compressing images one by one by hand is not sustainable. Image optimization has to become a pipeline: as soon as an image comes in, every size and format it needs is generated automatically and stored where it belongs. There are two routes: compress at build time or at run time.
Build-time compression suits sites where images are fixed in number and do not change often: on every release, a compression script runs over new images, generating each size and format, and deploys them with the static files. Sharp is the most common tool for this route. It is a Node.js image processing library built on libvips, and it is fast — one command turns an original into several sizes and formats. Its official documentation states the typical use plainly: converting large images into smaller, web-friendly JPEG, PNG, WebP, GIF, and AVIF images of varying dimensions. Using it is simple: read the original, resize, pick a format and quality, and write the file out. Xiao Bei's build script works exactly like this: on every release, it runs over the originals in public/images, generating four widths — 480, 800, 1200, and 1920 — with WebP and AVIF versions at each width, and keeping one JPEG as a fallback.
Run-time compression suits sites where users upload images: the server compresses on receipt, or an image CDN generates the requested size and format on demand from the URL parameters. Squoosh is a compression tool that runs in the browser, suited to processing a small number of images at once or to letting editors compress images in an admin panel; an image CDN suits sites with heavy traffic and many images, where adding parameters to the address returns any size and format while the origin stores only one original.
Choosing between the two routes is simple: fixed images in small numbers mean build-time compression — simple and reliable; user uploads in uncertain numbers mean run-time compression or an image CDN. Build-time compression is the starting point; add run-time compression once that works. One baseline holds for both: originals are never sent directly to the browser. Whichever route you take, the browser should only ever receive compressed versions.
CDN and cache headers: deliver images fast, and remember them
Once images are small, they still need to arrive fast. A content delivery network (CDN) puts images on nodes close to the user, so downloads do not cross half the planet; for image-heavy sites the effect is obvious. A small site does not need a dedicated image CDN from day one — putting images and pages behind the same CDN already saves most of the round trips. When image traffic genuinely grows, consider a dedicated image CDN that serves sizes and formats dynamically from request parameters.
Cache headers deserve one line of practice here, not a full strategy: name image files with a content hash, so an address only changes when the file does, and set a long cache lifetime — a year, for example — because a changed filename is a new file and an old cache will never block a new image; images without a hash in the filename should not get a long cache, or users will keep seeing the old picture after you replace it. For the full caching strategy, read the site's published "Vibe Project Caching Strategy" guide; this guide only states this one line: image cache headers follow the filename hash.
Six mistakes AI-generated images keep making
An AI helper generating pages fails most easily on images, because it aims for "the page has a picture," not "the picture is served correctly." Xiao Bei stepped on all six of the mistakes below; you can check them one by one.
First, serving originals directly. AI-generated illustrations and user-uploaded photos go into the page uncompressed and unresized, straight from the original address — a 4.8MB PNG delivered to a phone user. Fix: put every image through the pipeline, compress it and cut it to size before serving; originals are for archiving, not for pages.
Second, an oversized hero image. The home page hero uses a 2400-pixel-wide original, and first-screen loading depends entirely on it. Fix: cut the hero to the largest width it will ever display — if the desktop never shows more than 1920 pixels, serve only the 1920 version — and compress it under 200KB.
Third, no dimension attributes. Image tags without width and height make page content jump up and down while loading. Fix: write width and height on every image, or reserve the space with aspect-ratio, as described in the earlier section.
Fourth, one size for everything. All images across the site use a single version, so phones and desktops download the same large file. Fix: prepare several widths with srcset, and generate thumbnails separately from large images.
Fifth, one format for everything. Photos saved as PNG, icons as JPEG, transparent images as JPEG with the background turning black. Fix: choose the format by content — photos in WebP or AVIF, icons in SVG, screenshots in PNG.
Sixth, alt text that is empty everywhere or says "image" everywhere. Fix: content images get one descriptive sentence saying what is in the picture; only decorative images get an empty alt so screen readers skip them.
Doing the cost math: volume and time for a 12-image page
Whether image optimization is worth it becomes clear once you do the math. Mountain Wind Album's album page has 12 photos; take that page as the example, and the numbers are a fictional sample used to show how the math works. Before optimization, all 12 pictures were JPEG quality 82 versions at 620KB each — 12 times 620KB is 7440KB, or 7.4MB. After optimization, the hero image became an AVIF at 260KB, and the remaining 11 thumbnails were 45KB each — 11 times 45KB is 495KB — for a total of 755KB, about 0.76MB. From 7.4MB to 0.76MB, the saving is 6.685MB, and the volume is 89.9% smaller.
Now the time. On a 5Mbps mobile connection, 5Mbps converts to a download speed of 0.625MB per second. Before optimization, 7.44MB divided by 0.625MB per second takes 11.9 seconds to download; after optimization, 0.755MB divided by 0.625MB per second takes 1.2 seconds. Same page, same photos, loading time from 11.9 seconds down to 1.2 seconds — 10.7 seconds saved. A page that takes 11.9 seconds is one users close; a page that takes 1.2 seconds is one users stay to look at. That is the return on image optimization: it is not just a technical metric; it decides directly whether users stay.
Two numbers in this math deserve a separate mention. First, thumbnails are where most of the saving lives: 11 thumbnails going from 620KB each to 45KB each saves 575KB per image, 6325KB across 11 images — the great majority of the total saving. List pages and thumbnail walls, where many images appear at once, should compress thumbnails first for the highest return. Second, the hero image is the key to saving time: it is the first thing seen on the first screen and the main subject of Largest Contentful Paint, so moving it from a 620KB JPEG to a 260KB AVIF cuts first-screen time by a large step at once.

Launch acceptance checklist: tick every item before shipping pictures
The checklist below is what Xiao Bei now runs through before publishing any page with pictures. Twelve items, five minutes to check, checked before anything goes live.
First, no original image is sent directly to the browser; every image has been through the compression pipeline. Second, photos use WebP or AVIF with one JPEG kept as a fallback; icons use SVG and screenshots use PNG. Third, every photo has four width versions — 480, 800, 1200, and 1920 — or at least a "phone version" and a "desktop version." Fourth, srcset w descriptors match the files' real widths, and sizes matches the page layout. Fifth, first-screen images have no lazy loading, and images below the fold carry loading="lazy." Sixth, every image carries width and height, or reserves its space with aspect-ratio. Seventh, alt text describes the content, and decorative images use an empty alt. Eighth, the hero image is compressed under 200KB and first-screen time has been measured. Ninth, image filenames carry a content hash and cache lifetimes are set long enough. Tenth, the image CDN or static hosting is confirmed, and image addresses open directly. Eleventh, open the page once on mobile data, scroll to the bottom, and confirm every image loads and the page does not jump. Twelfth, check total image volume once a month, and compress thumbnails first when it passes the budget.
The list does not need a perfect score on the first try. A new site that manages the first six items is already ahead of most vibe projects; the last six can be added slowly over the first month after launch. The habit is what matters: run the checklist every time a page with pictures goes out, instead of remembering to check three months after launch.
Start with the smallest image loop
If you want to start now, do not build the full pipeline first. Build the smallest image loop: pick the heaviest page, compress all its images, cut two sizes, add width, height, and alt text, then open it once on mobile data and see how much time came off. Mountain Wind Album's first image loop had only three tasks: compress the album page's 12 pictures into WebP, cut thumbnails to 400 pixels wide, and add width and height attributes to every image. Three tasks, one evening, and page volume fell from 7.4MB to under 1MB, loading time from 11.9 seconds to under 2 seconds. AVIF, srcset, and an image CDN were added gradually after that worked.
Back to Xiao Bei. Every photo he uploads now automatically generates four sizes and three formats through the pipeline, and he no longer has to think about images. His album pages never trended, but friends who open them no longer complain that they will not load — they start praising the photos. That is the best return image optimization can give a vibe project: users can finally see the content itself, instead of waiting for it to load.
Comments (0)
Sources
Related articles
One page should stand up on a phone, a tablet, and a computer. This guide follows MDN's Responsive web design, Using media queries, and the viewport meta reference: fluid HTML, the viewport tag, fluid grids and max-width, images that never burst their container, media query syntax, breakpoints set by content, mobile first, responsive type, testing capability instead of width, and a pre-launch checklist.
localStorage only stores strings and has no indexes. IndexedDB is a real database in the browser: structured data, far larger capacity, indexes, and transactions. Following MDN's Using IndexedDB, IDBDatabase, and IDBObjectStore: the five-step routine, opening, upgrades, keys, indexes, transactions, add versus put, get, cursors, ranges, and multi-tab upgrades.
You already call functions every day; this guide names what you have been doing. Following MDN's Functions, Arrow function expressions, and Rest parameters: declarations versus expressions, parameters and arguments, default values, rest parameters, spread syntax, return values, arrow function shorthand, the this trap, callbacks, and scope.