Skip to content

Chrome DevTools for coding agents

perf(memory): release old navigation request in NetworkCollector (#1200)

Flo1e6456ce222a
diff --git a/src/PageCollector.ts b/src/PageCollector.ts
index eeb29a20..8a419fc6 100644
--- a/src/PageCollector.ts
+++ b/src/PageCollector.ts
@@ -62,7 +62,7 @@ export class PageCollector<T> {
collector: (item: T) => void,
) => ListenerMap<PageEvents>;
#listeners = new WeakMap<Page, ListenerMap>();
- #maxNavigationSaved = 3;
+ protected maxNavigationSaved = 3;
/**
* This maps a Page to a list of navigations with a sub-list
@@ -159,7 +159,7 @@ export class PageCollector<T> {
}
// Add the latest navigation first
navigations.unshift([]);
- navigations.splice(this.#maxNavigationSaved);
+ navigations.splice(this.maxNavigationSaved);
}
protected cleanupPageDestroyed(page: Page) {
@@ -183,7 +183,7 @@ export class PageCollector<T> {
}
const data: T[] = [];
- for (let index = this.#maxNavigationSaved; index >= 0; index--) {
+ for (let index = this.maxNavigationSaved; index >= 0; index--) {
if (navigations[index]) {
data.push(...navigations[index]);
}
@@ -409,5 +409,6 @@ export class NetworkCollector extends PageCollector<HTTPRequest> {
} else {
navigations.unshift([]);
}
+ navigations.splice(this.maxNavigationSaved);
}
}
diff --git a/tests/PageCollector.test.ts b/tests/PageCollector.test.ts
index 6a8155fc..6814002a 100644
--- a/tests/PageCollector.test.ts
+++ b/tests/PageCollector.test.ts
@@ -284,6 +284,29 @@ describe('NetworkCollector', () => {
page.emit('request', request);
assert.equal(collector.getData(page, true).length, 3);
});
+
+ it('should not grow beyond maxNavigationSaved', async () => {
+ const browser = getMockBrowser();
+ const page = (await browser.pages())[0];
+ const mainFrame = page.mainFrame();
+ const collector = new NetworkCollector(browser);
+ await collector.init([page]);
+
+ // Simulate 5 navigations (maxNavigationSaved is 3)
+ for (let i = 0; i < 5; i++) {
+ const req = getMockRequest({
+ url: `http://example.com/nav${i}`,
+ navigationRequest: true,
+ frame: mainFrame,
+ });
+ page.emit('request', req);
+ page.emit('framenavigated', mainFrame);
+ }
+
+ // We expect 3 arrays in navigations (current + 2 saved)
+ // Each navigation has 1 request, so total should be 3
+ assert.equal(collector.getData(page, true).length, 3);
+ });
});
describe('ConsoleCollector', () => {