Merge pull request #13014 from appwrite/fix/screenshot-capture-scale

fix(screenshots): capture site previews above viewport resolution
This commit is contained in:
Chirag Aggarwal authored and GitHub committed 2026-07-27 15:27:28 +05:30
commit 83bb063ca4
4 files changed
+48

No files matched your search

+16
View File
@@ -9,6 +9,17 @@ 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;
private Factory $factory;
/**
@@ -33,6 +44,11 @@ class Client
'theme' => $theme,
'headers' => $headers,
'sleep' => $sleep,
'viewport' => [
'width' => self::VIEWPORT_WIDTH,
'height' => self::VIEWPORT_HEIGHT,
],
'deviceScaleFactor' => self::SCALE,
]));
$status = $response->getStatusCode();
Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 25 KiB

Binary file not shown.

Before

Width:  |  Height:  |  Size: 14 KiB

After

Width:  |  Height:  |  Size: 25 KiB

+32
View File
@@ -58,9 +58,41 @@ final class ClientTest extends TestCase
'theme' => 'dark',
'headers' => ['x-appwrite-hostname' => 'example.com'],
'sleep' => 500,
'viewport' => ['width' => 1280, 'height' => 720],
'deviceScaleFactor' => 1.5,
], \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 testCreateDoesNotBlockOnSubresources(): 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 navigates with `domcontentloaded` unless told
// otherwise. Waiting for `load` risks exhausting the request timeout on
// sites with slow or hanging subresources, which yields no screenshot at
// all rather than an early one.
$this->assertArrayNotHasKey('waitUntil', \json_decode((string)$this->request->getBody(), true));
}
public function testCreateError(): void
{
$response = (new Response(400, body: new Stream('{"error":"Timeout exceeded"}')))