Skip to content

Commit 118515d

Browse files
gustavotrB4nan
andauthored
fix: add skipNavigation option to enqueueLinks (#2153)
Co-authored-by: Martin Adámek <banan23@gmail.com>
1 parent f188ebe commit 118515d

File tree

6 files changed

+46
-3
lines changed

6 files changed

+46
-3
lines changed

packages/core/src/enqueue_links/enqueue_links.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,12 @@ export interface EnqueueLinksOptions extends RequestQueueOperationOptions {
3535
/** Sets {@apilink Request.label} for newly enqueued requests. */
3636
label?: string;
3737

38+
/**
39+
* If set to `true`, tells the crawler to skip navigation and process the request directly.
40+
* @default false
41+
*/
42+
skipNavigation?: boolean;
43+
3844
/**
3945
* A base URL that will be used to resolve relative URLs when using Cheerio. Ignored when using Puppeteer,
4046
* since the relative URL resolution is done inside the browser automatically.
@@ -239,6 +245,7 @@ export async function enqueueLinks(options: SetRequired<EnqueueLinksOptions, 're
239245
urls: ow.array.ofType(ow.string),
240246
requestQueue: ow.object.hasKeys('fetchNextRequest', 'addRequest'),
241247
forefront: ow.optional.boolean,
248+
skipNavigation: ow.optional.boolean,
242249
limit: ow.optional.number,
243250
selector: ow.optional.string,
244251
baseUrl: ow.optional.string,

packages/core/src/enqueue_links/shared.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ export function filterRequestsByPatterns(requests: Request[], patterns?: UrlPatt
208208
*/
209209
export function createRequestOptions(
210210
sources: (string | Record<string, unknown>)[],
211-
options: Pick<EnqueueLinksOptions, 'label' | 'userData' | 'baseUrl'> = {},
211+
options: Pick<EnqueueLinksOptions, 'label' | 'userData' | 'baseUrl' | 'skipNavigation'> = {},
212212
): RequestOptions[] {
213213
return sources
214214
.map((src) => (typeof src === 'string' ? { url: src } : src as unknown as RequestOptions))
@@ -230,6 +230,10 @@ export function createRequestOptions(
230230
};
231231
}
232232

233+
if (options.skipNavigation) {
234+
requestOptions.skipNavigation = true;
235+
}
236+
233237
return requestOptions;
234238
});
235239
}

packages/playwright-crawler/src/internals/enqueue-links/click-elements.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,12 @@ export interface EnqueueLinksByClickingElementsOptions {
166166
* @default false
167167
*/
168168
forefront?: boolean;
169+
170+
/**
171+
* If set to `true`, tells the crawler to skip navigation and process the request directly.
172+
* @default false
173+
*/
174+
skipNavigation?: boolean;
169175
}
170176

171177
/**
@@ -233,6 +239,7 @@ export async function enqueueLinksByClickingElements(options: EnqueueLinksByClic
233239
maxWaitForPageIdleSecs: ow.optional.number,
234240
label: ow.optional.string,
235241
forefront: ow.optional.boolean,
242+
skipNavigation: ow.optional.boolean,
236243
}));
237244

238245
const {

packages/puppeteer-crawler/src/internals/enqueue-links/click-elements.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -167,6 +167,12 @@ export interface EnqueueLinksByClickingElementsOptions {
167167
* @default false
168168
*/
169169
forefront?: boolean;
170+
171+
/**
172+
* If set to `true`, tells the crawler to skip navigation and process the request directly.
173+
* @default false
174+
*/
175+
skipNavigation?: boolean;
170176
}
171177

172178
/**
@@ -234,6 +240,7 @@ export async function enqueueLinksByClickingElements(options: EnqueueLinksByClic
234240
maxWaitForPageIdleSecs: ow.optional.number,
235241
label: ow.optional.string,
236242
forefront: ow.optional.boolean,
243+
skipNavigation: ow.optional.boolean,
237244
}));
238245

239246
const {

test/core/autoscaling/autoscaled_pool.test.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -541,5 +541,3 @@ describe('AutoscaledPool', () => {
541541
expect(Date.now() - now).toBeGreaterThanOrEqual(1e3);
542542
}, 10e3);
543543
});
544-
545-
/* eslint-enable no-underscore-dangle */

test/core/enqueue_links/enqueue_links.test.ts

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,26 @@ describe('enqueueLinks()', () => {
189189
expect(enqueued[2].userData).toEqual({ label: 'COOL' });
190190
});
191191

192+
test('works with skipNavigation', async () => {
193+
const { enqueued, requestQueue } = createRequestQueueMock();
194+
195+
await browserCrawlerEnqueueLinks({
196+
options: {
197+
selector: '.click',
198+
skipNavigation: true,
199+
},
200+
page,
201+
requestQueue,
202+
originalRequestUrl: 'https://example.com',
203+
});
204+
205+
expect(enqueued).toHaveLength(2);
206+
207+
for (const request of enqueued) {
208+
expect(request.skipNavigation).toBe(true);
209+
}
210+
});
211+
192212
test('works with exclude glob', async () => {
193213
const { enqueued, requestQueue } = createRequestQueueMock();
194214
const globs = [

0 commit comments

Comments
 (0)