Inconsistent Puppeteer page navigation behavior across browsers on Heroku Node.js

I’m having trouble with Puppeteer for PDF generation on Heroku. It works fine in DuckDuckGo but fails in Chrome and Edge. Here’s what happens:

  1. Login works in all browsers
  2. Dashboard loads correctly
  3. In Chrome and Edge, the report page loads but loses user credentials

This results in a blank report for Chrome and Edge, while DuckDuckGo produces the expected PDF. I’ve been stuck on this for a week. Any ideas?

Here’s a simplified version of my code:

const browser = await puppeteer.launch({args: ['--no-sandbox']});
const page = await browser.newPage();

await page.goto('https://example.com/login');
await page.type('#username', 'myuser');
await page.type('#password', 'mypass');
await page.click('#login-button');

await page.waitForNavigation();
await page.screenshot({path: 'dashboard.jpg'});

await page.goto('https://example.com/report');
await page.screenshot({path: 'report.jpg'});

const pdf = await page.pdf({
  path: 'report.pdf',
  format: 'A4',
  printBackground: true
});

return {FilePath: '/reports/report.pdf', FileName: 'report.pdf'};

Any help would be appreciated!

I’ve dealt with similar Puppeteer issues on Heroku before. One thing that often helps is explicitly setting cookies after login. Try something like this after your login step:

const cookies = await page.cookies();
await page.setCookie(…cookies);

Also, consider adding a slight delay between navigation and PDF generation:

await page.goto(‘https://example.com/report’);
await page.waitForTimeout(2000);
const pdf = await page.pdf({…});

These tweaks have resolved many of my cross-browser inconsistencies with Puppeteer on Heroku. If you’re still having trouble, you might want to look into using a service like Browserless.io, which can provide more stable Puppeteer environments.

hey mate, have u tried clearin browser cache n cookies b4 each run? sometimes that helps with weird session stuff. also, maybe check ur network requests in dev tools to see if theres any diff between browsers. could be some funky headers or smth. just a thought!

I’ve encountered similar issues with Puppeteer on Heroku before, and it sounds like you might be dealing with a session management problem. In my experience, the issue often stems from how different browsers handle cookies and local storage.

Here’s what worked for me:

  1. Use a persistent context instead of creating a new page each time. This helps maintain the session across navigations.

  2. Implement explicit waits for specific elements on each page, rather than just waiting for navigation. This ensures the page has fully loaded before proceeding.

  3. Consider using localStorage or sessionStorage to store and retrieve authentication tokens, then inject them into the page before navigation.

  4. If possible, try implementing a headless authentication flow that doesn’t rely on UI interaction.

Lastly, make sure your Heroku dyno has enough memory allocated, as Puppeteer can be resource-intensive. If you’re on a free tier, you might hit memory limits that cause inconsistent behavior.

Hope this helps point you in the right direction. Let me know if you need more specific guidance!