<?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: Toufiqul Hasan</title>
    <description>The latest articles on DEV Community by Toufiqul Hasan (@toufiqul-hasan).</description>
    <link>https://dev.to/toufiqul-hasan</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%2F830711%2F37461d04-745b-4d3c-88c6-23c35142a48c.png</url>
      <title>DEV Community: Toufiqul Hasan</title>
      <link>https://dev.to/toufiqul-hasan</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/toufiqul-hasan"/>
    <language>en</language>
    <item>
      <title>Why I built a 100% client-side image compressor (and what it took)</title>
      <dc:creator>Toufiqul Hasan</dc:creator>
      <pubDate>Wed, 04 Nov 2026 10:28:25 +0000</pubDate>
      <link>https://dev.to/toufiqul-hasan/building-a-100-client-side-image-compressor-ohd</link>
      <guid>https://dev.to/toufiqul-hasan/building-a-100-client-side-image-compressor-ohd</guid>
      <description>&lt;p&gt;As a developer, I do the thing you're supposed to do: I open devtools and watch the network tab. A few months ago I needed a quick image compressor, picked a popular one, and dropped a file in. Sure enough, a POST request with my full-resolution image went straight to some API endpoint.&lt;/p&gt;

&lt;p&gt;The privacy policy said files get deleted after an hour. I had no way to verify that. Neither do you.&lt;/p&gt;

&lt;p&gt;That gap between what a policy promises and what the network tab can prove is what bothered me. So I built the alternative: &lt;a href="https://compressify.org/" rel="noopener noreferrer"&gt;Compressify&lt;/a&gt;, a free image compressor where nothing ever leaves your browser. After the app loads, the only requests you'll see are analytics pings. Your images stay in tab-local memory, get compressed by WebAssembly codecs, and come back as downloads.&lt;/p&gt;

&lt;p&gt;This post is about what it actually took to build that.&lt;/p&gt;

&lt;h2&gt;
  
  
  Standing on Squoosh's shoulders
&lt;/h2&gt;

&lt;p&gt;I didn't write the codecs. Google's Squoosh team already proved browser-side image compression works, and the jSquash project packages those same encoders for the web: AVIF, MozJPEG, JPEG XL, PNG, and WebP, all running natively in the browser. On top of those outputs, the app reads HEIC/HEIF (iPhone photos), GIF (first frame), and SVG as input.&lt;/p&gt;

&lt;p&gt;The codecs live in a worker pool of three, so large batches compress in parallel without freezing the UI. Cancellation is wired through the whole pipeline: queued jobs are dequeued instantly, and in-flight jobs kill and replace their worker, which frees the WASM heap immediately instead of letting a long AVIF encode run into the void.&lt;/p&gt;

&lt;h2&gt;
  
  
  Target file size is a binary search, not a guess
&lt;/h2&gt;

&lt;p&gt;A target file size mode can't ask a server to "make it fit." Everything has to happen client-side, so the approach is: encode at some quality, measure the output, adjust, repeat.&lt;/p&gt;

&lt;p&gt;Binary search over the quality parameter converges fast (quality to size is roughly monotonic) and lands the highest quality that fits under the limit. Drop in a file, ask for 100 KB, and instead of guessing percentages you get the best-looking file that passes.&lt;/p&gt;

&lt;h2&gt;
  
  
  The offline problem
&lt;/h2&gt;

&lt;p&gt;A client-side app should survive with no internet, so Compressify is a PWA with a service worker precaching everything. That hit a wall fast: the WASM codecs are heavy, and the HEIC decoder alone is about two megabytes. Precaching that for every visitor, including the majority who will never touch a HEIC file, felt wrong.&lt;/p&gt;

&lt;p&gt;The fix was lazy-loading codecs per format. The HEIC decoder only downloads when you actually drop a HEIC file, then gets runtime-cached for offline use. If you never process iPhone photos, you never pay that bandwidth cost.&lt;/p&gt;

&lt;h2&gt;
  
  
  Privacy claims need auditing, not adjectives
&lt;/h2&gt;

