Recover when a cached proxy will not decode

The editor decodes the copy of the proxy kept in IndexedDB before asking the
server, so one stale or truncated copy fails with Chrome's "The source image
could not be decoded." on every later visit. A failed decode now drops that
copy, refetches the current bytes and retries, and the orientation option falls
back to a plain decode if the decoder refuses it.

🤖 Generated with Codebuff
Co-Authored-By: Codebuff <noreply@codebuff.com>
This commit is contained in:
2026-11-28 06:09:57 +00:00
parent ecb3d5de7c
commit 2bec4102a2
2 changed files with 30 additions and 2 deletions
+5
View File
@@ -64,6 +64,11 @@ export async function getAsset(id: string): Promise<Blob | undefined> {
return wrap((await store('assets')).get(id) as IDBRequest<Blob | undefined>);
}
/** Drop one cached file without touching its photo record. */
export async function deleteAsset(id: string): Promise<void> {
await wrap((await store('assets', 'readwrite')).delete(id));
}
export async function getAllEvents(): Promise<EventRecord[]> {
return wrap((await store('events')).getAll() as IDBRequest<EventRecord[]>);
}
+25 -2
View File
@@ -1,5 +1,5 @@
import type { PhotoRecord } from '../types';
import { getAsset, putAsset } from './db';
import { deleteAsset, getAsset, putAsset } from './db';
import { parsePreview16, type Preview16 } from './preview16';
const CACHE_LIMIT = 6;
@@ -56,6 +56,18 @@ async function fetchRemote(photo: PhotoRecord): Promise<Blob | null> {
}
}
/**
* Decode upright, falling back for decoders that refuse the orientation option
* (a plain decode is better than no frame at all).
*/
async function decodeBitmap(blob: Blob): Promise<ImageBitmap> {
try {
return await createImageBitmap(blob, { imageOrientation: 'from-image' });
} catch {
return await createImageBitmap(blob);
}
}
export async function getBitmap(photo: PhotoRecord): Promise<ImageBitmap> {
const hit = cache.get(photo.id);
if (hit) {
@@ -75,7 +87,18 @@ export async function getBitmap(photo: PhotoRecord): Promise<ImageBitmap> {
}
// Apply EXIF orientation so the pixels the editor works on are upright.
const bitmap = await createImageBitmap(blob, { imageOrientation: 'from-image' });
// A cached copy can be stale — truncated by an interrupted download, or
// written when the server produced a different proxy — and then it decodes
// to nothing forever, so one failure drops it and refetches the current bytes.
let bitmap: ImageBitmap;
try {
bitmap = await decodeBitmap(blob);
} catch (err) {
await deleteAsset(photo.id).catch(() => undefined);
const fresh = (await fetchRemoteCopy(photo)) ?? undefined;
if (!fresh) throw err;
bitmap = await decodeBitmap(fresh);
}
cache.set(photo.id, bitmap);
while (cache.size > CACHE_LIMIT) {
const first = cache.keys().next();