<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: 方振宇</title>
    <description>The latest articles on DEV Community by 方振宇 (@final_god).</description>
    <link>https://dev.to/final_god</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4242064%2Fc3d2e312-f980-49ee-990f-811aae8bfebb.png</url>
      <title>DEV Community: 方振宇</title>
      <link>https://dev.to/final_god</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/final_god"/>
    <language>en</language>
    <item>
      <title>When &lt;img&gt; Hover Previews Break fetch(): A Subtle Browser CORS Cache Bug (and 2 Other Image UI Fixes)</title>
      <dc:creator>方振宇</dc:creator>
      <pubDate>Tue, 01 Dec 2026 09:50:12 +0000</pubDate>
      <link>https://dev.to/final_god/when-hover-previews-break-fetch-a-subtle-browser-cors-cache-bug-and-2-other-image-ui-dbe</link>
      <guid>https://dev.to/final_god/when-hover-previews-break-fetch-a-subtle-browser-cors-cache-bug-and-2-other-image-ui-dbe</guid>
      <description>&lt;p&gt;While rebuilding the frontend of &lt;a href="https://ai-image-to-image.io" rel="noopener noreferrer"&gt;AI Image to Image&lt;/a&gt; — 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.&lt;/p&gt;

&lt;p&gt;Here are the three concrete fixes that made the biggest difference.&lt;/p&gt;




&lt;h2&gt;
  
  
  1. The &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; Hover Preview vs. &lt;code&gt;fetch()&lt;/code&gt; CORS Cache Bug
&lt;/h2&gt;

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

&lt;p&gt;Everything worked on first click — &lt;strong&gt;unless you hovered the card first&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;If you hovered first, the click handler failed with a CORS error, even though the CDN clearly sent &lt;code&gt;Access-Control-Allow-Origin: *&lt;/code&gt;.&lt;/p&gt;

&lt;h3&gt;
  
  
  Why it happened
&lt;/h3&gt;

&lt;ol&gt;
&lt;li&gt;The hover preview rendered a plain &lt;code&gt;&amp;lt;img src="https://img.example.com/sample.webp"&amp;gt;&lt;/code&gt; &lt;strong&gt;without&lt;/strong&gt; &lt;code&gt;crossorigin="anonymous"&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;Because it was a standard &lt;code&gt;no-cors&lt;/code&gt; image request, the browser cached the response &lt;em&gt;without&lt;/em&gt; CORS headers attached to the cache entry.&lt;/li&gt;
&lt;li&gt;Moments later, &lt;code&gt;fetch(url)&lt;/code&gt; (which defaults to &lt;code&gt;mode: "cors"&lt;/code&gt;) hit that exact browser cache entry. Since the cached entry lacked CORS permission, the browser rejected the &lt;code&gt;fetch()&lt;/code&gt; call.&lt;/li&gt;
&lt;/ol&gt;

&lt;h3&gt;
  
  
  The fix
&lt;/h3&gt;

&lt;p&gt;Two small changes solved it completely:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Add &lt;code&gt;crossorigin="anonymous"&lt;/code&gt; to preview &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; elements if the same URL will be fetched via JS later.&lt;/li&gt;
&lt;li&gt;Bypass the HTTP cache when loading an example into a &lt;code&gt;File&lt;/code&gt; via &lt;code&gt;fetch()&lt;/code&gt;:
&lt;/li&gt;
&lt;/ul&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="k"&gt;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;loadExampleAsFile&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nb"&gt;fetch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;url&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;mode&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'cors'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
    &lt;span class="n"&gt;cache&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="s1"&gt;'no-store'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="c1"&gt;// avoids hitting a non-CORS &amp;lt;img&amp;gt; cache entry&lt;/span&gt;
  &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;ok&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;throw&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nb"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sb"&gt;`Failed to load example: ${res.status}`&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="n"&gt;blob&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="n"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;File&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;filename&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="kd"&gt;type&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="n"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="kd"&gt;type&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="s1"&gt;'image/webp'&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;If your app mixes &lt;code&gt;&amp;lt;img src="..."&amp;gt;&lt;/code&gt; previews with &lt;code&gt;fetch()&lt;/code&gt; or &lt;code&gt;&amp;lt;canvas&amp;gt;&lt;/code&gt; reads on the same CDN assets, this one is easy to miss during local testing and annoying in production.&lt;/p&gt;




&lt;h2&gt;
  
  
  2. Don't Set &lt;code&gt;fetchpriority="high"&lt;/code&gt; on Both Sides of a Before/After Slider
&lt;/h2&gt;

&lt;p&gt;Our hero section has an interactive before/after comparison slider. Initially, we put &lt;code&gt;fetchpriority="high"&lt;/code&gt; on &lt;strong&gt;both&lt;/strong&gt; the "before" and "after" images, and we hadn't added a &lt;code&gt;&amp;lt;link rel="preconnect"&amp;gt;&lt;/code&gt; for the image CDN subdomain.&lt;/p&gt;

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

&lt;h3&gt;
  
  
  What we changed
&lt;/h3&gt;

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

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight html"&gt;&lt;code&gt;&lt;span class="c"&gt;&amp;lt;!-- In &amp;lt;head&amp;gt; --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;link&lt;/span&gt; &lt;span class="na"&gt;rel&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"preconnect"&lt;/span&gt; &lt;span class="na"&gt;href&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"https://img.ai-image-to-image.io"&lt;/span&gt; &lt;span class="na"&gt;crossorigin&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;

&lt;span class="c"&gt;&amp;lt;!-- Hero comparison slider --&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt;
  &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"https://img.ai-image-to-image.io/after-1024.webp"&lt;/span&gt;
  &lt;span class="na"&gt;srcset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"... 320w, ... 640w, ... 1024w"&lt;/span&gt;
  &lt;span class="na"&gt;sizes&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"(max-width: 768px) 100vw, 640px"&lt;/span&gt;
  &lt;span class="na"&gt;fetchpriority&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"high"&lt;/span&gt;
  &lt;span class="na"&gt;alt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Transformed result"&lt;/span&gt;
&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;img&lt;/span&gt;
  &lt;span class="na"&gt;src&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"https://img.ai-image-to-image.io/before-1024.webp"&lt;/span&gt;
  &lt;span class="na"&gt;srcset&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"... 320w, ... 640w, ... 1024w"&lt;/span&gt;
  &lt;span class="na"&gt;sizes&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"(max-width: 768px) 100vw, 640px"&lt;/span&gt;
  &lt;span class="na"&gt;decoding&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"async"&lt;/span&gt;
  &lt;span class="na"&gt;alt&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;"Original photo"&lt;/span&gt;
&lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;






&lt;h2&gt;
  
  
  3. Routing Multiple Image-to-Image Models Without Bloating the UI
&lt;/h2&gt;

&lt;p&gt;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 &lt;code&gt;image_urls&lt;/code&gt; vs. &lt;code&gt;image_input&lt;/code&gt;) and let the client dynamically update resolution selectors and credit badges from that config.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;




&lt;p&gt;If you want to inspect the before/after slider or test the image-to-image flow in action, you can try it directly on &lt;a href="https://ai-image-to-image.io" rel="noopener noreferrer"&gt;ai-image-to-image.io&lt;/a&gt;. Happy to hear if anyone else has run into weird &lt;code&gt;&amp;lt;img&amp;gt;&lt;/code&gt; + &lt;code&gt;fetch()&lt;/code&gt; CORS cache collisions!&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>javascript</category>
      <category>performance</category>
      <category>frontend</category>
    </item>
  </channel>
</rss>
