Skip to content

Build your personal knowledge base with Trilium Notes

fix(print): load lazy images eagerly before printing (closes #11733)

Elian Doran79ddca11672e
diff --git a/apps/client/src/print.spec.tsx b/apps/client/src/print.spec.tsx
index d7bd960466..ef96210849 100644
--- a/apps/client/src/print.spec.tsx
+++ b/apps/client/src/print.spec.tsx
@@ -158,12 +158,14 @@ describe("SingleNoteRenderer", () => {
expect(emptyContainer.querySelector("main")?.innerHTML).toContain("Unable to parse spreadsheet data");
});
- it("renders a text note and waits for every image (complete, load, error)", async () => {
+ it("renders a text note and waits for every image (complete, load, error, lazy)", async () => {
const note = buildNote({ id: "rich", title: "Rich", type: "text" });
const completeImg = makeImage(true);
const loadImg = makeImage(false);
const errorImg = makeImage(false);
- h.getRenderedContent.mockResolvedValueOnce({ $renderedContent: [completeImg, loadImg, errorImg] });
+ const lazyImg = makeImage(false);
+ lazyImg.setAttribute("loading", "lazy");
+ h.getRenderedContent.mockResolvedValueOnce({ $renderedContent: [completeImg, loadImg, errorImg, lazyImg] });
const onReady = vi.fn();
renderInto(<SingleNoteRenderer note={note} onReady={onReady} onProgressChanged={() => {}} />);
@@ -171,8 +173,10 @@ describe("SingleNoteRenderer", () => {
// Re-firing once the listeners are attached resolves the pending image promises.
loadImg.dispatchEvent(new Event("load"));
errorImg.dispatchEvent(new Event("error"));
+ lazyImg.dispatchEvent(new Event("load"));
expect(onReady).toHaveBeenCalledWith({ type: "single-note" });
});
+ expect(lazyImg.getAttribute("loading")).toBe("eager");
// Printing preserves full include-note nesting via expandNestedIncludes.
expect(h.getRenderedContent).toHaveBeenCalledWith(note, { noChildrenList: true, expandNestedIncludes: true, mediaEnvironment: "native" });
});
diff --git a/apps/client/src/print.tsx b/apps/client/src/print.tsx
index d2d3c9679d..d7dc13d01e 100644
--- a/apps/client/src/print.tsx
+++ b/apps/client/src/print.tsx
@@ -126,8 +126,12 @@ export function SingleNoteRenderer({ note, onReady }: RendererProps) {
// to settle first; otherwise they land after the capture and are missing from the PDF.
await waitForPendingRenders(container);
- // Wait for all images to load.
+ // Wait for all images to load. A lazy image, such as a link embed's favicon, never
+ // scrolls into the print window's viewport, so it would fire neither `load` nor `error`.
const images = Array.from(container.querySelectorAll("img"));
+ for (const img of images) {
+ img.loading = "eager";
+ }
await Promise.all(
images.map(img => {
if (img.complete) return Promise.resolve();