The best way to control Claude Code, Codex and more from any computer or phone
perf(web): skip base64 for oversized image candidates (#5220)
Taras committed
Aug 11, 2026 at 06:33 UTC
f9730979c1046e3e69ac89aa86882ebf8b2be9a6
1 file changed
+12
-15
apps/web/src/lib/imageCompression.ts
+12
-15
@@ -140,27 +140,28 @@ function createCanvas(width: number, height: number): Canvas2D | null {
140
* and it keeps alpha, so screenshots with transparency survive intact.
141
* Browsers that can't encode it silently fall back to JPEG.
142
*/
143
-async function encodeToDataUrl(
143
+async function encodeCanvas(
144
canvas: OffscreenCanvas | HTMLCanvasElement,
145
quality: number,
146
mimeType: string,
147
-): Promise<{ dataUrl: string; mimeType: string } | null> {
147
+ budgetChars: number,
148
+): Promise<{ dataUrl: string | null; mimeType: string } | null> {
149
if (typeof HTMLCanvasElement !== "undefined" && canvas instanceof HTMLCanvasElement) {
150
const dataUrl = canvas.toDataURL(mimeType, quality);
151
// toDataURL silently returns a PNG when the requested type is unsupported.
152
if (!dataUrl.startsWith(`data:${mimeType}`)) return null;
152
- return { dataUrl, mimeType };
153
+ return { dataUrl: dataUrl.length <= budgetChars ? dataUrl : null, mimeType };
154
}
155
const blob = await (canvas as OffscreenCanvas).convertToBlob({ type: mimeType, quality });
156
if (blob.type && blob.type !== mimeType) return null;
157
+ const dataUrlLength = `data:${mimeType};base64,`.length + 4 * Math.ceil(blob.size / 3);
158
+ if (dataUrlLength > budgetChars) return { dataUrl: null, mimeType };
159
return { dataUrl: await blobToDataUrl(blob, mimeType), mimeType };
160
}
161
162
/**
163
* Draws `bitmap` scaled to fit `maxDimension` and encodes it, stepping
161
- * quality down until the data URL fits `budgetChars`. Returns the smallest
162
- * encoding produced, even if it still exceeds the budget, so the caller can
163
- * decide whether to keep or drop it.
164
+ * quality down until the data URL fits `budgetChars`.
165
*/
166
async function encodeWithinBudget(
167
bitmap: ImageBitmap,
@@ -175,7 +176,7 @@ async function encodeWithinBudget(
176
177
// Probe WebP once; JPEG (no alpha) needs a white matte, so the fill has to
178
// happen before drawing and depends on which codec we end up using.
178
- const probe = await encodeToDataUrl(target.canvas, QUALITY_STEPS[0], "image/webp");
179
+ const probe = await encodeCanvas(target.canvas, QUALITY_STEPS[0], "image/webp", 0);
180
const mimeType = probe ? "image/webp" : "image/jpeg";
181
182
if (mimeType === "image/jpeg") {
@@ -184,18 +185,14 @@ async function encodeWithinBudget(
185
}
186
target.context.drawImage(bitmap, 0, 0, width, height);
187
187
- let smallest: { dataUrl: string; mimeType: string } | null = null;
188
for (const quality of QUALITY_STEPS) {
189
- const encoded = await encodeToDataUrl(target.canvas, quality, mimeType);
189
+ const encoded = await encodeCanvas(target.canvas, quality, mimeType, budgetChars);
190
if (!encoded) break;
191
- if (smallest === null || encoded.dataUrl.length < smallest.dataUrl.length) {
192
- smallest = encoded;
193
- }
194
- if (encoded.dataUrl.length <= budgetChars) {
195
- return encoded;
191
+ if (encoded.dataUrl !== null) {
192
+ return { dataUrl: encoded.dataUrl, mimeType: encoded.mimeType };
193
}
194
}
198
- return smallest;
195
+ return null;
196
}
197
198
type ReencodeResult =