Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
fix: keep the adaptive crawler's detection rerun from seeing afterStorageCommit writes (#4310)
Nikita Chapovskii79ae8889a274
diff --git a/packages/playwright-crawler/src/internals/adaptive-playwright-crawler.ts b/packages/playwright-crawler/src/internals/adaptive-playwright-crawler.tsindex 15003f081..90a02a9d7 100644--- a/packages/playwright-crawler/src/internals/adaptive-playwright-crawler.ts+++ b/packages/playwright-crawler/src/internals/adaptive-playwright-crawler.ts@@ -802,16 +802,21 @@ export class AdaptivePlaywrightCrawler<transactions,);- // A failed browser attempt is handed to the retry logic with its writes - `userData` stashed for the- // next attempt must survive.- this.#keepContextModifications(browserRun.contextModifications);-if (!browserRun.ok) {+ // A failed browser attempt is handed to the retry logic with its writes - `userData` stashed for the+ // next attempt must survive.+ this.#keepContextModifications(browserRun.contextModifications);throw browserRun.error;}- browserRun.logs.forEach(([log, method, ...args]) => log[method](...(args as [any, any])));- await browserRun.transaction.commit();+ try {+ browserRun.logs.forEach(([log, method, ...args]) => log[method](...(args as [any, any])));+ // The `afterStorageCommit` callbacks belong to this attempt, so the detection rerun undoes their+ // writes too - the journals stay open until they have run.+ await browserRun.transaction.commit();+ } finally {+ this.#keepContextModifications(browserRun.contextModifications);+ }// Past the shutdown fence, `teardown()` has already drained - and possibly torn down the predictor.if (httpResultRejected && !this.#shutDown) {diff --git a/test/core/crawlers/adaptive-playwright-crawler.test.ts b/test/core/crawlers/adaptive-playwright-crawler.test.tsindex 411f8957e..747794089 100644--- a/test/core/crawlers/adaptive-playwright-crawler.test.ts+++ b/test/core/crawlers/adaptive-playwright-crawler.test.ts@@ -940,6 +940,38 @@ describe('AdaptivePlaywrightCrawler', () => {expect(seenUserData).toEqual([{ seeded: true }, { seeded: true }]);expect(browserRequest?.userData).toEqual({ seeded: true, attempts: 1 });});++ test('the detection rerun (HTTP) does not see what the browser attempt wrote in afterStorageCommit', async () => {+ const renderingTypePredictor = makeRiggedRenderingTypePredictor({+ detectionProbabilityRecommendation: 1,+ renderingType: 'clientOnly',+ });++ const seen: Dictionary[] = [];+ const requestHandler: AdaptivePlaywrightCrawlerOptions['requestHandler'] = async ({+ request,+ useState,+ afterStorageCommit,+ }) => {+ const state = await useState<Dictionary>();+ seen.push({ state: { ...state }, userData: { ...request.userData } });+ afterStorageCommit(() => {+ state.stored = true;+ request.userData.stored = true;+ });+ };++ const crawler = await makeOneshotCrawler({ requestHandler, renderingTypePredictor }, [+ `http://${HOSTNAME}:${port}/static`,+ ]);+ await crawler.run();++ expect(seen).toEqual([+ { state: {}, userData: {} },+ { state: {}, userData: {} },+ ]);+ expect(await (await KeyValueStore.open()).getValue('CRAWLEE_STATE')).toEqual({ stored: true });+ });});test('should return deeply equal but not identical state objects across handler runs', async () => {