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.
This commit is contained in:
Chirag Aggarwal committed 2026-07-27 13:24:03 +05:30
1 parent 4919ebcc38
commit caed7aad24
2 files changed
+54

No files matched your search

+23
View File
@@ -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();
+31
View File
@@ -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"}')))