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 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 implementing the API contract.
- Assistant
The implementation and focused unit tests are in
packages/vinext/src/shims/link.tsxandtests/link*.test.ts. I’ll inspect those plus the installed Next.jslink.tsxsource/type declarations to determine exact event semantics, especially cancellation and modified-click behavior. - Assistant
The shim already declares
onNavigate, but the declaration says it is ignored and likely never invokes it. I’ll narrow the inspection to the click handler and compare it line-for-line with Next.js’slinkClickedlogic and public event type. - Assistant
This checkout contains a partial implementation that goes beyond Next.js’s public contract (
urlanddefaultPrevented) and wraps the callback in a broadtry/catch. The callback should nevertheless fire for ordinary clicks, so I need to establish whether the failure is in test interaction, async timing, or a compatibility mismatch. I’ll check repository status/history and run the focused Link tests before constructing a direct component reproduction. - Assistant
The focused test command exceeded the environment timeout, but no test result was reached. The history suggests the partial code predates recent Link fixes and has no direct unit coverage. I’ll inspect the commit that introduced it and the upstream deploy-suite fixture to identify the eight expected scenarios, then use the project’s fast anchor-capture harness for reproduction.
- Assistant
The source confirms the callback has existed since initial release, but it was implemented against a non-Next event shape. Existing tests only cover the “must not fire for download” case. I’ll retrieve the exact upstream eight-test suite from the installed package’s source tree or GitHub (network permitting), because the issue’s phrase “history state replacement via
onNavigatereturn value” may refer to application behavior after callingpreventDefault, not an actual callback return contract. - Assistant
The upstream suite confirms the contract:
onClickalways runs first.onNavigateruns only for unmodified, non-download, same-origin client navigations.event.preventDefault()cancels router navigation.- External links stay browser-owned, except
replacemust calllocation.replace. - The callback return value is ignored; cancellation is event-based.
Most of this partial implementation is present, but external
replaceis missing and its event object/callback exception handling diverges from Next.js. I’ll inspect the upstream fixture callback itself and the project’s generated shim declarations, then refactor the decision logic into a testable helper and validate all scenarios without modifying tests. - Assistant
The public declaration already has the correct minimal type, while the runtime’s private type does not. Since the ordinary callback path should pass today, the concrete compatibility defect exposed by this suite is external
replace; additional defects are swallowed callback errors and a nonstandard event shape. I’ll make a minimal, upstream-aligned change directly inhandleClick: handle externalreplace, invoke a{preventDefault}event without URL parsing or exception swallowing, and preserve cancellation. Then I’ll use an untracked temporary Vitest file to exercise all click branches and remove it afterward. - Assistant
Python is unavailable in this environment, so no file was changed. I’ll apply the same targeted replacements with Node.js and immediately inspect the diff for accidental edits.
- Assistant
The diff reveals an important local compatibility extension: vinext’s own E2E tests read
event.urlfor resolved relative links. Removing it would regress supported behavior even though Next.js only typespreventDefault. I’ll preserveurlanddefaultPreventedas runtime extras while fixing the core semantics: construct the URL separately, never swallow callback errors, and use an independent cancellation flag. - Assistant
The local URL extension is now retained. For reproduction/verification, I can use the existing anchor-capture test harness with a temporary copy of the focused test file containing added scenarios, but that is unnecessarily large. Instead I’ll first run the already-ported download and normal navigation tests individually (fast once Vite is warm), then run typecheck/build. After that I’ll create a temporary standalone Vitest test under
/tmponly if needed for callback cancellation and external replacement. - Assistant
Vitest startup exceeds the per-command execution window, so I’ll launch it in the background, capture its exit code/output, and poll without blocking.
- Assistant
The shell suspended the background job due to job control. I’ll restart it detached with
setsid, then poll the result files. - Assistant
The delay is Corepack attempting to download the pinned pnpm version, not the tests. I’ll bypass Corepack and invoke the repository’s installed
vitestbinary directly. - Assistant
This workspace uses Vite Plus’s built-in test runner rather than a standalone Vitest binary. I’ll invoke
node_modules/.bin/vp testdirectly, which avoids Corepack. - Assistant
The existing focused tests pass. I’ll now create a temporary, untracked reproduction test that captures the rendered anchor handler and verifies the two previously uncovered critical paths: (1)
onNavigatefires andpreventDefaultcancels App Router navigation; (2) externalreplacebypassesonNavigateand callslocation.replace. I’ll delete the file immediately after execution. - Assistant
The temporary harness reproduced the symptom, but for the same known reason documented in the repository’s test helper: importing the shim and actual React in parallel races the React mock, so no anchor handler is captured. This is a harness failure, not the implementation. I’ll rerun with the required sequential imports and both scenarios in one test/module lifecycle, then remove the temporary file.
- Assistant
The reduced mock still omitted the project helper’s React
createElementand JSX-dev interception, so it again failed to capture rather than exercising Link. I’ll avoid maintaining a parallel harness: make a temporary copy of the proven existing navigation test, inject anonNavigatespy into its normal-click case, run only that case, and delete the copy. This directly verifies the reported “callback never fires” symptom using the repository’s established machinery. - Assistant
The proven harness now confirms an ordinary internal click invokes
onNavigate; the temporary file was removed. The shell reported nonzero only because an earlier suspended Corepack job remains attached, while the test runner itself passed. Before final validation I’ll inspect the upstream fixture source to ensure its callback doesn’t rely on some subtle behavior, terminate stale processes, and run the package checks directly through Vite Plus. - Assistant
The upstream callback only sets React state or calls
preventDefault, so the implementation path is correct. The compatibility changes now cover every upstream branch, including external replacement. I’ll run both full Link unit files and the repository check command concurrently via detached processes, then inspect results and the final diff/status. - Assistant
All 113 focused Link tests pass.
vp checkcompleted formatting and its detached wrapper reported done, but the exit-code file has not appeared yet, likely because the command fan-outs left a child process. I’ll inspect processes/output, run the package build as an additional type-level validation, and review the final diff. - Assistant
Validation is clean: check/lint/typecheck pass, build succeeds (with only the repository’s expected virtual-module warnings), and all Link unit tests pass. I’ll do one final semantic review against Next.js’s implementation and inspect the complete diff. In particular, I’ll verify protocol-relative external replacement resolves correctly through
location.replaceand ensure no generated build artifacts were modified.
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.