Our client-side image resizer had a silent HEIC fallback, and Playwright caught it alongside a hidden mobile CTA

2026-12-05来源 dev.toEnglish

When we launched a small photo-to-video web app, our daily smoke checks were green for a full week. Every page returned HTTP 200, the upload endpoint accepted test JPEGs, and GA4 showed 50 sessions, 13% engagement, and three visitors reaching plan selection.

Then we split GA4 by country. Thirty-five of those 50 sessions — and every single funnel event — came from Japan, where our own monitoring server sits. Excluding our server's country left 15 real sessions at 0% engagement and roughly 1.5 seconds per visit.

So we replaced the "page returns 200" check with a 30-case real-input Playwright script (1366×900 desktop and 390×844 mobile) that feeds the form the exact file types and viewports real visitors hit. It passed 28 cases and failed two. Both failures were subtle enough that unit tests and happy-path checks never saw them.

Bug 1: catch (e) { return f } quietly uploaded raw HEIC bytes

Before posting a photo to /api/upload, our page shrinks it in the browser to at most 1600 px on the long edge and encodes it as JPEG:

async function shrink(f) { try { const bm = await createImageBitmap(f); const k = Math.min(1, 1600 / Math.max(bm.width, bm.height)); const c = document.createElement('canvas'); c.width = Math.round(bm.width * k); c.height = Math.round(bm.height * k); c.getContext('2d').drawImage(bm, 0, 0, c.width, c.height); return await new Promise((r) => c.toBlob((b) => r(b || f), 'image/jpeg', 0.9)); } catch (e) { return f; } } 

That catch (e) { return f } looked defensive: if canvas resizing ever fails, fall back to uploading the original file. And on the server, /api/upload accepted image/(jpeg|png|webp|heic|heif), because we wanted iPhone photos to work.

Here is what actually happened across file types in our Playwright run:

Test input Browser decode (createImageBitmap) Content-Type sent to /api/upload Result
JPEG 1200×1600 decoded image/jpeg resized, clean thumbnail
Phone JPEG 4032×3024 decoded image/jpeg resized, clean thumbnail
Phone screenshot PNG 1170×2532 decoded image/jpeg converted, clean thumbnail
WebP 1080×1350 decoded image/jpeg converted, clean thumbnail
GIF 1×1 decoded image/jpeg converted, clean thumbnail
PDF picked by mistake throws → raw fallback application/pdf server returns HTTP 400 (expected)
iPhone HEIC on desktop Chrome throws → raw fallback image/heic HTTP 200, broken thumbnail, raw HEIC stored

When an iPhone user picks a photo directly in iOS Safari, iOS transcodes the HEIC file to JPEG before the browser sees it. So manual testing on an iPhone looked fine.

When someone AirDrops that same IMG_0413.HEIC to a laptop and uploads it in desktop Chrome, Chrome cannot decode HEIC. createImageBitmap(f) throws, catch silently hands back the untouched File, and because the server MIME allow-list included image/heic, the upload succeeds with HTTP 200. The slot turns green, the local URL.createObjectURL(f) preview cannot render, and downstream processing gets a raw .heic file it was never meant to handle.

How to catch and fix it

The test itself is four lines in Playwright — attach a listener for the real Content-Type header on /api/upload, feed a minimal ftypheic buffer, and assert that either the browser converted it to image/jpeg or the UI told the user to send a JPG:

const uploads = []; page.on('request', (req) => { if (req.url().endsWith('/api/upload')) uploads.push(req.headers()['content-type']); }); const heic = Buffer.concat([ Buffer.from([0, 0, 0, 0x18]), Buffer.from('ftypheic'), Buffer.alloc(2000, 7), ]); await page.locator('.slot[data-i="4"] input').setInputFiles({ name: 'IMG_0413.HEIC', mimeType: 'image/heic', buffer: heic, }); // Assert: either converted to image/jpeg, or rejected with a clear message. 

