Crawlee—A web scraping and browser automation library for Node.js to build reliable crawlers. In JavaScript and TypeScript. Extract data for AI, LLMs, RAG, or GPTs. Download HTML, PDF, JPG, PNG, and other files from websites. Works with Puppeteer, Playwright, Cheerio, JSDOM, and raw HTTP. Both headful and headless mode. With proxy rotation.
TypeScript apifyautomationcrawlercrawlingheadlessheadless-chromejavascriptnodejsnpmplaywrightpuppeteerscraperscrapingtypescriptweb-crawlerweb-crawlingweb-scraping
Add max frequency of screenshots to help with performance
Ondra Urband371f0726c28
diff --git a/src/live_view/live_view_server.js b/src/live_view/live_view_server.jsindex febaf478b..c0b60243e 100644--- a/src/live_view/live_view_server.js+++ b/src/live_view/live_view_server.js@@ -10,11 +10,6 @@ import { promisifyServerListen } from 'apify-shared/utilities';import { ENV_VARS, LOCAL_ENV_VARS } from 'apify-shared/consts';import { addTimeoutToPromise } from '../utils';-const DEFAULT_SCREENSHOT_DIRECTORY_PATH = path.resolve('live_view');-const DEFAULT_MAX_SCREENSHOT_FILES = 10;-const DEFAULT_SNAPSHOT_TIMEOUT_SECS = 3;--const writeFile = promisify(fs.writeFile);const unlink = promisify(fs.unlink);const ensureDir = promisify(fs.ensureDir);@@ -61,24 +56,32 @@ const ensureDir = promisify(fs.ensureDir);* @param {number} [options.maxScreenshotFiles=10]* Limits the number of screenshots stored* by the server. This is to prevent using up too much disk space.- * @param {number} [options.snapshotTimeoutSecs=1]+ * @param {number} [options.snapshotTimeoutSecs=3]* If a snapshot is not made within the timeout,* its creation will be aborted. This is to prevent* pages from being hung up by a stalled screenshot.+ * @param {number} [options.maxSnapshotFrequencySecs=2]+ * Use this parameter to further decrease the resource consumption+ * of `LiveViewServer` by limiting the frequency at which it'll+ * serve snapshots.*/class LiveViewServer {constructor(options = {}) {const {- screenshotDirectoryPath = DEFAULT_SCREENSHOT_DIRECTORY_PATH,- maxScreenshotFiles = DEFAULT_MAX_SCREENSHOT_FILES,- snapshotTimeoutSecs = DEFAULT_SNAPSHOT_TIMEOUT_SECS,+ screenshotDirectoryPath = path.resolve('live_view'),+ maxScreenshotFiles = 10,+ snapshotTimeoutSecs = 3,+ maxSnapshotFrequencySecs = 1,} = options;checkParamOrThrow(screenshotDirectoryPath, 'options.screenshotDirectoryPath', 'String');checkParamOrThrow(maxScreenshotFiles, 'options.maxScreenshotFiles', 'Number');+ checkParamOrThrow(maxScreenshotFiles, 'options.snapshotTimeoutSecs', 'Number');+ checkParamOrThrow(maxScreenshotFiles, 'options.maxSnapshotFrequencySecs', 'Number');this.screenshotDirectoryPath = screenshotDirectoryPath;this.maxScreenshotFiles = maxScreenshotFiles;this.snapshotTimeoutMillis = snapshotTimeoutSecs * 1000;+ this.maxSnapshotFrequencyMillis = maxSnapshotFrequencySecs * 1000;// Snapshot datathis.lastSnapshot = null;@@ -134,10 +137,15 @@ class LiveViewServer {* @return {Promise}*/async serve(page) {+ if (!this.hasClients()) return;// Only serve one snapshot at a time because Puppeteer// can't make screenshots in parallel.if (this.servingSnapshot) return;- if (!this.hasClients()) return;++ if (this.lastSnapshot) {+ const lastSnapshotAgeMillis = Date.now() - this.lastSnapshot.createdAt;+ if (lastSnapshotAgeMillis < this.maxSnapshotFrequencyMillis) return;+ }try {this.servingSnapshot = true;diff --git a/test/live_view/live_view_server.js b/test/live_view/live_view_server.jsindex 472270f36..58016d928 100644--- a/test/live_view/live_view_server.js+++ b/test/live_view/live_view_server.js@@ -40,6 +40,7 @@ describe('LiveViewServer', () => {lvs = new LiveViewServer({screenshotDirectoryPath: LOCAL_STORAGE_SUBDIR,maxScreenshotFiles: 2,+ maxSnapshotFrequencySecs: 0,});});