Skip to content

Build your personal knowledge base with Trilium Notes

client/image viewer: fix some issues

Adorian Doran6c2bf405cc1f
diff --git a/apps/client/src/widgets/react/ImageViewer.spec.tsx b/apps/client/src/widgets/react/ImageViewer.spec.tsx
index 47c21b9d4a..4ae4604a14 100644
--- a/apps/client/src/widgets/react/ImageViewer.spec.tsx
+++ b/apps/client/src/widgets/react/ImageViewer.spec.tsx
@@ -98,6 +98,14 @@ describe("ImageViewer", () => {
expect(container.querySelector(".content-error-message")).not.toBeNull();
});
+ it("reveals without decode() where the API is unavailable (ancient/headless runtimes)", async () => {
+ (HTMLImageElement.prototype as { decode?: () => Promise<void> }).decode = undefined;
+ const container = renderViewer({ src: "x" });
+
+ await vi.waitFor(() => expect(wrapperClassList(container)?.contains("img-loaded")).toBe(true));
+ expect(wrapperClassList(container)?.contains("img-loading-error")).toBe(false);
+ });
+
it("reveals an image that loaded even when decode() rejects (e.g. Chrome Android memory limits)", async () => {
// A large image can load fine yet have decode() reject; only a true load failure has naturalWidth 0.
HTMLImageElement.prototype.decode = () => Promise.reject(new Error("EncodingError"));
diff --git a/apps/client/src/widgets/react/ImageViewer.tsx b/apps/client/src/widgets/react/ImageViewer.tsx
index 9b1784dc12..568c67c4b3 100644
--- a/apps/client/src/widgets/react/ImageViewer.tsx
+++ b/apps/client/src/widgets/react/ImageViewer.tsx
@@ -97,13 +97,18 @@ export default function ImageViewer({ src, imgClassName, alt = "", minScale = 0.
updateZoomState(zoomRef.current?.instance?.state?.scale ?? 1);
});
const timer = setTimeout(reveal, REVEAL_FALLBACK_MS);
- img.decode().then(reveal, () => {
- // decode() can reject for an image that still paints fine — notably large images on
- // memory-constrained Chrome (Android), which throw EncodingError despite loading OK.
- // Only fail when the image truly didn't load; otherwise reveal without the smooth fade.
- if (img.complete && img.naturalWidth > 0) reveal();
- else settle(() => setLoadingError(true));
- });
+ if (typeof img.decode === "function") {
+ img.decode().then(reveal, () => {
+ // decode() can reject for an image that still paints fine — notably large images on
+ // memory-constrained Chrome (Android), which throw EncodingError despite loading OK.
+ // Only fail when the image truly didn't load; otherwise reveal without the smooth fade.
+ if (img.complete && img.naturalWidth > 0) reveal();
+ else settle(() => setLoadingError(true));
+ });
+ } else {
+ // No decode() (ancient/unusual runtimes, some headless test envs): reveal without the fade.
+ reveal();
+ }
return () => settle(() => {});
}, [ src ]);