File tree Expand file tree Collapse file tree 3 files changed +23
-9
lines changed Expand file tree Collapse file tree 3 files changed +23
-9
lines changed Original file line number Diff line number Diff line change @@ -7,16 +7,22 @@ Headless-cluster enables authenticated proxy support. Pass a data object to clus
7
7
8
8
``` js
9
9
// Create a cluster with 2 workers
10
+ // You can also use Cluster.CONCURRENCY_BROWSER
10
11
const cluster = await Cluster .launch ({
11
- concurrency: Cluster .CONCURRENCY_BROWSER ,
12
+ concurrency: Cluster .CONCURRENCY_CONTEXT ,
12
13
maxConcurrency: 2 ,
13
14
});
14
15
15
16
// Define a task
16
17
await cluster .task (async ({ page, data }) => {
18
+ try {
17
19
await page .goto (data .url );
18
- const pageTitle = await page .evaluate (() => document .title );
19
- return pageTitle;
20
+ } catch (err) {
21
+ console .log (err);
22
+ return ' Failed to load the page' ;
23
+ }
24
+ const pageTitle = await page .evaluate (() => document .title );
25
+ return pageTitle;
20
26
});
21
27
22
28
// Use try-catch block as "execute" will throw instead of using events
Original file line number Diff line number Diff line change @@ -2,16 +2,22 @@ const { Cluster } = require('../dist');
2
2
3
3
( async ( ) => {
4
4
// Create a cluster with 2 workers
5
+ // You can also use Cluster.CONCURRENCY_BROWSER
5
6
const cluster = await Cluster . launch ( {
6
- concurrency : Cluster . CONCURRENCY_BROWSER ,
7
+ concurrency : Cluster . CONCURRENCY_CONTEXT ,
7
8
maxConcurrency : 2 ,
8
9
} ) ;
9
10
10
11
// Define a task
11
12
await cluster . task ( async ( { page, data } ) => {
13
+ try {
12
14
await page . goto ( data . url ) ;
13
- const pageTitle = await page . evaluate ( ( ) => document . title ) ;
14
- return pageTitle ;
15
+ } catch ( err ) {
16
+ console . log ( err ) ;
17
+ return 'Failed to load the page' ;
18
+ }
19
+ const pageTitle = await page . evaluate ( ( ) => document . title ) ;
20
+ return pageTitle ;
15
21
} ) ;
16
22
17
23
// Use try-catch block as "execute" will throw instead of using events
@@ -32,4 +38,4 @@ const { Cluster } = require('../dist');
32
38
// Shutdown after everything is done
33
39
await cluster . idle ( ) ;
34
40
await cluster . close ( ) ;
35
- } ) ( ) ;
41
+ } ) ( ) ;
Original file line number Diff line number Diff line change @@ -6,10 +6,12 @@ import SingleBrowserImplementation from '../SingleBrowserImplementation';
6
6
7
7
export default class Context extends SingleBrowserImplementation {
8
8
9
- protected async createResources ( ) : Promise < ResourceData > {
9
+ protected async createResources ( data : any ) : Promise < ResourceData > {
10
10
const context = await ( this . browser as puppeteer . Browser )
11
- . createBrowserContext ( ) ;
11
+ . createBrowserContext ( data . contextOptions || { } ) ;
12
12
const page = await context . newPage ( ) ;
13
+ if ( data . authentication )
14
+ page . authenticate ( data . authentication ) ;
13
15
return {
14
16
context,
15
17
page,
You can’t perform that action at this time.
0 commit comments