The fix has two halves:

  1. Never treat a decode failure as "upload the raw bytes anyway" for formats your pipeline does not convert server-side. If createImageBitmap(f) throws and f.type is image/heic or image/heif, stop on the client and show a direct message ("Save HEIC photos as JPG first on a computer").
  2. Match your server MIME list to what your backend actually handles, not what you wish browsers converted. Keeping heic|heif in the server regex hid the client fallback completely.

Bug 2: The mobile first screen had zero tappable CTAs

Our second failure explained why real mobile visitors bounced in two seconds.

On desktop (1366×900), the hero heading, short copy, primary CTA button, and 9:16 example video sit side by side. On mobile (390×844), the layout stacks vertically: heading, intro paragraph, full-height 9:16 video, and then the hero CTA button. We also had a sticky bottom CTA bar, hidden until the user scrolled past the hero.

Our Playwright check measured what was actually inside the initial 390×844 viewport on load:

const r = await page.evaluate(() => { const visibleCtas = [...document.querySelectorAll('a.cta, button.cta')].filter((el) => { const b = el.getBoundingClientRect(); const s = getComputedStyle(el); const stickyOk = el.closest('#sticky') ? el.closest('#sticky').classList.contains('show') : true; return b.height > 0 && b.top < innerHeight && b.bottom > 0 && s.visibility !== 'hidden' && s.display !== 'none' && stickyOk; }); const hero = document.querySelector('.h-c .cta'); return { inView: visibleCtas.length, heroCtaTop: hero ? Math.round(hero.getBoundingClientRect().top) : null, }; }); 

Result on 390×844: {"inView": 0, "heroCtaTop": 1268}.

The primary CTA started 424 pixels below the bottom of the phone screen, and the sticky bar was hidden because the user had not scrolled yet. A first-time visitor on a phone saw a heading, a paragraph, and a muted vertical video with no button in sight.

Checking scrollWidth <= innerWidth had passed (over: 0), so a normal responsive layout check never flagged it. Checking whether at least one real CTA's getBoundingClientRect() intersects [0, innerHeight] caught it in one second.

Stop your own tests from poisoning analytics

One last detail from the same test script: the reason our real engagement problem stayed invisible for days was that our own automated browser runs fired GA4 tags every day.

Every Playwright context we launch against production now aborts analytics requests up front and intercepts checkout redirects so we never create real Stripe sessions when testing the form:

await context.route( /googletagmanager|google-analytics|googlesyndication|doubleclick|cloudflareinsights/, (route) => route.abort() ); await context.route(/checkout\.stripe\.com/, (route) => route.fulfill({ status: 200, contentType: 'text/html', body: '<h1>stripe stub</h1>' }) ); 

Even with route blocking in place, we still segment GA4 reports excluding our server's country whenever we check landing-page engagement, so any stray curl or manual check cannot inflate the baseline.

Summary checklist for upload-based web apps

  1. Test every photo format real users actually pick (4032×3024 JPEG, PNG screenshot, WebP, GIF, HEIC on desktop Chrome, wrong MIME like PDF, oversized file) and assert the outgoing Content-Type, not just the HTTP status.
  2. Do not fall back from createImageBitmap errors to raw uploads unless your server explicitly decodes that format.
  3. On a 390×844 mobile viewport, assert that at least one visible CTA's bounding box sits inside innerHeight before any scrolling happens.
  4. Stub analytics and payment URLs in every production Playwright context, and double-check GA4 by country so server traffic never masks real bounce rates.

These checks come from Tiny Desk AI Video, a small fan-made parody tool for the 15-second "Outstanding" desk-concert meme (not affiliated with NPR, Tiny Desk, or the performers).

原文:https://www.a8q9.cn/en/article/our-client-side-image-resizer-had-a-silent-heic-fallback-and-playwright-caught-i-f1ce76.html

相关文章