Repository navigation
fix: distinguish same-document navigations from page loads - #3410
thomasbachem wants to merge 2 commits into
Conversation
78fab5c to
2b4a86e
Compare
mangeshraut712
left a comment
There was a problem hiding this comment.
Reporting a same-URL reload and tagging History API navigations as sameDocument is useful, and the new helper tests cover the main cases. The sameDocument flag is inferred from the absence of Page.frameNavigated, and the main-frame check does not match the one already used in this file.
| const frameNavigatedHandler = ( | ||
| event: Protocol.Page.FrameNavigatedEvent, | ||
| ): void => { | ||
| if (!event.frame.parentId) { |
There was a problem hiding this comment.
#loadedNewDocument is set when !event.frame.parentId. The frameStartedNavigating listener a few lines below compares event.frameId to this.#page.mainFrame()._id, which is the more reliable main-frame test (a child frame with a missing parentId, or a prerender/OOPIF edge, would otherwise flip this flag). I'd use the same id comparison here.
sameDocument is then just !this.#loadedNewDocument whenever the URL changed. There is no positive listener for Page.navigatedWithinDocument. If frameNavigated is missed (Page domain not yet delivering the event, or a navigation kind this handler does not see), a full document load whose URL changed would be labeled sameDocument: true. Listening for navigatedWithinDocument and only setting sameDocument: true when that event fired would make the flag a positive signal instead of a fallback.
Part of ChromeDevTools#3394 `WaitForHelper` decides whether an action navigated by comparing the page URL before and after it. So a load that ends on the same URL, like a reload or a form POST that redirects back, isn't reported at all. This now also tracks whether the main frame loaded a new document during the action: CDP reports each one with `Page.frameNavigated`, while same-document navigations come as `Page.navigatedWithinDocument`. A new document then gets the existing `Page navigated to <url>.` line and `structuredContent.navigatedToUrl` even when its URL didn't change, in every tool that reports navigations: `click`, `click_at`, `drag`, `fill`, `fill_form`, `hover`, `press_key`, `type_text` and `evaluate_script`. I checked the tracking against whether `performance.timeOrigin` changed in headless Chrome 156, and it matched in all 15 cases I tried, among them a form POST redirecting back to the same URL, a cross-origin navigation, one committing after the 100 ms window and back/forward cache restores. The new test covers a reload that keeps the URL and fails on `main`. Another one, a `replaceState` that keeps the URL, passes on `main` too but fails if same-document navigations count as new documents. `npm run test` passes locally (macOS, Node 26, Chrome 156), and so does the suite after `npm run bundle`. `PageCollector` still pairs `FrameNavigatedWithinDocument` with `framenavigated` in two places, so I'm happy to move both, and the tracking here, onto the events from puppeteer/puppeteer#15759 once they land. How same-document navigations are reported moved to ChromeDevTools#3410, which builds on this. ChromeDevTools#3226 also touches `WaitForHelper.ts`, so I can rebase on it if it lands first.
|
Rebased onto main now that #3395 is in. |
2b4a86e to
54cbca3
Compare
| ); | ||
| structuredContent.navigatedToUrl = | ||
| this.#attachedWaitForResult.navigatedToUrl; | ||
| structuredContent.navigatedToUrl = navigatedToUrl; |
There was a problem hiding this comment.
We need to also record this for the structuredContent. When the structuredContent is use there is no way to distinguish if it's same doc navigation.
There was a problem hiding this comment.
Added sameDocument to structuredContent next to navigatedToUrl, false for a page load. The workflows here are still waiting for approval, so CI hasn't run yet.
Lightning00Blade
left a comment
There was a problem hiding this comment.
Please see the comment.
Fixes #3394
Same-document navigations are still reported as they have been since #1853, but now read
Page navigated to <url> within the same document., told apart with the document tracking from #3395. That changes the line in every tool that reports navigations:click,click_at,drag,fill,fill_form,hover,press_key,type_textandevaluate_script. The wording is of course up to you.structuredContentcarries the flag assameDocumentnext tonavigatedToUrl,falsefor a page load.Comparing the main frame's loader ID would be shorter, but Puppeteer only updates it on the
initlifecycle event, so a back/forward cache restore keeps the old one and would read as same-document.The new tests cover the response line and
structuredContentfor both kinds of navigation, plus a history navigation back to another document.sameDocumentis now also asserted on the reload test from #3395 and the twopushStatetests, which I renamed to match. All of them fail without the change, andnpm run testpasses locally (macOS, Node 26, Chrome 157).