@@ -104,25 +104,28 @@ export async function crawl(config: Config) {
104
104
// Uncomment this option to see the browser window.
105
105
// headless: false,
106
106
preNavigationHooks : [
107
- // Abort requests for certain resource types
108
- async ( { request, page, log } ) => {
109
- // If there are no resource exclusions, return
110
- const RESOURCE_EXCLUSTIONS = config . resourceExclusions ?? [ ] ;
111
- if ( RESOURCE_EXCLUSTIONS . length === 0 ) {
112
- return ;
113
- }
107
+ // Abort requests for certain resource types and add cookies
108
+ async ( crawlingContext , _gotoOptions ) => {
109
+ const { request, page, log } = crawlingContext ;
110
+ // Add cookies to the page
111
+ // Because the crawler has not yet navigated to the page, so the loadedUrl is always undefined. Use the request url instead.
114
112
if ( config . cookie ) {
115
113
const cookies = (
116
114
Array . isArray ( config . cookie ) ? config . cookie : [ config . cookie ]
117
115
) . map ( ( cookie ) => {
118
116
return {
119
117
name : cookie . name ,
120
118
value : cookie . value ,
121
- url : request . loadedUrl ,
119
+ url : request . url ,
122
120
} ;
123
121
} ) ;
124
122
await page . context ( ) . addCookies ( cookies ) ;
125
123
}
124
+ const RESOURCE_EXCLUSTIONS = config . resourceExclusions ?? [ ] ;
125
+ // If there are no resource exclusions, return
126
+ if ( RESOURCE_EXCLUSTIONS . length === 0 ) {
127
+ return ;
128
+ }
126
129
await page . route (
127
130
`**\/*.{${ RESOURCE_EXCLUSTIONS . join ( ) } }` ,
128
131
( route ) => route . abort ( "aborted" ) ,
0 commit comments