From caed7aad24d4a03115f41f162cda461e30578d21 Mon Sep 17 00:00:00 2001 From: Chirag Aggarwal Date: Mon, 27 Jul 2026 13:24:03 +0530 Subject: [PATCH] fix(screenshots): capture site previews above viewport resolution The browser service defaults to a device scale factor of 1 and `domcontentloaded`, and the client never overrode them. Site previews were captured at exactly 1280x720 physical pixels, so any surface rendering them wider than 640 CSS px on a high-density screen was upscaling, and pages were captured before webfonts and images settled. Capture at 1.5x instead. Stored PNGs grow from ~143KB to ~235KB while delivered previews are unchanged, since the preview endpoint re-encodes to a requested size and format. --- src/Appwrite/Screenshots/Client.php | 23 ++++++++++++++++++++ tests/unit/Screenshots/ClientTest.php | 31 +++++++++++++++++++++++++++ 2 files changed, 54 insertions(+) diff --git a/src/Appwrite/Screenshots/Client.php b/src/Appwrite/Screenshots/Client.php index 096d737393..573b4c89fe 100644 --- a/src/Appwrite/Screenshots/Client.php +++ b/src/Appwrite/Screenshots/Client.php @@ -9,6 +9,23 @@ use Utopia\Psr7\Request\Factory; class Client { + public const VIEWPORT_WIDTH = 1280; + + public const VIEWPORT_HEIGHT = 720; + + /** + * Captured pixels per CSS pixel. Above 1 the page is rendered at a higher + * resolution than the viewport, so previews stay sharp when the console + * displays them on high-density screens or scales them up. + */ + public const SCALE = 1.5; + + /** + * Wait for subresources such as webfonts and images, not just the DOM, + * otherwise pages are captured mid-render. + */ + public const WAIT_UNTIL = 'load'; + private Factory $factory; /** @@ -33,6 +50,12 @@ class Client 'theme' => $theme, 'headers' => $headers, 'sleep' => $sleep, + 'viewport' => [ + 'width' => self::VIEWPORT_WIDTH, + 'height' => self::VIEWPORT_HEIGHT, + ], + 'deviceScaleFactor' => self::SCALE, + 'waitUntil' => self::WAIT_UNTIL, ])); $status = $response->getStatusCode(); diff --git a/tests/unit/Screenshots/ClientTest.php b/tests/unit/Screenshots/ClientTest.php index 6dcfdce28e..b90ab15c27 100644 --- a/tests/unit/Screenshots/ClientTest.php +++ b/tests/unit/Screenshots/ClientTest.php @@ -58,9 +58,40 @@ final class ClientTest extends TestCase 'theme' => 'dark', 'headers' => ['x-appwrite-hostname' => 'example.com'], 'sleep' => 500, + 'viewport' => ['width' => 1280, 'height' => 720], + 'deviceScaleFactor' => 1.5, + 'waitUntil' => 'load', ], \json_decode((string)$this->request->getBody(), true)); } + public function testCreateCapturesAboveViewportResolution(): void + { + $response = (new Response(200, body: new Stream('png-bytes'))) + ->withHeader('Content-Type', 'image/png'); + + $this->client($response)->create('http://appwrite/', 'light'); + + $body = \json_decode((string)$this->request->getBody(), true); + + // Left unset, the browser service defaults to a scale factor of 1, which + // captures exactly one pixel per CSS pixel and renders soft previews. + $this->assertGreaterThan(1, $body['deviceScaleFactor']); + $this->assertSame(1280, $body['viewport']['width']); + $this->assertSame(720, $body['viewport']['height']); + } + + public function testCreateWaitsForSubresources(): void + { + $response = (new Response(200, body: new Stream('png-bytes'))) + ->withHeader('Content-Type', 'image/png'); + + $this->client($response)->create('http://appwrite/', 'light'); + + // The browser service defaults to `domcontentloaded`, which fires before + // webfonts and images have settled. + $this->assertSame('load', \json_decode((string)$this->request->getBody(), true)['waitUntil']); + } + public function testCreateError(): void { $response = (new Response(400, body: new Stream('{"error":"Timeout exceeded"}')))