User Agents

Web browsers and everyday web tech, explained

Keep Puppeteer Running When Chrome Is Minimized

· 4 min read · Browsers

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

By default, Chromium (the browser Puppeteer controls) aggressively throttles background tabs, pauses timers, and can even freeze rendering when the window is minimized. This causes scripts to hang, time out, or produce inconsistent results. To keep Puppeteer running reliably while minimized, you need to launch the browser with specific flags that disable background throttling and ensure the page stays active. Below is a practical guide to achieve that, including code snippets and troubleshooting.

What You'll Need

  • Node.js (version 12 or later) installed.
  • Puppeteer installed in your project (npm install puppeteer).
  • A basic understanding of JavaScript and async/await.

Step-by-Step

  1. Launch Puppeteer with the right flags. The most reliable way is to pass Chromium command-line flags via the args option. The key flags are --disable-background-timer-throttling, --disable-backgrounding-occluded-windows, --disable-renderer-backgrounding, and --disable-features=CalculateNativeWinOcclusion. Here's a minimal launch example:
    const browser = await puppeteer.launch({
      headless: false, // or true if you don't need a visible window
      args: [
        '--disable-background-timer-throttling',
        '--disable-backgrounding-occluded-windows',
        '--disable-renderer-backgrounding',
        '--disable-features=CalculateNativeWinOcclusion'
      ]
    });
  2. Keep the page active by using page.bringToFront() periodically. Even with flags, some throttling can occur if the OS marks the window as occluded. Calling page.bringToFront() every few seconds (e.g., with setInterval) forces the browser to treat the tab as active. Example:
    const keepAlive = setInterval(async () => {
      await page.bringToFront();
    }, 5000);
    // Don't forget to clearInterval when done.
  3. Use page.evaluate to override throttling inside the page. Some throttling is applied by the renderer itself. You can disable it by overriding document.hidden and document.visibilityState to simulate an always-visible page. Add this script after page load:
    await page.evaluateOnNewDocument(() => {
      Object.defineProperty(document, 'hidden', { value: false, configurable: true });
      Object.defineProperty(document, 'visibilityState', { value: 'visible', configurable: true });
    });
  4. Set a long timeout for actions that might be delayed. Even with the above, some operations (like waiting for network requests) might take longer. Increase Puppeteer's default timeout to avoid premature failures:
    page.setDefaultTimeout(60000); // 60 seconds
  5. Consider using headless: true if you don't need a visible window. Headless mode is less prone to throttling because there's no window to minimize. If your task doesn't require seeing the browser, run it headless—it's more stable and uses fewer resources. Just remove headless: false or set it to true.
  6. Use page.setRequestInterception to keep the network alive. If your script relies on long-polling or WebSockets, you can periodically send a ping to prevent the connection from being closed. This is more advanced, but you can intercept requests and add a keep-alive header or emulate activity.

Common Mistakes

  • Only using --disable-background-timer-throttling. That flag alone doesn't cover all throttling mechanisms. You need the combination of all four flags listed above, especially --disable-backgrounding-occluded-windows and --disable-renderer-backgrounding.
  • Forgetting to call page.bringToFront() when the window is minimized. The OS may still mark the window as occluded even with flags, so the periodic call is essential.
  • Using headless: false when you don't need it. If you're minimizing the window, you're likely not interacting with it visually. Headless mode avoids the problem entirely and is more efficient.
  • Not clearing the interval for bringToFront(). This can cause memory leaks or keep the process alive after your script ends.

Tips

  • Test your script in both minimized and maximized states. Sometimes the issue only appears after a few minutes of minimization, so run a long test.
  • Use page.waitForSelector with a visible timeout instead of arbitrary sleeps. This is more reliable and works well with the flags.
  • If you're on Windows, also add --disable-features=CalculateNativeWinOcclusion. This flag specifically addresses Windows 10/11 occlusion detection that can throttle background tabs.
  • Monitor the browser's process CPU usage. If it drops to near zero while minimized, throttling is still happening—adjust your flags or use headless.

Troubleshooting

  • Script still pauses after a few minutes. Check if the OS itself is suspending the process (e.g., Windows power settings). Disable sleep mode or run the script in a VM that isn't minimized. Also try adding --disable-background-networking to the args.
  • Page becomes unresponsive (white screen). This usually means the renderer crashed due to memory pressure. Reduce the number of tabs or increase the browser's memory limit with --js-flags="--max-old-space-size=4096".
  • Network requests stall. Some sites use service workers that get throttled. Try disabling service workers with --disable-service-worker or use page.setBypassCSP(true) if you control the site.
  • Errors like "Protocol error: Connection closed" appear. This indicates the browser process died. Check the logs for OOM (out-of-memory) errors and increase system memory or reduce the number of pages.

Frequently Asked Questions

Does headless mode also get throttled?

No, headless mode doesn't have a window, so there's no minimization or occlusion. It's the most reliable way to run Puppeteer in the background.

Can I use these flags with Puppeteer's executablePath?

Yes, the args array works regardless of which Chrome/Chromium executable you use.

Will these flags affect the performance of my script?

They can slightly increase CPU usage because the browser stays active, but for most scripts the difference is negligible.

Is there a way to keep the tab active without periodic bringToFront()?

You can try setting --disable-renderer-backgrounding alone, but in practice the periodic call is the most reliable method.

What if I'm using Puppeteer in a Docker container?

In a container, there's no GUI, so you should use headless mode. The flags are still useful to prevent throttling inside the container's virtual display.

Can I minimize the window manually after launching with these flags?

Yes, but you must keep calling bringToFront(). If you manually minimize, the OS may still throttle despite the flags.