Dev.to WebDev 🛠 Dev 👁 2 📖 3 min read

When Hover Previews Break fetch(): A Subtle Browser CORS Cache Bug (and 2 Other Image UI Fixes)

While rebuilding the frontend of AI Image to Image — a lightweight image-to-image editor that lets users upload a photo or click built-in before/after examples — we ran into a surprisingly subtle browser caching bug, plu

While rebuilding the frontend of AI Image to Image — a lightweight image-to-image editor that lets users upload a photo or click built-in before/after examples — we ran into a surprisingly subtle browser caching bug, plus a couple of image-loading issues that quietly hurt LCP.

Here are the three concrete fixes that made the biggest difference.

1. The <img> Hover Preview vs. fetch() CORS Cache Bug

Our homepage has a row of example cards. When you hover over a card, it previews the transformed image in an <img> tag served from our image CDN (img.ai-image-to-image.io, which returns Access-Control-Allow-Origin: *). When you click the card ("Try this photo"), JavaScript calls fetch(url) to turn that image into a File object and load it into the editor.

Everything worked on first click — unless you hovered the card first.

If you hovered first, the click handler failed with a CORS error, even though the CDN clearly sent Access-Control-Allow-Origin: *.

Why it happened

  1. The hover preview rendered a plain <img src="https://img.example.com/sample.webp"> without crossorigin="anonymous".
  2. Because it was a standard no-cors image request, the browser cached the response without CORS headers attached to the cache entry.
  3. Moments later, fetch(url) (which defaults to mode: "cors") hit that exact browser cache entry. Since the cached entry lacked CORS permission, the browser rejected the fetch() call.

The fix

Two small changes solved it completely:

  • Add crossorigin="anonymous" to preview <img> elements if the same URL will be fetched via JS later.
  • Bypass the HTTP cache when loading an example into a File via fetch():
async function loadExampleAsFile(url, filename) {
  const res = await fetch(url, {
    mode: 'cors',
    cache: 'no-store', // avoids hitting a non-CORS <img> cache entry
  });
  if (!res.ok) throw new Error(`Failed to load example: ${res.status}`);
  const blob = await res.blob();
  return new File([blob], filename, { type: blob.type || 'image/webp' });
}

If your app mixes <img src="..."> previews with fetch() or <canvas> reads on the same CDN assets, this one is easy to miss during local testing and annoying in production.

2. Don't Set fetchpriority="high" on Both Sides of a Before/After Slider

Our hero section has an interactive before/after comparison slider. Initially, we put fetchpriority="high" on both the "before" and "after" images, and we hadn't added a <link rel="preconnect"> for the image CDN subdomain.

On high-DPI mobile screens, the browser immediately requested both 1536w responsive WebP images in parallel (~266 KB combined) before finishing the critical render path, pushing our homepage LCP p75 above 4 seconds.

What we changed

  1. Added <link rel="preconnect" href="https://img.ai-image-to-image.io" crossorigin> in <head> so TLS negotiation to the image CDN starts early.
  2. Kept fetchpriority="high" only on the primary visible layer (the full-bleed "after" image) and let the overlaid "before" image load at normal priority.
  3. Generated tight responsive WebP widths (320w, 640w, 1024w, 1248w) so mobile devices don't pull desktop-sized PNGs.
<!-- In <head> -->
<link rel="preconnect" href="https://img.ai-image-to-image.io" crossorigin />

<!-- Hero comparison slider -->
<img
  src="https://img.ai-image-to-image.io/after-1024.webp"
  srcset="... 320w, ... 640w, ... 1024w"
  sizes="(max-width: 768px) 100vw, 640px"
  fetchpriority="high"
  alt="Transformed result"
/>
<img
  src="https://img.ai-image-to-image.io/before-1024.webp"
  srcset="... 320w, ... 640w, ... 1024w"
  sizes="(max-width: 768px) 100vw, 640px"
  decoding="async"
  alt="Original photo"
/>

3. Routing Multiple Image-to-Image Models Without Bloating the UI

Different image edits need different trade-offs: fast 1K previews vs. sharper 2K/3K portrait or hair edits vs. heavier instruction-following edits. Rather than scattering model-specific logic across separate pages, we keep a single declarative model config (resolution options, credit cost per variant, and payload field mapping like image_urls vs. image_input) and let the client dynamically update resolution selectors and credit badges from that config.

That way, adding a new backend model (for example, adding Seedream 5.5 alongside FLUX 3 Klein and GPT Image) or spinning up a focused tool page like a photo-to-line-art converter doesn't require duplicating form handlers or credit checks.

If you want to inspect the before/after slider or test the image-to-image flow in action, you can try it directly on ai-image-to-image.io. Happy to hear if anyone else has run into weird <img> + fetch() CORS cache collisions!

📰 Read the original article on Dev.to WebDev

Originally published by Dev.to WebDev. Aggregated on AIWithGhost for educational purposes — full credit and traffic to the original publisher.