Skip to content

Commit f8657f7

Browse files
committed
fix(puppeteer): make Chromium launch + navigation tolerant in CI
1 parent 3c2f493 commit f8657f7

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

src/utils/pdf.test.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,8 @@ describe("pdf utils", () => {
124124
</body>
125125
</html>`;
126126
expect(mockPage.setContent).toHaveBeenCalledWith(expectedHtml, {
127-
waitUntil: "networkidle0",
127+
timeout: 60000,
128+
waitUntil: "networkidle2",
128129
});
129130
expect(mockPage.pdf).toHaveBeenCalledWith({
130131
path: outputFilePath,

src/utils/pdf.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,28 @@ export async function convertHtmlToPdf(
6060
}
6161
}
6262

63-
await page.setContent(finalHtml, { waitUntil: "networkidle0" });
63+
// Use a slightly more permissive network idle and increase the timeout to
64+
// avoid flaky CI failures when external resources are slow or blocked.
65+
// Also set a default navigation timeout on the page so Puppeteer's internal
66+
// navigation watchers use the same timeout value.
67+
const navigationTimeout = 60000; // 60s
68+
const maybeSetDefaultNavTimeout = page as unknown as {
69+
setDefaultNavigationTimeout?: (t: number) => void;
70+
};
71+
maybeSetDefaultNavTimeout.setDefaultNavigationTimeout?.(navigationTimeout);
72+
73+
try {
74+
await page.setContent(finalHtml, { waitUntil: "networkidle2", timeout: navigationTimeout });
75+
} catch (err) {
76+
// If setContent times out, warn and continue: in CI some external requests
77+
// (fonts, images) may hang or be blocked and we still want to produce a PDF.
78+
const msg = err instanceof Error ? err.message : String(err);
79+
if (/Navigation timeout|TimeoutError/i.test(msg)) {
80+
console.warn("Warning: page.setContent timed out - continuing without full network idle.");
81+
} else {
82+
throw err;
83+
}
84+
}
6485

6586
await page.pdf({
6687
path: outputFilePath,

0 commit comments

Comments
 (0)