Skip to content

Build your personal knowledge base with Trilium Notes

fix(standalone/ios): revoke image blob URLs to stop the memory leak

Elian Doran5332cee3fbdc
diff --git a/apps/standalone/src/ios-interceptors.spec.ts b/apps/standalone/src/ios-interceptors.spec.ts
index 105a127e42..d692eee86b 100644
--- a/apps/standalone/src/ios-interceptors.spec.ts
+++ b/apps/standalone/src/ios-interceptors.spec.ts
@@ -235,6 +235,74 @@ describe("setupImageInterceptor", () => {
expect(mocks.localFetch).toHaveBeenCalledOnce();
});
+ it("revokes the previous blob URL when an image is repointed to another API image", async () => {
+ let counter = 0;
+ vi.spyOn(URL, "createObjectURL").mockImplementation(() => `blob:url-${++counter}`);
+ const revoke = vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => {});
+ // Fresh Response per call — a body can only be read once, and this image
+ // is swapped twice.
+ mocks.localFetch.mockImplementation(async () => makeResponse("bytes"));
+
+ const img = document.createElement("img");
+ img.setAttribute("src", "/api/images/1");
+ document.body.appendChild(img);
+ setupImageInterceptor();
+ await vi.waitFor(() => expect(img.getAttribute("src")).toBe("blob:url-1"));
+
+ // Repoint the same element at a different API image; the first blob URL is
+ // now orphaned and must be revoked, otherwise it leaks for the page's life.
+ img.setAttribute("src", "/api/images/2");
+ await vi.waitFor(() => expect(img.getAttribute("src")).toBe("blob:url-2"));
+
+ expect(revoke).toHaveBeenCalledWith("blob:url-1");
+ });
+
+ it("revokes the blob URL when an intercepted image is removed from the DOM", async () => {
+ vi.spyOn(URL, "createObjectURL").mockReturnValue("blob:removed-url");
+ const revoke = vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => {});
+ mocks.localFetch.mockResolvedValue(makeResponse("bytes"));
+
+ const img = document.createElement("img");
+ img.setAttribute("src", "/api/images/1");
+ document.body.appendChild(img);
+ setupImageInterceptor();
+ await vi.waitFor(() => expect(img.getAttribute("src")).toBe("blob:removed-url"));
+
+ // Navigating away removes the <img>; its blob URL must be revoked or it
+ // leaks — the dominant "every note you open adds memory" case.
+ img.remove();
+ await vi.waitFor(() => expect(revoke).toHaveBeenCalledWith("blob:removed-url"));
+ });
+
+ it("revokes blob URLs of images inside a removed container subtree", async () => {
+ vi.spyOn(URL, "createObjectURL").mockReturnValue("blob:subtree-url");
+ const revoke = vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => {});
+ mocks.localFetch.mockResolvedValue(makeResponse("bytes"));
+
+ const container = document.createElement("div");
+ const img = document.createElement("img");
+ img.setAttribute("src", "/api/images/1");
+ container.appendChild(img);
+ document.body.appendChild(container);
+ setupImageInterceptor();
+ await vi.waitFor(() => expect(img.getAttribute("src")).toBe("blob:subtree-url"));
+
+ // Removing the wrapper detaches the nested <img> too; its URL must be freed.
+ container.remove();
+ await vi.waitFor(() => expect(revoke).toHaveBeenCalledWith("blob:subtree-url"));
+ });
+
+ it("ignores removed non-element nodes", async () => {
+ const revoke = vi.spyOn(URL, "revokeObjectURL").mockImplementation(() => {});
+ const text = document.createTextNode("text");
+ document.body.appendChild(text);
+ setupImageInterceptor();
+
+ text.remove(); // removedNodes contains a text node — neither <img> nor Element
+ await new Promise((r) => setTimeout(r, 20));
+ expect(revoke).not.toHaveBeenCalled();
+ });
+
it("ignores src mutations on non-image elements", async () => {
const span = document.createElement("span");
span.setAttribute("src", "/api/images/a");
diff --git a/apps/standalone/src/ios-interceptors.ts b/apps/standalone/src/ios-interceptors.ts
index 547a0c6ee7..2a79e58d86 100644
--- a/apps/standalone/src/ios-interceptors.ts
+++ b/apps/standalone/src/ios-interceptors.ts
@@ -57,6 +57,11 @@ export function setupFetchInterceptor() {
// re-fetch on idempotent re-renders.
export function setupImageInterceptor() {
const lastProcessedSrc = new WeakMap<HTMLImageElement, string>();
+ // Object URLs are held by the browser until explicitly revoked (the SPA
+ // document never unloads), so track the one currently assigned to each <img>
+ // and revoke it whenever the image is repointed or removed — otherwise every
+ // image swap leaks memory, which matters most on the constrained iOS WebView.
+ const blobUrls = new WeakMap<HTMLImageElement, string>();
function matchesLocalApi(value: string): URL | null {
if (!value || value.startsWith("blob:") || value.startsWith("data:")) return null;
@@ -69,6 +74,14 @@ export function setupImageInterceptor() {
return (abs.origin === location.origin && isLocalApiRequest(abs)) ? abs : null;
}
+ function releaseBlobUrl(img: HTMLImageElement) {
+ const previous = blobUrls.get(img);
+ if (previous) {
+ URL.revokeObjectURL(previous);
+ blobUrls.delete(img);
+ }
+ }
+
async function swapToBlob(img: HTMLImageElement, originalSrc: string, absUrl: URL) {
if (lastProcessedSrc.get(img) === originalSrc) return;
lastProcessedSrc.set(img, originalSrc);
@@ -80,7 +93,11 @@ export function setupImageInterceptor() {
return;
}
const blob = await resp.blob();
- img.setAttribute("src", URL.createObjectURL(blob));
+ // Free the URL from a prior swap on this same element before replacing it.
+ releaseBlobUrl(img);
+ const blobUrl = URL.createObjectURL(blob);
+ blobUrls.set(img, blobUrl);
+ img.setAttribute("src", blobUrl);
} catch (err) {
console.warn("[ImageInterceptor] Failed to load", absUrl.href, err);
lastProcessedSrc.delete(img);
@@ -106,6 +123,13 @@ export function setupImageInterceptor() {
node.querySelectorAll("img").forEach((img) => checkImage(img as HTMLImageElement));
}
}
+ for (const node of record.removedNodes) {
+ if (node instanceof HTMLImageElement) {
+ releaseBlobUrl(node);
+ } else if (node instanceof Element) {
+ node.querySelectorAll("img").forEach((img) => releaseBlobUrl(img as HTMLImageElement));
+ }
+ }
}
}
});