Skip to content

getAISnapshot hangs on pages with heavy JavaScript #43

@parkerhancock

Description

@parkerhancock

Problem

getAISnapshot() can hang indefinitely on pages with heavy JavaScript execution or anti-bot measures. There's no timeout mechanism, so scripts calling this function may never complete.

Reproduction

const client = await connect();
const page = await client.page("test");
await page.goto("https://some-heavy-js-site.com");

// This may hang forever
const snapshot = await getAISnapshot(page);

Proposed Solution

Add timeout handling with a fast health check:

  1. Health check first: Run a quick 3-second page.evaluate(() => true) to detect unresponsive pages early
  2. Configurable timeout: Add optional timeout parameter (default 10s)
  3. Clear error messages: Explain why the page might be unresponsive
async function getAISnapshot(page: Page, timeout = 10000): Promise<string> {
  // Quick health check
  const healthCheck = page.evaluate(() => true);
  const healthy = await Promise.race([
    healthCheck.then(() => true),
    new Promise(r => setTimeout(() => r(false), 3000))
  ]);
  
  if (!healthy) {
    throw new Error('Page unresponsive - may have anti-bot measures');
  }
  
  // Actual snapshot with timeout
  return Promise.race([
    actualSnapshotLogic(page),
    new Promise((_, reject) => 
      setTimeout(() => reject(new Error(`Timeout after ${timeout}ms`)), timeout)
    )
  ]);
}

Working Implementation

I have this fix in my fork:

Happy to submit a PR if there's interest.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions