Keep Puppeteer Running When Chrome Is Minimized
In this article
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
- Launch Puppeteer with the right flags. The most reliable way is to pass Chromium command-line flags via the
argsoption. 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' ] }); - Keep the page active by using
page.bringToFront()periodically. Even with flags, some throttling can occur if the OS marks the window as occluded. Callingpage.bringToFront()every few seconds (e.g., withsetInterval) 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. - Use
page.evaluateto override throttling inside the page. Some throttling is applied by the renderer itself. You can disable it by overridingdocument.hiddenanddocument.visibilityStateto 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 }); }); - 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 - Consider using
headless: trueif 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 removeheadless: falseor set it totrue. - Use
page.setRequestInterceptionto 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-windowsand--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: falsewhen 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.waitForSelectorwith 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-networkingto 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-workeror usepage.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.