<?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: Zhang Qiang</title>
    <description>The latest articles on DEV Community by Zhang Qiang (@zq4034698).</description>
    <link>https://dev.to/zq4034698</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%2F4202258%2Fcb05cfb1-ae95-428f-be84-a2088c0c23fc.png</url>
      <title>DEV Community: Zhang Qiang</title>
      <link>https://dev.to/zq4034698</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/zq4034698"/>
    <language>en</language>
    <item>
      <title>Your Website Is Slow and You Don't Know Why: A 10-Minute Triage Checklist</title>
      <dc:creator>Zhang Qiang</dc:creator>
      <pubDate>Mon, 09 Nov 2026 01:51:20 +0000</pubDate>
      <link>https://dev.to/zq4034698/your-website-is-slow-and-you-dont-know-why-a-10-minute-triage-checklist-4276</link>
      <guid>https://dev.to/zq4034698/your-website-is-slow-and-you-dont-know-why-a-10-minute-triage-checklist-4276</guid>
      <description>&lt;p&gt;Your website feels slow. Users complain, bounce rates climb, and every "speed up your site" guide throws twenty things at you at once. Before you rewrite anything, spend ten minutes triaging. Most slow sites have one dominant cause, and this checklist finds it fast.&lt;/p&gt;

&lt;h2&gt;
  
  
  Minute 0–2: Measure first, guess never
&lt;/h2&gt;

&lt;p&gt;Open your site in an incognito window and run a quick test with your browser's built-in tools (F12 → Network tab, throttled to "Fast 4G" if you want realistic mobile numbers) or any free speed test. Write down three numbers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Time to First Byte (TTFB)&lt;/strong&gt; — how long before the server responds at all.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Largest Contentful Paint (LCP)&lt;/strong&gt; — when the main content appears.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Total page weight&lt;/strong&gt; — how many megabytes the browser downloads.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If TTFB is over 600ms, the problem is server-side: slow hosting, no caching, or a database query doing too much work. Skip the image-optimizing guides — they won't help you yet. If TTFB is fine but LCP is slow, the problem is on the page itself. That one distinction saves you hours.&lt;/p&gt;

&lt;h2&gt;
  
  
  Minute 2–4: Find the heavyweight
&lt;/h2&gt;

&lt;p&gt;In the Network tab, sort by size. On most slow sites, the top three entries tell the whole story:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;One giant hero image.&lt;/strong&gt; A 4MB PNG where a 200KB WebP would do. Resize it to the dimensions it actually displays at, convert it, and you just cut seconds.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A video autoplaying in the background.&lt;/strong&gt; Replace it with a poster image and a click-to-play button. Nobody asked for the video; everybody pays for it.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Third-party scripts.&lt;/strong&gt; Chat widgets, analytics, ad tags, social embeds. Each one is a separate request to a server you don't control. Disable them one at a time and re-test — the culprit usually reveals itself immediately.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;A good rule of thumb: if your page downloads more than 2MB on first load, something on it doesn't need to be there yet.&lt;/p&gt;

&lt;h2&gt;
  
  
  Minute 4–6: Check what blocks rendering
&lt;/h2&gt;

&lt;p&gt;Still slow with a light page? Look at the waterfall for render-blocking resources: CSS and JavaScript files the browser must download and process before it can paint anything.&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;CSS:&lt;/strong&gt; One big stylesheet in the &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; blocks everything. Split off the critical above-the-fold styles inline and load the rest after.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;JavaScript:&lt;/strong&gt; Any script tag without &lt;code&gt;defer&lt;/code&gt; or &lt;code&gt;async&lt;/code&gt; stops the page from rendering while it downloads. Add &lt;code&gt;defer&lt;/code&gt; to anything that doesn't need to run before the page appears.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Web fonts:&lt;/strong&gt; A font that loads late causes a flash of invisible text. Use &lt;code&gt;font-display: swap&lt;/code&gt; so the fallback font shows immediately.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;You don't need to understand all of this deeply — just look for long bars at the start of the waterfall and ask whether each one earns its place.&lt;/p&gt;

&lt;h2&gt;
  
  
  Minute 6–8: Rule out the server
&lt;/h2&gt;

&lt;p&gt;If TTFB was the problem, check three server-side things:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Caching.&lt;/strong&gt; Is your server sending cache headers for static assets? A returning visitor should barely download anything.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Compression.&lt;/strong&gt; Brotli or gzip should be on. An uncompressed HTML file is often 3–4x larger than it needs to be.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Location.&lt;/strong&gt; If your server is in Frankfurt and your users are in Singapore, physics is your bottleneck. A CDN puts copies near your visitors.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;Shared hosting with a slow database is the most common version of this story. A quick test: load a plain static HTML file from the same server. If that's fast and your real pages aren't, the server hardware is fine — your application is the problem.&lt;/p&gt;

&lt;h2&gt;
  
  
  Minute 8–10: Write down the one fix
&lt;/h2&gt;