&lt;p&gt;"Nothing gets uploaded" is easy to type and harder to keep true. Fonts, analytics, error trackers: every dependency is a potential leak. I ended up auditing every network call the app makes. Two results of that audit:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Elements that render user file names carry &lt;code&gt;data-clarity-mask="true"&lt;/code&gt;, so session recordings never capture them. Analytics payloads contain file types and counts, never names or contents.&lt;/li&gt;
&lt;li&gt;The domain never sends email, so its DMARC policy is set to reject at DNS level. Nobody can spoof mail from it either.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Perfect Lighthouse scores turned out to be a maintenance decision, too. The homepage is prerendered at build time with a Vite plugin and hydrated on the client, which keeps Largest Contentful Paint around 0.8 seconds. Every feature I add since then threatens that number. My previous &lt;a href="https://dev.to/toufiqul-hasan/perfect-100s-cls-mystery-and-a-silent-analytics-bug-5h4e"&gt;post&lt;/a&gt; covers that grind in detail: a &lt;code&gt;font-display: optional&lt;/code&gt; CLS bug, an analytics shim that silently dropped every event because gtag.js only processes &lt;code&gt;arguments&lt;/code&gt; objects, and how prerendering without breaking hydration actually works.&lt;/p&gt;

&lt;h2&gt;
  
  
  What it adds up to
&lt;/h2&gt;

&lt;p&gt;No sign-up, no watermark, no limits, no uploads, works offline as an installable PWA, and a network tab that stays quiet. Input formats cover iPhone photos, GIFs, and SVG; output formats cover everything modern; and you can target an exact file size, resize, compare formats, or grab the whole batch as a ZIP.&lt;/p&gt;

&lt;p&gt;If you ever need to shrink a file and want it to stay on your device, it's at &lt;a href="https://compressify.org" rel="noopener noreferrer"&gt;https://compressify.org&lt;/a&gt;. And if you've built something where the privacy claim required auditing your dependencies the same way, I'd genuinely like to hear how you handled it.&lt;/p&gt;

</description>
      <category>webdev</category>
      <category>webassembly</category>
      <category>javascript</category>
      <category>privacy</category>
    </item>
    <item>
      <title>Perfect Lighthouse scores the hard way: CLS, gtag.js, and hydration</title>
      <dc:creator>Toufiqul Hasan</dc:creator>
      <pubDate>Wed, 28 Oct 2026 14:00:53 +0000</pubDate>
      <link>https://dev.to/toufiqul-hasan/perfect-100s-cls-mystery-and-a-silent-analytics-bug-5h4e</link>
      <guid>https://dev.to/toufiqul-hasan/perfect-100s-cls-mystery-and-a-silent-analytics-bug-5h4e</guid>
      <description>&lt;p&gt;I got tired of uploading personal photos to random servers just to shrink a file by forty percent. Almost every popular image compressor online takes your image, uploads it over the wire to someone's server, crunches it, and sends it back. There's really no reason for that anymore. Browsers run WebAssembly. So, sitting at my desk here in Bangladesh, I decided to build &lt;a href="https://compressify.org/" rel="noopener noreferrer"&gt;Compressify&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The premise was simple: batch compression, HEIC input, and conversion to WebP, AVIF, and JPEG XL, all happening 100% on the client side using jSquash codecs (the same underlying encoders Google used for Squoosh). No backend, no accounts, and an installable PWA that works completely offline.&lt;/p&gt;

&lt;p&gt;I thought the hard engineering was over once the WASM memory buffers stopped crashing. Then I loaded up PageSpeed Insights to collect my expected 100 score.&lt;/p&gt;

&lt;p&gt;Mobile gave me a clean Cumulative Layout Shift of 0. Desktop gave me 0.676. Exactly 0.676, on every single run. The desktop headline was jumping around on load like an accordion.&lt;/p&gt;

&lt;h2&gt;
  
  
  The CLS bug: font-display: optional cuts both ways
&lt;/h2&gt;

&lt;p&gt;It took me an entire evening of profiling to realize the culprit was &lt;code&gt;font-display: optional&lt;/code&gt;. On mobile, PageSpeed's lab test throttles the network heavily. The Inter font file took too long to download, so the browser hit its short block period, gave up, and stuck with the system fallback font. No swap ever happened, which meant no shift.&lt;/p&gt;

&lt;p&gt;On the unthrottled desktop lab test, though, the font downloaded almost immediately. Chrome painted the initial page using the system font, and then swapped to Inter a split second later. Because the glyph dimensions between the system font and Inter didn't line up, the headline reflowed and threw the whole layout down the page.&lt;/p&gt;

