Skip to content

Commit 200e9fc

Browse files
committed
Improve browser session configuration and navigation handling
- Add `--no-sandbox` Chrome argument for better compatibility - Set default navigation timeout to 15 seconds - Add extra HTTP headers to mimic browser behavior - Simplify navigation timeout and error handling - Adjust navigation wait conditions for more reliable page loading
1 parent d6c1df7 commit 200e9fc

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

src/services/browser/BrowserSession.ts

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,7 @@ export class BrowserSession {
8585
this.browser = await stats.puppeteer.launch({
8686
args: [
8787
"--user-agent=Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36",
88+
"--no-sandbox",
8889
],
8990
executablePath: stats.executablePath,
9091
defaultViewport: (() => {
@@ -95,6 +96,13 @@ export class BrowserSession {
9596
})
9697
// (latest version of puppeteer does not add headless to user agent)
9798
this.page = await this.browser?.newPage()
99+
await this.page?.setDefaultNavigationTimeout(15000)
100+
await this.page?.setExtraHTTPHeaders({
101+
Accept: "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,*/*;q=0.8",
102+
"Accept-Language": "en-US,en;q=0.5",
103+
Connection: "keep-alive",
104+
"Upgrade-Insecure-Requests": "1",
105+
})
98106
}
99107
}
100108

@@ -152,14 +160,6 @@ export class BrowserSession {
152160
} catch (err) {
153161
if (!(err instanceof TimeoutError)) {
154162
logs.push(`[Error] ${err.toString()}`)
155-
const currentUrl = this.page?.url() || ""
156-
await this.closeBrowser()
157-
return {
158-
screenshot: "",
159-
logs: logs.join("\n"),
160-
currentUrl,
161-
currentMousePosition: this.currentMousePosition,
162-
}
163163
}
164164
}
165165

@@ -215,7 +215,7 @@ export class BrowserSession {
215215
async navigateToUrl(url: string): Promise<BrowserActionResult> {
216216
return this.doAction(async (page) => {
217217
// networkidle2 isn't good enough since page may take some time to load. we can assume locally running dev sites will reach networkidle0 in a reasonable amount of time
218-
await page.goto(url, { timeout: 7_000, waitUntil: ["domcontentloaded", "networkidle2"] })
218+
await page.goto(url, { timeout: 15_000, waitUntil: "domcontentloaded" })
219219
// await page.goto(url, { timeout: 10_000, waitUntil: "load" })
220220
await this.waitTillHTMLStable(page) // in case the page is loading more resources
221221
})
@@ -273,14 +273,12 @@ export class BrowserSession {
273273

274274
if (hasNetworkActivity) {
275275
// If we detected network activity, wait for navigation/loading
276-
try {
277-
await page.waitForNavigation({
278-
waitUntil: ["domcontentloaded", "networkidle2"],
279-
timeout: 7000,
276+
await page
277+
.waitForNavigation({
278+
waitUntil: "domcontentloaded",
279+
timeout: 15000,
280280
})
281-
} catch (err) {
282-
// Ignore navigation timeout errors
283-
}
281+
.catch(() => {})
284282
await this.waitTillHTMLStable(page)
285283
}
286284

0 commit comments

Comments
 (0)