Skip to content

Build your personal knowledge base with Trilium Notes

fix(clipper): give up on a slow favicon or PDF check after 3 seconds

Elian Dorana9f6cda8fc77
diff --git a/apps/web-clipper/entrypoints/background/index.spec.ts b/apps/web-clipper/entrypoints/background/index.spec.ts
index f3974a8cf4..91587f8c0a 100644
--- a/apps/web-clipper/entrypoints/background/index.spec.ts
+++ b/apps/web-clipper/entrypoints/background/index.spec.ts
@@ -1,6 +1,8 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { fakeBrowser } from "wxt/testing/fake-browser";
+import { OPTIONAL_REQUEST_TIMEOUT_MS } from "@/utils";
+
const { facade, TriliumError } = vi.hoisted(() => ({
facade: {
callService: vi.fn<(method: string, path: string, body?: unknown) => Promise<any>>(),
@@ -866,7 +868,7 @@ describe("background", () => {
srcUrl: "https://example.com/cat.png",
pageUrl: PAGE_URL
});
- expect(fetchMock).toHaveBeenCalledWith("https://example.com/cat.png");
+ expect(fetchMock).toHaveBeenCalledWith("https://example.com/cat.png", undefined);
expect(lastPayload()).toMatchObject({
title: "Active page",
images: [ {
@@ -1052,6 +1054,22 @@ describe("background", () => {
]);
});
+ it("saves the tabs without a favicon that does not arrive in time", async () => {
+ tabs = [
+ { id: 1, title: "Slow", url: "https://a.com/", favIconUrl: "https://a.com/favicon.ico" }
+ ];
+ const timeout = vi.spyOn(AbortSignal, "timeout").mockImplementation(() => AbortSignal.abort());
+ fetchMock.mockImplementationOnce((_url: string, init?: RequestInit) => (init?.signal?.aborted
+ ? Promise.reject(new DOMException("The operation timed out.", "TimeoutError"))
+ : new Promise(() => {})));
+
+ await onCommand("saveTabs");
+
+ expect(timeout).toHaveBeenCalledWith(OPTIONAL_REQUEST_TIMEOUT_MS);
+ expect((lastPayload() as { tabs: unknown }).tabs)
+ .toEqual([ { url: "https://a.com/", title: "Slow", favicon: undefined } ]);
+ });
+
it("adds no ellipsis for up to three tabs and shows why saving failed", async () => {
tabs = [
{ id: 1, title: "A", url: "https://a.com/" },
diff --git a/apps/web-clipper/entrypoints/background/index.ts b/apps/web-clipper/entrypoints/background/index.ts
index b4d275d2b1..8f0737b8a9 100644
--- a/apps/web-clipper/entrypoints/background/index.ts
+++ b/apps/web-clipper/entrypoints/background/index.ts
@@ -1,7 +1,9 @@
import {
MAX_PART_HEIGHT, type ScreenshotSlice, ScreenshotStitcher, type StitchMessage, type StitchRequest
} from "@/screenshot";
-import { extractPage, isPdfTab, randomString, type Rect, type SaveOutcome, type Saved } from "@/utils";
+import {
+ extractPage, isPdfTab, OPTIONAL_REQUEST_TIMEOUT_MS, randomString, type Rect, type SaveOutcome, type Saved
+} from "@/utils";
import setupContextMenu from "./context_menu";
import TriliumServerFacade, { TriliumError } from "./trilium_server_facade";
@@ -439,8 +441,8 @@ export default defineBackground(() => {
});
}
- async function fetchImage(url: string) {
- const resp = await fetch(url);
+ async function fetchImage(url: string, init?: RequestInit) {
+ const resp = await fetch(url, init);
if (!resp.ok) {
throw new Error(`HTTP ${resp.status}`);
}
@@ -709,7 +711,7 @@ export default defineBackground(() => {
async function fetchFavicon(url: string | undefined) {
if (!url) return undefined;
try {
- return await fetchImage(url);
+ return await fetchImage(url, { signal: AbortSignal.timeout(OPTIONAL_REQUEST_TIMEOUT_MS) });
} catch {
return undefined;
}
diff --git a/apps/web-clipper/utils.spec.ts b/apps/web-clipper/utils.spec.ts
index 770bea5be2..2bc983bfbe 100644
--- a/apps/web-clipper/utils.spec.ts
+++ b/apps/web-clipper/utils.spec.ts
@@ -2,7 +2,9 @@ import type { Window as HappyDOMWindow } from "happy-dom";
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { fakeBrowser } from "wxt/testing/fake-browser";
-import { createLink, extractPage, getPageLocationOrigin, isPdfTab, randomString } from "./utils";
+import {
+ createLink, extractPage, getPageLocationOrigin, isPdfTab, OPTIONAL_REQUEST_TIMEOUT_MS, randomString
+} from "./utils";
describe("randomString", () => {
it("returns alphanumeric strings of the requested length", () => {
@@ -114,7 +116,9 @@ describe("isPdfTab", () => {
answer("Application/PDF; qs=0.001");
expect(await isPdfTab(TAB)).toBe(true);
- expect(fetchMock).toHaveBeenCalledWith(TAB.url, { method: "HEAD", credentials: "include" });
+ expect(fetchMock).toHaveBeenCalledWith(TAB.url, {
+ method: "HEAD", credentials: "include", signal: expect.any(AbortSignal)
+ });
answer("text/html");
expect(await isPdfTab(TAB)).toBe(false);
@@ -124,6 +128,17 @@ describe("isPdfTab", () => {
expect(await isPdfTab(TAB)).toBe(false);
});
+ it("gives up on a website that does not answer in time", async () => {
+ executeScript.mockRejectedValue(new Error("Missing host permission for the tab"));
+ const timeout = vi.spyOn(AbortSignal, "timeout").mockImplementation(() => AbortSignal.abort());
+ fetchMock.mockImplementation((_url: string, init?: RequestInit) => (init?.signal?.aborted
+ ? Promise.reject(new DOMException("The operation timed out.", "TimeoutError"))
+ : new Promise(() => {})));
+
+ expect(await isPdfTab(TAB)).toBe(false);
+ expect(timeout).toHaveBeenCalledWith(OPTIONAL_REQUEST_TIMEOUT_MS);
+ });
+
it("checks only web pages in a tab it can address", async () => {
expect(await isPdfTab({ id: 7, url: "chrome://settings" })).toBe(false);
expect(await isPdfTab({ id: 7 })).toBe(false);
diff --git a/apps/web-clipper/utils.ts b/apps/web-clipper/utils.ts
index 2b132571ce..2caaa6ad41 100644
--- a/apps/web-clipper/utils.ts
+++ b/apps/web-clipper/utils.ts
@@ -1,5 +1,8 @@
export type Rect = { x: number, y: number, width: number, height: number };
+/** How long a request the extension can do without, such as a favicon, has to answer, in milliseconds. */
+export const OPTIONAL_REQUEST_TIMEOUT_MS = 3000;
+
/** An image of a clipping; the clipping's `content` references it by `imageId`. */
export type ClippedImage = { imageId: string, src: string };
@@ -54,7 +57,8 @@ export async function isPdfTab({ id, url }: { id?: number, url?: string }) {
return injection?.result === true;
} catch {
try {
- const response = await fetch(url, { method: "HEAD", credentials: "include" });
+ const signal = AbortSignal.timeout(OPTIONAL_REQUEST_TIMEOUT_MS);
+ const response = await fetch(url, { method: "HEAD", credentials: "include", signal });
return isPdfType(response.headers.get("content-type"));
} catch {
return false;