fix(extension): resolve waitForLoadState("networkidle") on an already…

Merged
SE
4d ago
+493-25

thanks @tomtommyyuan for the contribution here!

why

Fixes #3370.

waitForLoadState("networkidle") times out on an already-idle page, even right after goto(..., { waitUntil: "networkidle" }). It waits for Chrome's networkIdle event, which has already fired for that document.

what changed

Use the same network-idle check as goto: wait for load, then for 500 ms with no in-flight requests for the current main document. waitForLoadState("networkidle") now runs goto's LifecycleWatcher on the current document. If a navigation or reload replaces the document, the wait restarts for the new one within the same timeout.

This also fixes two cases that would otherwise keep the wait pending:

  • Worker script requests have no loader ID on the page session, and their completion events arrive on the worker target. When the document's loader ID is known, the filter now requires an exact match.
  • Requests from a document that navigated away can remain in NetworkManager without a completion event. They are now removed when the frame navigates. SessionNetworkCapture also captures Page.frameNavigated for this.

Network-idle timeouts use TimeoutError, so callers such as waitForURL can still identify them by error.name. waitForMainLoadState is unchanged to avoid overlapping with #3177.

Includes @RaphaelFakhri's #3073 as a separate, unchanged commit. It makes the idle check count requests already in flight, including fetches started by a load listener. That fix needs the Worker filter change above to avoid introducing a timeout in goto. The last commit folds its changeset into this PR's.

One behavior change to be aware of: goto now counts requests that started before load. A buffering <video> or <audio> with preload="auto" or autoplay can therefore cause a timeout where main resolved about 500 ms after load.

test plan

New local Chrome integration tests cover an already-idle page, a fetch started by a load listener, a Worker in both goto and waitForLoadState, navigation with a request in flight, and a goto or reload that replaces the document during the wait. The network-manager unit tests include #3073's cases and a new case for clearing requests after navigation.

Passed locally: pnpm check, the extension, sdk-ts and scripts unit tests, and the full local integration suite.


Summary by cubic

Fixes waitForLoadState("networkidle") timing out on an already-idle page (#3370). The wait now uses goto's network-idle check on the current document: wait for load, then 500 ms with no in-flight requests. If a goto or reload replaces the document mid-wait, the wait restarts for the new one within the same timeout.

Two cases that kept the wait pending are also fixed: Web Worker script requests now require an exact loader-ID match, and requests from documents that navigated away are removed when the frame navigates. networkidle and load-stage timeouts now throw TimeoutError, so callers can still identify them by error.name.

Behavior change: goto now counts requests that started before load. A buffering <video> or <audio> with preload="auto" or autoplay can cause a networkidle timeout where main resolved about 500 ms after load. When the current document's loader ID is unknown, the wait only counts requests that start after it begins.