Skip to content

fix: distinguish same-document navigations from page loads - #3410

Open
thomasbachem wants to merge 2 commits into
ChromeDevTools:mainfrom
thomasbachem:fix/same-document-navigations
Open

thomasbachem wants to merge 2 commits into
ChromeDevTools:mainfrom
thomasbachem:fix/same-document-navigations

Conversation

@thomasbachem

@thomasbachem thomasbachem commented Nov 4, 2026 •

Copy link
Copy Markdown
Contributor

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_text and evaluate_script. The wording is of course up to you. structuredContent carries the flag as sameDocument next to navigatedToUrl, false for a page load.

Comparing the main frame's loader ID would be shorter, but Puppeteer only updates it on the init lifecycle 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 structuredContent for both kinds of navigation, plus a history navigation back to another document. sameDocument is now also asserted on the reload test from #3395 and the two pushState tests, which I renamed to match. All of them fail without the change, and npm run test passes locally (macOS, Node 26, Chrome 157).

@mangeshraut712 mangeshraut712 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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) {

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#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.

marco-prontera pushed a commit to marco-prontera/chrome-devtools-mcp that referenced this pull request Nov 11, 2026
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.
@thomasbachem

Copy link
Copy Markdown
Contributor Author

Rebased onto main now that #3395 is in.

Comment thread src/McpResponse.ts
);
structuredContent.navigatedToUrl =
this.#attachedWaitForResult.navigatedToUrl;
structuredContent.navigatedToUrl = navigatedToUrl;

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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 Lightning00Blade left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please see the comment.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Actions report same-document (SPA) navigations like page loads, but not reloads

3 participants