Build your personal knowledge base with Trilium Notes
TypeScript electronelectron-appknowledge-baseknowledge-graphknowledge-managementknowledge-management-graphlocal-firstnote-managmentnote-takernote-takingnotebooknotesnotes-apppersonal-knowledge-basepersonal-wikiscriptableself-hostedself-hostingwiki
fix(mobile/ios): images not being displayed
AndreeaCorlacifd237dc760c8
diff --git a/apps/standalone/src/main.ts b/apps/standalone/src/main.tsindex 708ad1280c..1a757ffd66 100644--- a/apps/standalone/src/main.ts+++ b/apps/standalone/src/main.ts@@ -54,6 +54,92 @@ function setupFetchInterceptor() {}+// <img src="api/images/..."> requests are issued by the browser's internal+// image loader — they don't go through window.fetch or XMLHttpRequest, so the+// interceptors above can't catch them. On the capacitor:// scheme there is+// also no Service Worker to fall back on (WebKit only registers SWs for+// http/https origins), so the request hits Capacitor's URL scheme handler,+// which has no idea about /api/images/... and silently 404s — that's the+// broken-image placeholder users see in the iOS app.+//+// Watch the DOM for <img> elements whose src points at a local API path and+// transparently swap the src to a blob: URL backed by the SQLite worker. This+// catches images added via HTML parsing (initial document, innerHTML), via+// setAttribute/attr (jQuery, content_renderer), and via the src property.+// Setting the same src again on the same element is deduplicated so we don't+// re-fetch on idempotent re-renders.+function setupImageInterceptor() {+ const lastProcessedSrc = new WeakMap<HTMLImageElement, string>();++ function matchesLocalApi(value: string): URL | null {+ if (!value || value.startsWith("blob:") || value.startsWith("data:")) return null;+ let abs: URL;+ try {+ abs = new URL(value, location.href);+ } catch {+ return null;+ }+ return (abs.origin === location.origin && isLocalApiRequest(abs)) ? abs : null;+ }++ async function swapToBlob(img: HTMLImageElement, originalSrc: string, absUrl: URL) {+ if (lastProcessedSrc.get(img) === originalSrc) return;+ lastProcessedSrc.set(img, originalSrc);++ try {+ const resp = await localFetch(new Request(absUrl.toString()));+ if (!resp.ok) {+ lastProcessedSrc.delete(img);+ return;+ }+ const blob = await resp.blob();+ img.setAttribute("src", URL.createObjectURL(blob));+ } catch (err) {+ console.warn("[ImageInterceptor] Failed to load", absUrl.href, err);+ lastProcessedSrc.delete(img);+ }+ }++ function checkImage(img: HTMLImageElement) {+ const src = img.getAttribute("src");+ if (!src) return;+ const absUrl = matchesLocalApi(src);+ if (absUrl) swapToBlob(img, src, absUrl);+ }++ const observer = new MutationObserver((records) => {+ for (const record of records) {+ if (record.type === "attributes" && record.attributeName === "src" && record.target instanceof HTMLImageElement) {+ checkImage(record.target);+ } else if (record.type === "childList") {+ for (const node of record.addedNodes) {+ if (node instanceof HTMLImageElement) {+ checkImage(node);+ } else if (node instanceof Element) {+ node.querySelectorAll("img").forEach((img) => checkImage(img as HTMLImageElement));+ }+ }+ }+ }+ });++ function start() {+ observer.observe(document.documentElement, {+ subtree: true,+ childList: true,+ attributes: true,+ attributeFilter: ["src"]+ });+ document.querySelectorAll("img").forEach((img) => checkImage(img as HTMLImageElement));+ }++ if (document.documentElement) {+ start();+ } else {+ document.addEventListener("DOMContentLoaded", start, { once: true });+ }+}+// jQuery $.ajax uses XMLHttpRequest, which window.fetch interception does not// catch. On the capacitor:// scheme there is no Service Worker to route// requests, so XHR-bound API calls would hit the native bridge and return@@ -190,6 +276,7 @@ async function bootstrap() {if (location.protocol === "capacitor:") {setupFetchInterceptor();setupXhrInterceptor();+ setupImageInterceptor();} else {attachServiceWorkerBridge();await waitForServiceWorkerControl();