@crawlee/browser 4.0.0-beta.95 → 4.0.0-beta.96

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -339,6 +339,15 @@ export declare abstract class BrowserCrawler<Page extends CommonPage = CommonPag
339
339
  private prepareNavigation;
340
340
  private navigate;
341
341
  private finalizeNavigation;
342
+ /**
343
+ * Copies cookies from the live browser page into the session cookie jar.
344
+ */
345
+ private persistCookiesFromPage;
346
+ /**
347
+ * Runs the user request handler, then re-reads browser cookies so login flows /
348
+ * `page.setCookie` / XHR `Set-Cookie` updates are stored for later requests.
349
+ */
350
+ protected runRequestHandler(crawlingContext: ExtendedContext): Promise<void>;
342
351
  private handleBlockedRequestByContent;
343
352
  private restoreRequestState;
344
353
  private applyCookies;
@@ -272,23 +272,51 @@ export class BrowserCrawler extends BasicCrawler {
272
272
  }
273
273
  await this.processResponse(response, crawlingContext);
274
274
  tryCancel();
275
- // TODO: Should we save the cookies also after/only the handle page?
276
- if (this.saveResponseCookies && crawlingContext.session) {
277
- const { cookies } = await this.browserPool.extractPageState(crawlingContext.page);
278
- tryCancel();
279
- const url = crawlingContext.request.loadedUrl;
280
- for (const cookie of cookies) {
275
+ // Persist cookies from the navigation response before the user handler runs.
276
+ // Cookies set during `requestHandler` are saved again afterwards.
277
+ await this.persistCookiesFromPage(crawlingContext);
278
+ return { request: crawlingContext.request };
279
+ }
280
+ /**
281
+ * Copies cookies from the live browser page into the session cookie jar.
282
+ */
283
+ async persistCookiesFromPage(crawlingContext) {
284
+ if (!this.saveResponseCookies || !crawlingContext.session) {
285
+ return;
286
+ }
287
+ const { cookies } = await this.browserPool.extractPageState(crawlingContext.page);
288
+ tryCancel();
289
+ // Prefer the live page URL — the handler may have navigated after the initial load.
290
+ const url = (await crawlingContext.page.url()) || crawlingContext.request.loadedUrl || crawlingContext.request.url;
291
+ for (const cookie of cookies) {
292
+ try {
293
+ crawlingContext.session.cookieJar.setCookieSync(browserPoolCookieToToughCookie(cookie), url, {
294
+ ignoreError: false,
295
+ });
296
+ }
297
+ catch (e) {
298
+ this.log.debug(`Could not set cookie: ${e.message}`);
299
+ }
300
+ }
301
+ }
302
+ /**
303
+ * Runs the user request handler, then re-reads browser cookies so login flows /
304
+ * `page.setCookie` / XHR `Set-Cookie` updates are stored for later requests.
305
+ */
306
+ async runRequestHandler(crawlingContext) {
307
+ try {
308
+ await super.runRequestHandler(crawlingContext);
309
+ }
310
+ finally {
311
+ if (!crawlingContext.request.skipNavigation) {
281
312
  try {
282
- crawlingContext.session.cookieJar.setCookieSync(browserPoolCookieToToughCookie(cookie), url, {
283
- ignoreError: false,
284
- });
313
+ await this.persistCookiesFromPage(crawlingContext);
285
314
  }
286
- catch (e) {
287
- this.log.debug(`Could not set cookie: ${e.message}`);
315
+ catch {
316
+ // Page may already be closed on some failure paths; ignore.
288
317
  }
289
318
  }
290
319
  }
291
- return { request: crawlingContext.request };
292
320
  }
293
321
  async handleBlockedRequestByContent(crawlingContext) {
294
322
  if (this.retryOnBlocked) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@crawlee/browser",
3
- "version": "4.0.0-beta.95",
3
+ "version": "4.0.0-beta.96",
4
4
  "description": "The scalable web crawling and scraping library for JavaScript/Node.js. Enables development of data extraction and web automation jobs (not only) with headless Chrome and Puppeteer.",
5
5
  "engines": {
6
6
  "node": ">=22.0.0"
@@ -48,10 +48,10 @@
48
48
  },
49
49
  "dependencies": {
50
50
  "@apify/timeout": "^0.3.2",
51
- "@crawlee/basic": "4.0.0-beta.95",
52
- "@crawlee/browser-pool": "4.0.0-beta.95",
53
- "@crawlee/types": "4.0.0-beta.95",
54
- "@crawlee/utils": "4.0.0-beta.95",
51
+ "@crawlee/basic": "4.0.0-beta.96",
52
+ "@crawlee/browser-pool": "4.0.0-beta.96",
53
+ "@crawlee/types": "4.0.0-beta.96",
54
+ "@crawlee/utils": "4.0.0-beta.96",
55
55
  "ow": "^2.0.0",
56
56
  "tslib": "^2.8.1",
57
57
  "type-fest": "^4.41.0"
@@ -75,5 +75,5 @@
75
75
  }
76
76
  }
77
77
  },
78
- "gitHead": "ba8602d011706fb5c9930232402156eb5b657eb3"
78
+ "gitHead": "b5c20fe6cecad57a6e3dd26b4137776d4b62588a"
79
79
  }