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.
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 callsubmit) usinggit statusor 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 inspect the repository structure, package scripts, Link shim, and any bundled Next.js reference implementation. This will establish the relevant API contract and the project’s testing conventions before making changes.
- Assistant
The source already contains an
onNavigateimplementation and tests, suggesting the repository may include an incomplete prior attempt or behavior that differs from the current Next.js contract. I’ll inspect the exact Link types/click path, the focused tests, git status, and both Pages/App Router reference implementations to identify the semantic gap—especially the issue’s mention of callback return values versus Next.js’s documentedpreventDefault()API. - Assistant
Next.js 16.2.6 confirms that
onNavigatereceives only{ preventDefault() }; return values are ignored. Navigation is canceled only when that method is called. It fires after the normalonClick, only for unmodified, non-download, local client-side navigations, and before router dispatch. I’ll now inspect vinext’s entire click handler and existing assertions without truncation, then run the focused unit suites to see whether the current implementation already passes visible tests and expose likely hidden failures. - Assistant
The handler broadly follows Next.js, but two discrepancies stand out: it adds nonstandard
url/defaultPreventedfields, and—more importantly—it wraps both event creation and the user callback in a blanketcatch. That silently swallows callback exceptions and then continues navigation, unlike Next.js. The visible tests also mostly test hand-built event objects rather than the actual click path. Before editing, I’ll inspect repository history/blame and the upstream eight-case test fixture to determine all expected scenarios, then run only the focused Link navigation file with a longer timeout. - Assistant
The implementation predates this issue, so the reported failures indicate the existing code is not faithfully reachable in at least one runtime path rather than simply absent. I’ll inspect the rendered anchor and module alias/build setup, and use the installed local test binaries directly (avoiding Corepack’s interactive download prompt). I’ll also fetch the exact upstream test file over the network if available, because its eight scenarios are the strongest acceptance specification.
- Assistant
The exact upstream suite shows seven cases per router. Vinext already handles six behaviors in principle, but fails Next.js’s external
replacebehavior because it returns without callinglocation.replace(). The core callback path is present, so to deliver a meaningful, contract-accurate fix I’ll:- Make the public event shape match Next.js (
preventDefaultonly). - Remove the catch that swallows user callback errors.
- Keep cancellation based on an internal flag, as upstream does.
- Implement external
replacewithwindow.location.replace. - Exercise actual click handlers through a temporary standalone Vitest file (then delete it), since modifying tests is forbidden.
First I’ll inspect the local
vpinvocation and TypeScript/build scripts, plus upstream fixtures to ensure state timing isn’t exposing another issue.
- Make the public event shape match Next.js (
- Assistant
The Next public declaration confirms the callback event must contain only
preventDefault(). I’ll now update the source handler to mirror upstream exactly: external links remain native unlessreplaceis set, user callback exceptions propagate, and cancellation uses the closure flag. This also avoids URL parsing entirely for the callback, which is important because Next’s event does not expose a URL and valid internal navigation should never skip the callback due to unrelated parsing. - Assistant
Python is unavailable in this environment, so no file was changed. I’ll apply the same targeted replacements using the repository’s provided
replaceutility. - Assistant
The type edits applied, while the handler replacements did not because the formatter had wrapped the relevant calls differently than the captured text. I’ll inspect those exact ranges and replace them using line-based edits.
- Assistant
I’ll replace lines 558–562 and 575–598 directly, preserving surrounding URL resolution needed by Pages Router and fallback navigation.
- Assistant
The line edit is functionally correct but left one obsolete comment and lacks a visual separator before the router branch; I’ll clean those up. Then I’ll run the focused unit test using
vpdirectly (which does not invoke Corepack), followed by formatting/type checks. The existing focused suite does execute the rendered anchor for native click exclusions and provides regression coverage around this handler. - Assistant
The test correctly caught a malformed brace introduced by my second line-range edit: the range shifted and duplicated the callback’s closing brace while deleting the first router comment line. I’ll replace the whole affected block atomically, then rerun the focused suite.
- Assistant
The focused Link suite now passes all 25 tests. To verify the actual positive callback and cancellation paths without adding tests, I’ll use the existing Playwright cases already present in the repository: the Pages Router advanced spec and App Router navigation spec both click Links whose
onNavigaterecords the resolved navigation. I’ll list projects/spec setup first, then run those tests by title if feasible. I’ll also run formatter/type/lint checks and build the package so generated declarations reflect the corrected event contract. - Assistant
Build, format, lint, and type checks pass. However, the repository’s existing E2E fixtures currently read
event.url, a vinext extension not present in Next.js 16.2.6. Removing it would regress those existing tests even though it improves strict upstream parity. The issue asks to implement the callback rather than remove supported fields, so I’ll preserve the extension (urlanddefaultPrevented) while retaining the substantive fixes: callback invocation/cancellation without swallowing errors and externalreplace. I’ll restore those type/event fields and then run the two existing E2E callback tests, which directly verify invocation in both routers. - Assistant
The restoration edit again targeted stale line numbers after the type block shifted the handler, dropping the opening
ifandreturn. I’ll correct the entire callback block atomically using its current range and immediately validate syntax before any E2E run. - Assistant
Syntax/type/lint checks and all 25 focused unit tests pass after preserving vinext’s URL extension. I’ll now run the two existing browser tests by title. They directly click the component and assert that
onNavigatefired before successful SPA navigation in Pages and App Router modes. - Assistant
The browser specs reached test execution but could not launch because the Playwright Chromium binary is absent; this is an environment prerequisite failure, not an application assertion failure. The build also exposed a pre-existing dependency-scan warning unrelated to this change, but completed successfully.
I’ll now run the broader Link and shim unit suites (including existing event-shape checks), inspect the final diff for scope and correctness, and verify no generated/test artifacts are tracked. I’ll also ensure the external
replacebranch exactly matches Next.js and that callback return values remain intentionally ignored in favor ofpreventDefault().
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.