Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 17 additions & 3 deletions browse/src/browser-manager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1016,14 +1016,28 @@ export class BrowserManager {
}
});

// Capture response sizes via response finished
// Capture response sizes via response finished.
// Prefer Content-Length header over res.body() — body() buffers the entire
// response into memory, which is O(response_size) per request and blocks
// the event loop on large payloads (images, JS bundles, API responses).
// Content-Length is available immediately from headers with zero I/O cost.
// Fall back to body() only when Content-Length is absent (chunked transfer).
page.on('requestfinished', async (req) => {
try {
const res = await req.response();
if (res) {
const url = req.url();
const body = await res.body().catch(() => null);
const size = body ? body.length : 0;
let size = 0;

const contentLength = res.headers()['content-length'];
if (contentLength) {
size = parseInt(contentLength, 10) || 0;
} else {
// Chunked or unknown — body() is the only option
const body = await res.body().catch(() => null);
size = body ? body.length : 0;
}

for (let i = networkBuffer.length - 1; i >= 0; i--) {
const entry = networkBuffer.get(i);
if (entry && entry.url === url && !entry.size) {
Expand Down