Skip to content

A self-hostable bookmark-everything app (links, notes and images) with AI-based automatic tagging and full text search

fix(workers): wait for autoconsent in parallel with page load

Mohamed Bassem98c52b85c275
diff --git a/apps/workers/workers/crawler/autoconsent.ts b/apps/workers/workers/crawler/autoconsent.ts
index 2c7062d1..aba120cc 100644
--- a/apps/workers/workers/crawler/autoconsent.ts
+++ b/apps/workers/workers/crawler/autoconsent.ts
@@ -247,3 +247,24 @@ export async function waitForAutoconsent(
abortRaceResolve(abortSignal, undefined),
);
}
+
+/**
+ * Lets autoconsent use the time already spent waiting for the page to settle.
+ *
+ * No-CMP detection can take longer than our autoconsent budget because the
+ * library retries detection several times. Waiting for it only after the page
+ * load wait therefore adds the full timeout to ordinary pages. Start both
+ * waits together instead. If a CMP appears after the initial autoconsent wait
+ * timed out, wait once more for the actual opt-out action before capture.
+ */
+export async function waitForPageLoadAndAutoconsent(
+ pageLoad: Promise<unknown>,
+ handle: AutoconsentHandle | undefined,
+ abortSignal: AbortSignal,
+): Promise<void> {
+ await Promise.all([pageLoad, waitForAutoconsent(handle, abortSignal)]);
+
+ if (handle?.cmpDetected()) {
+ await waitForAutoconsent(handle, abortSignal);
+ }
+}
diff --git a/apps/workers/workers/crawler/crawlPage.ts b/apps/workers/workers/crawler/crawlPage.ts
index d2fb64dc..43a2393a 100644
--- a/apps/workers/workers/crawler/crawlPage.ts
+++ b/apps/workers/workers/crawler/crawlPage.ts
@@ -32,7 +32,10 @@ import logger from "@karakeep/shared/logger";
import { tryCatch } from "@karakeep/shared/tryCatch";
import type { AutoconsentHandle } from "./autoconsent";
-import { installAutoconsent, waitForAutoconsent } from "./autoconsent";
+import {
+ installAutoconsent,
+ waitForPageLoadAndAutoconsent,
+} from "./autoconsent";
import {
CONTEXT_CLOSE_TIMEOUT_MS,
PAGE_CLOSE_TIMEOUT_MS,
@@ -719,7 +722,7 @@ export async function crawlPage(
);
// Wait until network is relatively idle or timeout after 5 seconds
- await withSpan(
+ const pageLoad = withSpan(
tracer,
"crawlerWorker.crawlPage.waitForLoadState",
{
@@ -740,18 +743,18 @@ export async function crawlPage(
},
);
+ await waitForPageLoadAndAutoconsent(
+ pageLoad,
+ autoconsentHandle,
+ abortSignal,
+ );
+
abortSignal.throwIfAborted();
logger.info(
`[Crawler][${jobId}] Finished waiting for the page to load.`,
);
- // If a consent dialog was detected, give autoconsent up to 3s to finish
- // opting out before we capture the HTML/screenshot/PDF. No CMP → no
- // added latency.
- await waitForAutoconsent(autoconsentHandle, abortSignal);
- abortSignal.throwIfAborted();
-
const [htmlContent, screenshot, pdf] = await capturePageAssets(
activePage,
jobId,