Skip to content

Commit 567ab0b

Browse files
committed
fix: correctly set cookies
1 parent 2ca876e commit 567ab0b

File tree

1 file changed

+11
-8
lines changed

1 file changed

+11
-8
lines changed

src/core.ts

+11-8
Original file line numberDiff line numberDiff line change
@@ -104,25 +104,28 @@ export async function crawl(config: Config) {
104104
// Uncomment this option to see the browser window.
105105
// headless: false,
106106
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.
114112
if (config.cookie) {
115113
const cookies = (
116114
Array.isArray(config.cookie) ? config.cookie : [config.cookie]
117115
).map((cookie) => {
118116
return {
119117
name: cookie.name,
120118
value: cookie.value,
121-
url: request.loadedUrl,
119+
url: request.url,
122120
};
123121
});
124122
await page.context().addCookies(cookies);
125123
}
124+
const RESOURCE_EXCLUSTIONS = config.resourceExclusions ?? [];
125+
// If there are no resource exclusions, return
126+
if (RESOURCE_EXCLUSTIONS.length === 0) {
127+
return;
128+
}
126129
await page.route(
127130
`**\/*.{${RESOURCE_EXCLUSTIONS.join()}}`,
128131
(route) => route.abort("aborted"),

0 commit comments

Comments
 (0)