ISSUE DESCRIPTION <Link onNavigate> prop not implemented This issue was created by an…
- User
ISSUE DESCRIPTION
<Link onNavigate>prop not implementedThis issue was created by an agent analysing CI failures from the Next.js Deploy Suite (vinext
mainvs Next.jsv16.2.6, 2026-05-20).Problem
The
onNavigatecallback prop on<Link>(added in Next.js 15) does not fire. Tests that rely ononNavigateto detect navigations, intercept them, or track click state all fail.Specific failures include:
onNavigatecallback never fires on click- Click state tracking broken (link thinks it was never clicked)
- History state replacement via
onNavigatereturn value not working
Estimated Impact
~8 test failures.
Affected Test Suites
test/e2e/link-on-navigate-prop/index.test.ts(8 failures)
Recommendation
-
Reproduce first in vinext's own test suite. Add a test with
<Link href="/page" onNavigate={(e) => ...}>and verify the callback fires on click. Confirm it does not. -
Study the Next.js implementation. Search
.nextjs-ref/packages/next/src/client/link.tsxforonNavigateto understand the API contract: when the callback fires, what arguments it receives, and how the return value affects navigation (e.g., preventing default, replacing history state). -
Implement
onNavigatein thenext/linkshim. The Link component needs to callonNavigatebefore performing the navigation and respect the return value.
Interface
No new interfaces are introduced.
ADDITIONAL ADVICE
Since you are given a git repository, you can use git commands to simplify your work. For example, if you made a mistake and want to revert your changes, you can use
git checkout <file>to restore the file to its original state. You can also reset all changes in the repository usinggit reset --hardcommand. Additionally, you can usegit stashandgit stash popto temporarily save your changes and restore them later. Finally, you can see the changes that you've made (which will be submitted when you call ) using or commands. However you don't need to use or before submitting your changes. In fact, only submits currently unstaged changes because it uses to get the changes that need to be submitted. - Assistant
I’ll first locate the Link shim, its tests, and the vendored Next.js reference implementation. Then I’ll reproduce the missing callback with the project’s existing test setup before making the smallest contract-compatible change.
- Assistant
The source already contains an
onNavigateblock, so this may be an incomplete or semantically incorrect implementation rather than a total omission. I’ll inspect git state, the full click path and types, existing unit tests, and Next 16.2.6’s exact implementation to identify the mismatch. - Assistant
Next’s public contract is clear: the callback receives only
preventDefault(), its return value is ignored, and calling that method cancels router navigation. It runs only for an eligible local client-side navigation, after the click event itself has been prevented. I’ll now inspect vinext’s eligibility checks and tests specifically around clicks to see whether callback timing, URL shape, or cancellation differs. - Assistant
The fixture already has a relative-URL
onNavigatescenario, suggesting recent baseline work may cover only one variant. I’ll enumerate all relevant test cases and run the focused Link unit/integration tests to establish the current behavior. I’ll also inspect git history for the existing block, since that can reveal whether it is a partial fix that omitted the deploy-suite scenarios. - Assistant
The focused command hit a Corepack prompt, so tests did not run; I’ll use the repository’s installed
vpbinary instead. More importantly, the current implementation predates this issue and exposes non-Next fields (url,defaultPrevented) while swallowing every exception thrown by the user callback. That exception swallowing is a concrete contract bug, but it does not explain “never fires.” I’ll inspect the eight upstream test scenarios (via the installed/reference source or remote repository if available) and the component render path, especially legacy behavior where props can be attached to a child anchor. - Assistant
The upstream suite confirms seven scenarios per router. Vinext’s current code already satisfies callback firing/cancellation in principle, but misses one exact Next behavior covered there: an external link with
replacemust calllocation.replacerather than falling through to native push navigation. I also found the callback wrapped in a broadtry/catch; because URL parsing cannot fail after the helper has produced a browser href, this incorrectly suppresses exceptions from application code. I’ll implement the callback as a small pure helper matching Next’s cancellation semantics, use it from the click handler, and add externalreplacehandling. This also gives a direct temporary reproduction path without modifying repository tests. - Assistant
I’ll preserve vinext’s existing
url/defaultPreventedextensions because its own compatibility fixtures rely on them, while matching Next’s observable behavior: invoke once, cancel only throughpreventDefault, ignore the callback return, and propagate callback exceptions. For externalreplace, I’ll prevent the native click and invokewindow.location.replace, exactly as Next does. - Assistant
Python is unavailable in this environment, so that edit did not apply (the formatter consequently had nothing to change). I’ll apply the same two targeted replacements with the provided repository editing command, then inspect the diff.
- Assistant
The editor requires the target range to be displayed before replacement. I’ll open the relevant section and then replace the exact line ranges directly.
- Assistant
Now that the range is open, I’ll apply the two exact edits and format/check the result.
- Assistant
The diff now cleanly separates external replacement from local navigation and no longer catches user callback errors. I’ll verify the focused unit suite, repository type/lint checks for this package, build output, and the existing pages-router E2E case that executes a real
onNavigatecallback in the browser. - Assistant
Unit tests, static checks, and package build passed. The browser test could not launch because the Playwright Chromium binary is absent; its web-server setup also reported a pre-existing unrelated JSX-in-
.jsfixture parse error. Before deciding whether to install a browser, I’ll inspect the upstream fixture itself. The suite’s “navigated” state may depend on callback cancellation or persistence in a way not obvious from the assertions, which could explain why the existing callback block still produced CI failures. - Assistant
The guessed fixture paths were wrong. I’ll query the GitHub directory listing to retrieve the actual fixture files and verify whether callback state is intentionally preserved with
preventDefault(). - Assistant
I’ll recursively list and fetch the small shared/router fixture files.
- Assistant
Now I’ll fetch the shared component and route wrappers to confirm the full expected interaction lifecycle.
- Assistant
The upstream component confirms the callback’s return value is not used; cancellation is exclusively
e.preventDefault(). It also confirms the history-length failure is the externalreplacecase now handled by the patch. The other event filters (modifier, target, download, external) already precede callback invocation in vinext, matching Next.I’ll run the complete unit project now and inspect the final source-only diff. The browser E2E remains blocked by the missing browser binary and unrelated fixture parse issue, but the existing focused unit coverage and package build are green.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.