&lt;p&gt;My first fix was creating a metric-matched fallback font using &lt;code&gt;size-adjust&lt;/code&gt; and &lt;code&gt;ascent-override&lt;/code&gt;. That shrank the shift, but didn't eliminate it. Average glyph width overrides don't help when individual letters still push a stray word onto a new line.&lt;/p&gt;

&lt;p&gt;So I stopped trying to be clever and inlined the critical font file as a base64 data URI directly inside the inlined CSS. The font is present on the very first paint. It can't swap because there's nothing to wait on. CLS dropped to zero instantly across the board. The trade-off was making the initial HTML roughly 100 KB heavier, but for a standalone utility app, that was a compromise I was happy to make.&lt;/p&gt;

&lt;h2&gt;
  
  
  Deferring analytics without breaking tracking
&lt;/h2&gt;

&lt;p&gt;Next came analytics. I set up GA4, ran Lighthouse again, and got dinged for 63 KB of unused JavaScript from &lt;code&gt;gtag.js&lt;/code&gt;. I tried loading it during browser idle, but Lighthouse still captures idle time within its audit window.&lt;/p&gt;

&lt;p&gt;The working pattern: gate every third-party script and the service worker registration behind the user's first real interaction.&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="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;kickOff&lt;/span&gt; &lt;span class="o"&gt;=&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="c1"&gt;// gtag.js, Clarity, registerSW all start here&lt;/span&gt;
&lt;span class="p"&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;pointerdown&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;keydown&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;touchstart&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;scroll&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;forEach&lt;/span&gt;&lt;span class="p"&gt;((&lt;/span&gt;&lt;span class="nx"&gt;signal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt;
  &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;kickOff&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="na"&gt;once&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;passive&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="na"&gt;capture&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="kc"&gt;true&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;&lt;code&gt;capture: true&lt;/code&gt; matters: listeners added during an event dispatch don't fire for that same event, so a regular bubble-phase listener would push the real load to the user's SECOND interaction. Real visitors get tracked the moment they do anything; synthetic audit bots that just stare at the screen trigger zero script load.&lt;/p&gt;

&lt;p&gt;Which brings me to the most embarrassing mistake I made on this project. When I added the GA4 snippet, I looked at Google's traditional boilerplate and thought I'd modernize it with ES6 rest parameters.&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="c1"&gt;// What I wrote:&lt;/span&gt;
&lt;span class="kd"&gt;const&lt;/span&gt; &lt;span class="nx"&gt;gtag&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;(...&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;=&amp;gt;&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dataLayer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;args&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt;

&lt;span class="c1"&gt;// What the documentation told me to write:&lt;/span&gt;
&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nf"&gt;gtag&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nb"&gt;window&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;dataLayer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;arguments&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;Google's library explicitly checks for an &lt;code&gt;arguments&lt;/code&gt; object inside &lt;code&gt;dataLayer&lt;/code&gt;. It silently ignores standard JavaScript arrays. No console warnings. No syntax errors. The network tab showed &lt;code&gt;gtag.js&lt;/code&gt; loading cleanly, but not a single tracking beacon ever left the browser. I went days wondering why nobody was using the app before I checked the Network tab during an active session and saw dead silence.&lt;/p&gt;

&lt;p&gt;I pasted the official &lt;code&gt;arguments&lt;/code&gt; snippet directly into the browser console, triggered a test event, and the beacon fired instantly. Days of analytics down the drain because I wanted to feel fancy with an arrow function.&lt;/p&gt;

&lt;p&gt;Two more hardening steps came out of that incident. Tracking is disabled entirely on &lt;code&gt;localhost&lt;/code&gt; so my own test runs don't pollute the dashboards, and the GA4/Clarity initializers run behind a production-origin allowlist, so preview deploys and LAN testing never reach the vendor scripts either.&lt;/p&gt;

&lt;h2&gt;
  
  
  The E2E timeout hierarchy
&lt;/h2&gt;

&lt;p&gt;The E2E tests gave me their own headaches. In Playwright, my image decode tests consistently passed in half a second on Chromium, but WebKit would randomly hang and fail at the 30-second mark. I spent hours suspecting Safari's WASM engine was choking.&lt;/p&gt;

&lt;p&gt;The real issue was timeout hierarchy: the assertion timeout I had set was longer than the test-level timeout. When WebKit took slightly longer to initialize a codec, the test suite killed the entire run before the assertion had a chance to evaluate. WebKit was just sitting on a doomed decode longer than Chromium was. Fixing the timeout cascade cleared up the false negatives immediately.&lt;/p&gt;

&lt;p&gt;The feature that went surprisingly smoothly was the Web Share Target API. If you install &lt;a href="https://compressify.org/" rel="noopener noreferrer"&gt;Compressify&lt;/a&gt; on Android, it registers as a system share target. You can pick photos from your gallery, tap Share, and send them directly to the app. A small service worker imported via Workbox catches the incoming POST request, stores the incoming files in Cache Storage as &lt;code&gt;FormData&lt;/code&gt;, and redirects into the app to pull them out. I followed the official implementation examples to the letter this time, and it worked on the first try. Lesson learned.&lt;/p&gt;

&lt;h2&gt;
  
  
  Prerendering the homepage without breaking hydration
&lt;/h2&gt;

&lt;p&gt;The last big change was SEO itself. The SEO content on the homepage (how-to steps, FAQ, format guides) used to be a lazy-loaded React chunk, which meant crawlers saw an empty shell and LCP waited for JavaScript.&lt;/p&gt;

&lt;p&gt;The fix: a small Vite plugin that bundles the app for Node (rolldown), renders it with &lt;code&gt;renderToString&lt;/code&gt;, and injects the output into &lt;code&gt;&amp;lt;div id="root"&amp;gt;&lt;/code&gt; during &lt;code&gt;transformIndexHtml&lt;/code&gt;. The client then calls &lt;code&gt;hydrateRoot&lt;/code&gt;, so first paint comes from static HTML and React adopts those nodes instead of repainting them.&lt;/p&gt;

&lt;p&gt;Two constraints made this tricky:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The client's first render must match the prerendered HTML byte for byte. Persisted settings and the real theme are applied in layout effects after hydration, never in state initializers, or React throws away the static DOM and repaints everything (which pushes LCP out to mount time).&lt;/li&gt;
&lt;li&gt;No &lt;code&gt;React.lazy&lt;/code&gt; or &lt;code&gt;Suspense&lt;/code&gt; anywhere in the prerendered tree. A boundary that suspends server-side emits fallback markers the client cannot reproduce, and hydration fails for the whole tree.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The result: crawlers get the full page as raw HTML, LCP sits at ~0.8 seconds, and the Lighthouse scores stayed at 100.&lt;/p&gt;

&lt;h2&gt;
  
  
  Locking the domain down
&lt;/h2&gt;

&lt;p&gt;Toward the end, I wired an &lt;code&gt;axe-core&lt;/code&gt; accessibility check into CI. It forced me to fix subtle contrast misses I had overlooked: &lt;code&gt;zinc-500&lt;/code&gt; against &lt;code&gt;zinc-950&lt;/code&gt; fails WCAG AA standards, while &lt;code&gt;zinc-400&lt;/code&gt; passes.&lt;/p&gt;

&lt;p&gt;Since the domain never sends email, I also set a DMARC policy of reject at DNS level, so nobody can spoof mail from it either. Every claim like "nothing leaves your device" or "nobody can impersonate us" is just DNS records and a network tab once you actually audit them.&lt;/p&gt;

&lt;p&gt;With the font inlined, third-party scripts deferred to user input, and the contrast dialed in, the app now hits straight 100s across every Lighthouse category on both &lt;a href="https://pagespeed.web.dev/analysis/https-compressify-org/51rso7whgg?form_factor=desktop" rel="noopener noreferrer"&gt;desktop&lt;/a&gt; and &lt;a href="https://pagespeed.web.dev/analysis/https-compressify-org/51rso7whgg?form_factor=mobile" rel="noopener noreferrer"&gt;mobile&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;It's live at &lt;a href="https://compressify.org" rel="noopener noreferrer"&gt;https://compressify.org&lt;/a&gt; if you want to poke at it or compress a few files locally. If you've ever run into weird font swap quirks or lost data to silent third-party script bugs like this, let me know in the comments.&lt;/p&gt;

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