@@ -55,16 +55,6 @@ export async function crawl(config: Config) {
55
55
const crawler = new PlaywrightCrawler ( {
56
56
// Use the requestHandler to process each of the crawled pages.
57
57
async requestHandler ( { request, page, enqueueLinks, log, pushData } ) {
58
- if ( config . cookie ) {
59
- // Set the cookie for the specific URL
60
- const cookie = {
61
- name : config . cookie . name ,
62
- value : config . cookie . value ,
63
- url : request . loadedUrl ,
64
- } ;
65
- await page . context ( ) . addCookies ( [ cookie ] ) ;
66
- }
67
-
68
58
const title = await page . title ( ) ;
69
59
pageCounter ++ ;
70
60
log . info (
@@ -108,12 +98,24 @@ export async function crawl(config: Config) {
108
98
// headless: false,
109
99
preNavigationHooks : [
110
100
// Abort requests for certain resource types
111
- async ( { page, log } ) => {
101
+ async ( { request , page, log } ) => {
112
102
// If there are no resource exclusions, return
113
103
const RESOURCE_EXCLUSTIONS = config . resourceExclusions ?? [ ] ;
114
104
if ( RESOURCE_EXCLUSTIONS . length === 0 ) {
115
105
return ;
116
106
}
107
+ if ( config . cookie ) {
108
+ const cookies = (
109
+ Array . isArray ( config . cookie ) ? config . cookie : [ config . cookie ]
110
+ ) . map ( ( cookie ) => {
111
+ return {
112
+ name : cookie . name ,
113
+ value : cookie . value ,
114
+ url : request . loadedUrl ,
115
+ } ;
116
+ } ) ;
117
+ await page . context ( ) . addCookies ( cookies ) ;
118
+ }
117
119
await page . route ( `**\/*.{${ RESOURCE_EXCLUSTIONS . join ( ) } }` , ( route ) =>
118
120
route . abort ( "aborted" ) ,
119
121
) ;
0 commit comments