&lt;p&gt;You now know which of these is true:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The server is slow → fix hosting, caching, or the database.&lt;/li&gt;
&lt;li&gt;The page is heavy → compress the top three assets.&lt;/li&gt;
&lt;li&gt;Rendering is blocked → defer scripts, split CSS.&lt;/li&gt;
&lt;li&gt;Third parties are the problem → remove or lazy-load them.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Pick the single biggest one and fix it first. Re-run the test. If the number moves, you found it. If it doesn't, you just eliminated a suspect in ten minutes instead of a weekend.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why triage beats optimization
&lt;/h2&gt;

&lt;p&gt;Optimization guides assume you already know what's slow. Most people don't — they guess, usually wrong, and spend a day minifying files on a site whose real problem is a 4MB hero image or a database query with no index. Measure, sort by size, and let the waterfall tell you where the seconds go.&lt;/p&gt;

&lt;p&gt;I keep a longer version of this checklist, plus hands-on troubleshooting walkthroughs for servers, networks, and everyday IT problems, on my knowledge base at &lt;a href="https://targetkb.com/" rel="noopener noreferrer"&gt;TargetKB&lt;/a&gt; — practical notes from real incidents, written for people who have to fix things, not just read about them.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>performance</category>
      <category>tutorial</category>
      <category>beginners</category>
    </item>
    <item>
      <title>Stop Uploading User Photos to Your Server: Hit Exact File-Size Limits in the Browser</title>
      <dc:creator>Zhang Qiang</dc:creator>
      <pubDate>Sun, 01 Nov 2026 13:00:35 +0000</pubDate>
      <link>https://dev.to/zq4034698/stop-uploading-user-photos-to-your-server-hit-exact-file-size-limits-in-the-browser-4pg4</link>
      <guid>https://dev.to/zq4034698/stop-uploading-user-photos-to-your-server-hit-exact-file-size-limits-in-the-browser-4pg4</guid>
      <description>&lt;p&gt;Every developer has met this form validation message: "Photo must be under 200KB." "Signature: max 50KB." The usual fix is to send the user's photo to the server, resize it there, and send it back. It works — but it also means a stranger's ID photo, signature, or profile picture just travelled to your infrastructure and sat in a temp folder so you could shrink it.&lt;/p&gt;

&lt;p&gt;There is a better default: do the whole job in the browser. The file never leaves the device, and you still hit the exact size limit. Here's how the technique works, and the edge cases that will bite you if you skip them.&lt;/p&gt;

&lt;h2&gt;
  
  
  The problem: a quality slider is not a file-size control
&lt;/h2&gt;

&lt;p&gt;The browser gives you one knob for JPEG output, &lt;code&gt;canvas.toBlob()&lt;/code&gt;'s &lt;code&gt;quality&lt;/code&gt; parameter (0 to 1). The naive approach is "keep lowering quality until it fits." Two things make that painful:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Size does not move linearly with quality.&lt;/strong&gt; Dropping from 0.9 to 0.8 can save hundreds of kilobytes; dropping from 0.4 to 0.3 might save almost nothing. Fixed steps waste encodes or overshoot.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Some targets are unreachable by quality alone.&lt;/strong&gt; A 4000 x 3000 phone photo will not fit into 20KB at any quality. At some point you must reduce the pixel count.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;So the algorithm needs two loops: search quality first, shrink dimensions only when quality can't do the job.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 1: Search quality, keep the best fit
&lt;/h2&gt;

&lt;p&gt;Binary search beats stepping. Seven iterations narrow quality to well under 0.01 — finer than any visible difference — and you keep the &lt;em&gt;largest&lt;/em&gt; file that still fits, not the first one that fits. That matters when a human reviews the photo: every byte you're allowed to keep is detail in someone's face.&lt;br&gt;
&lt;/p&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;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;quality&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;Promise&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toBlob&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;
      &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blob&lt;/span&gt; &lt;span class="p"&gt;?&lt;/span&gt; &lt;span class="nf"&gt;resolve&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nf"&gt;reject&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;encode failed&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;))),&lt;/span&gt;
      &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;image/jpeg&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="nx"&gt;quality&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;async&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;fitQuality&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;maxBytes&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="nx"&gt;MIN_Q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;MAX_Q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mf"&gt;0.95&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="c1"&gt;// If even the lowest quality is too big, quality alone can't fix it.&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;smallest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;MIN_Q&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="nx"&gt;smallest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;maxBytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="kc"&gt;null&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;MIN_Q&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;MAX_Q&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;best&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;smallest&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;++&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="nx"&gt;q&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;hi&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&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="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;q&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="nx"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="nx"&gt;maxBytes&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;best&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;blob&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;lo&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;q&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;
    &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;hi&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;q&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="nx"&gt;best&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;One detail people get wrong: is 1KB 1000 or 1024 bytes? Forms rarely say. Using 1000 passes either way, so treat the target as &lt;code&gt;targetKB * 1000&lt;/code&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Step 2: Shrink dimensions only when needed
&lt;/h2&gt;

&lt;p&gt;When &lt;code&gt;fitQuality&lt;/code&gt; returns null, estimate the scale factor from how far off you are. JPEG size grows roughly with pixel count, and pixel count grows with the &lt;em&gt;square&lt;/em&gt; of the scale factor. If the smallest encode is 4x too big, scaling each side by sqrt(1/4) = 0.5 is a good first guess:&lt;br&gt;
&lt;/p&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;compressToSize&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;targetKB&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="nx"&gt;maxBytes&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;targetKB&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;bitmap&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;createImageBitmap&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;file&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;imageOrientation&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;from-image&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt; &lt;span class="p"&gt;});&lt;/span&gt;
  &lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;scale&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
  &lt;span class="k"&gt;for &lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;let&lt;/span&gt; &lt;span class="nx"&gt;round&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;round&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;6&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;round&lt;/span&gt;&lt;span class="o"&gt;++&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="nx"&gt;canvas&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nf"&gt;drawScaled&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;bitmap&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;fitted&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;fitQuality&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;maxBytes&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="nx"&gt;fitted&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;fitted&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;smallest&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;await&lt;/span&gt; &lt;span class="nf"&gt;encode&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;canvas&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mf"&gt;0.1&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;ratio&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;sqrt&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;maxBytes&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="nx"&gt;smallest&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;size&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;
    &lt;span class="nx"&gt;scale&lt;/span&gt; &lt;span class="o"&gt;*=&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;max&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;ratio&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mf"&gt;0.95&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="c1"&gt;// safety margin, max half per round&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="nc"&gt;Error&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s2"&gt;`Cannot reach &lt;/span&gt;&lt;span class="p"&gt;${&lt;/span&gt;&lt;span class="nx"&gt;targetKB&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="s2"&gt;KB without destroying the image`&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;In practice this lands in one or two rounds, because the square-root estimate is close.&lt;/p&gt;

&lt;h2&gt;
  
  
  The pitfalls (each one cost me a bug report)
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;EXIF orientation.&lt;/strong&gt; Phone photos are often stored sideways with an EXIF flag. &lt;code&gt;createImageBitmap(file, { imageOrientation: "from-image" })&lt;/code&gt; respects it; without that option, uploads come out rotated.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Transparency turns black.&lt;/strong&gt; JPEG has no alpha channel. Fill the canvas white before drawing, or a transparent signature PNG becomes a black rectangle.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Files that already fit.&lt;/strong&gt; If someone uploads a 15KB JPG and asks for 20KB, re-encoding only makes it worse. Return the original — but check the format still matches, since a 15KB PNG fails a "JPG only" form.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Huge inputs on phones.&lt;/strong&gt; A 48MP photo decodes into a big pixel buffer, and mobile browsers have canvas size limits. Cap the long side (e.g. 2000px) before the search starts.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;No magic quality number.&lt;/strong&gt; Chrome and Safari use different JPEG encoders, so the same quality value yields different sizes. Because the search runs on the user's own encoder, it adapts automatically. Never hard-code a "quality 0.62 works for 100KB" rule.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Don't block the UI.&lt;/strong&gt; Seven to fifteen encodes can freeze a slow phone. Move the work to a Web Worker with &lt;code&gt;OffscreenCanvas&lt;/code&gt; where supported.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  The ops checklist: client-side is not a trust boundary
&lt;/h2&gt;

&lt;p&gt;Doing this in the browser is a privacy and UX win, not a security control. Your server must still:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;
&lt;strong&gt;Re-check the size server-side.&lt;/strong&gt; A crafted request can claim any size.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Validate MIME type by content, not extension.&lt;/strong&gt; Sniff the magic bytes; reject anything outside your allowlist.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Enforce a hard cap at the reverse proxy&lt;/strong&gt; (e.g. &lt;code&gt;client_max_body_size&lt;/code&gt; in nginx) so oversized bodies never reach your app.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Delete originals.&lt;/strong&gt; If any flow still uploads an original, make sure the resized derivative is the only thing that persists.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;One side benefit worth knowing: re-encoding through canvas drops EXIF metadata, including GPS tags, because canvas works with raw pixels. That is a property of the technique, not a reason to skip server-side validation — but it is one more reason to prefer client-side processing for sensitive uploads like ID photos and signatures.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it without writing code
&lt;/h2&gt;

&lt;p&gt;If you just want to see the approach in action, &lt;a href="https://targetkb.com/" rel="noopener noreferrer"&gt;TargetKB's free browser image compressor&lt;/a&gt; does exactly this: type a target in KB or MB and get a file that fits, with the image processed on your own device. No sign-up, no upload.&lt;/p&gt;

&lt;p&gt;What upload limits have you run into? The weird ones — "signature must be under 20KB, JPG only, 140x60 pixels" — are always the most fun to break. Tell me in the comments.&lt;/p&gt;




&lt;p&gt;&lt;em&gt;Zhang Qiang writes practical IT guides at &lt;a href="https://targetkb.com/" rel="noopener noreferrer"&gt;TargetKB&lt;/a&gt; — cybersecurity and ops troubleshooting, plus handy browser tools that keep your data on your own device.&lt;/em&gt;&lt;/p&gt;

</description>
      <category>javascript</category>
      <category>webdev</category>
      <category>security</category>
      <category>tutorial</category>
    </item>
  </channel>
</rss>
