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 start by exploring the repository structure to understand the codebase and then find the relevant files for the
<Link onNavigate>prop implementation.Assistant
I need to make one tool call at a time. Let me look at the current link shim first.
Assistant
The
onNavigatecallback appears to already be partially implemented. Let me look at the full file and understand the issue better. Let me check the Next.js reference implementation.Assistant
The reference directory doesn't exist. Let me look at the test suite to understand what's expected.
Assistant
Let me look at the full component definition, especially how
onNavigateis destructured from props.Assistant
OK so
onNavigateis destructured. Let me look at the rest of the destructure to see whatrestandrestWithoutLocaleare.Assistant
The implementation seems present. Let me check the git history to understand the current state.
Assistant
Let me check if Next.js is available in node_modules to study the actual API.
Assistant
Let me look at the actual Next.js implementation in link.js.
Assistant
Let me check if there are any uncommitted changes and look at the actual implementation more carefully.
Assistant
The implementation already exists. Let me find the actual Next.js e2e test for onNavigate to understand what's expected.
Assistant
Let me look at the existing link-navigation tests to understand how to reproduce.
Assistant
Let me run the existing link-navigation tests to see the current state.
Assistant
Let me run the existing tests to verify the current state.
Assistant
The existing tests pass. The implementation looks correct. Let me write a reproduction test to confirm whether
onNavigateactually fires on click in the App Router case.Assistant
Let me create a temporary reproduction test to verify
onNavigatefires on click.Assistant
The onClick wasn't captured because I need to mock jsx-runtime too. Let me copy the full helper from the real test file.
Assistant
The unit test passes —
onNavigatefires correctly. This is confusing since the issue says it doesn't fire. Let me check the git history of theonNavigatecode to understand if it was recently added.Assistant
Let me check the Next.js docs for onNavigate and the app-dir link implementation more thoroughly.
Assistant
Let me look at the e2e test structure and search for onNavigate usage in fixtures.
Assistant
Let me look at the actual Next.js e2e test. Let me check the .agents directory and any cached references.
Assistant
Let me look at the actual Next.js e2e test for link-on-navigate-prop. Since I can't access the internet, let me reconstruct from the Next.js source. Actually, let me check if there's a way to find the test. The Next.js GitHub repo's
test/e2e/link-on-navigate-prop/index.test.ts.Let me think about what this test does based on Next.js conventions. Looking at the actual Next.js source code I know:
The
test/e2e/link-on-navigate-prop/index.test.tsin Next.js has anappfixture. Let me recall the structure. Actually, I recall the Next.js test for onNavigate. Let me reconstruct.Actually, I found the key issue. Let me re-read the issue's three bullet points carefully:
onNavigatecallback never fires on click- Click state tracking broken (link thinks it was never clicked)
- History state replacement via
onNavigatereturn value not working
Wait, these all might stem from ONE root cause: onNavigate never fires. If onNavigate never fires, then:
Assistant
The HEAD is dated 2026-05-20, matching the issue date. The onNavigate code exists. Let me examine the full
handleClickfunction carefully to find a potential bug.Assistant
Let me check if there's a built/dist version of the shim that might differ from the source.
Assistant
The dist also has onNavigate. Let me investigate how
next/linkis resolved in the vinext runtime — maybe a different link shim is used in the actual app.Assistant
So
next/linkresolves toshims/link(the link.tsx I've been looking at). So the same shim is used.Let me check the next-shims.d.ts to see the type declaration for next/link.
Assistant
Let me search for how the
.nextjs-refdirectory is referenced or set up in the repo.Assistant
Let me check if I have internet access to fetch the actual Next.js e2e test.
Assistant
I have internet access. Let me fetch the full Next.js e2e test and its fixture.
Assistant
Now I have the test. Let me fetch the fixture files to understand the
is-navigated/is-clickedsemantics.Assistant
Now I understand the fixture. Let me fetch the app-router and pages-router fixture structures.
Assistant
Let me verify the onNavigate implementation commit is actually in the current branch history.
Assistant
The implementation exists and onNavigate fires (confirmed by unit test and existing e2e test). Let me set up the actual Next.js e2e test scenarios in vinext to find the real failures. First, let me understand the e2e infrastructure.
Assistant
Let me look at the test helpers and check whether I can run the existing onNavigate e2e test.
Assistant
Let me set up the actual e2e test. First, let me look at the app-basic fixture structure.
Assistant
Let me set up the actual e2e test to find the real failures. I'll create a fixture mirroring the Next.js test. First, the shared OnNavigate component and app-router pages.
Assistant
Let me fix the
link-to-mainonNavigate to match the original fixture properly.Assistant
Let me use the edit command to fix the link-to-main onNavigate.
Assistant
Now let me create the app-router layout and pages.
Assistant
Let me verify how components are imported in this fixture (path aliases).
Assistant
The tsconfig only includes "app", "*.ts", and the next-shims.d.ts. The
componentsdirectory is NOT in the include. So my import../../../components/OnNavigatemight not be type-checked, but at runtime (Vite) it should resolve as a relative path. Let me check if there's an existingcomponentsdirectory in app-basic.Assistant
Now let me check if the dev server is already running, and write a focused e2e spec.
Assistant
Let me write a focused e2e spec mirroring the Next.js test cases.
Assistant
Now let me start the dev server and run the spec.
Assistant
The server is up. Now let me run my e2e spec.
Assistant
The browsers aren't installed. Let me install chromium.
Assistant
Excellent! This is very revealing. 4 out of 5 tests PASS — including "should trigger both onClick and onNavigate for internal navigation" and "should prevent navigation when onNavigate calls preventDefault". This confirms
onNavigateIS implemented and fires correctly.The only failure is the external-link-with-
replacetest (history.length 2→3, expected to stay 2). This matches the issue's "History state replacement" bullet.Let me verify the onNavigate code is committed at HEAD (not something I need to add).
Assistant
Now I understand the situation clearly. Let me verify the onNavigate code is committed at HEAD and confirm my analysis. The
onNavigateis already implemented and working (4/5 e2e tests pass). The only failure is the external-link-with-replaceprop. Let me look at the URL utility functions to implement the fix correctly.Assistant
Now I understand the fix needed. The
onNavigateitself works; the only failure is the external-link-with-replaceprop. Next.js callse.preventDefault()+location.replace(href)for external links withreplace, but vinext just returns early. Let me checkisDangerousSchemeto ensure my fix is safe.Assistant
Now let me implement the fix. Let me open the link.tsx file at the external link handling section.
Assistant
Let me check
applyLocaleToHrefto determine the correct URL forlocation.replace.Assistant
Confirmed: for external URLs,
resolvedHref === fullHref(no locale/basePath/trailing-slash transformation). Now let me implement the fix to handle thereplaceprop for external links, matching Next.js.Assistant
Now I need to rebuild and restart the dev server to pick up the change. Let me restart the dev server.
Assistant
Still failing. Let me verify whether the dev server is actually using the updated source. Let me check how the shim is resolved in dev.
Assistant
The dev server uses the dist version of the shims (since the plugin loads from
dist/index.js). I need to rebuild the dist. Let me check and rebuild.Assistant
Confirmed — the dist doesn't have my fix. Let me rebuild the vinext package.
Assistant
Build complete. Let me verify my fix is now in the dist.
Assistant
My fix is now in the dist. Let me restart the dev server and re-run the e2e test.
Assistant
The dev server has a 500 error about a missing dist file. Let me check the dist structure.
Assistant
The file exists — likely a stale Vite cache issue. Let me clear caches and restart.
Assistant
The dev server is failing on
server-globals.jswhich I didn't touch. Let me check if the build corrupted it.Assistant
Let me look at the full dev log from the start to understand the 500 error.
Assistant
The old dev server is still on port 4174 (my pkill missed it). The new one went to 4176. Let me properly kill all and restart on 4174.
Assistant
Port 4174 is now down. Let me find and kill any remaining actual vite node processes, then start fresh.
Assistant
Server is up on 4174. Let me re-run the e2e test.
Assistant
All 5 App Router e2e tests pass now. Let me also verify the Pages Router case. First, let me check the pages-basic fixture structure.
Assistant
The fix is in shared code (before the App/Pages branch), so it applies to both routers. Let me create a Pages Router test to verify. I'll use window flags that persist across Pages Router navigation.
Assistant
Let me rewrite the Pages Router page more cleanly using React state plus window persistence.
Assistant
Now let me start the Pages Router dev server (port 4173) and run the Pages Router spec.
Assistant
The Pages Router external-replace and preventDefault tests pass. The internal-navigation test is flaky due to my window-flag approach across navigation (a test artifact, not a vinext bug — the preventDefault test proves onNavigate fires in Pages Router). Let me verify the Pages Router does SPA navigation (so
_appstate persists, as the Next.js test expects) by checking existing Pages Router link tests.Assistant
There's an existing Pages Router SPA navigation test that waits for hydration (
__VINEXT_ROOT__) before clicking. My Pages Router test doesn't wait for hydration, causing the flakiness (Link falls back to hard navigation before the router is ready). Let me fix my test to wait for hydration.Assistant
All 4 Pages Router tests pass with the hydration wait — the flakiness was a test artifact. Both routers now pass all onNavigate scenarios.
Now, per the instructions, I must NOT add new tests to the repository. Let me first run the existing unit tests to ensure no regressions, then remove my temporary test files.
Assistant
All 1044 existing tests pass. Now let me check if there are existing unit tests covering external-link behavior that I should verify don't break.
Assistant
All 1044 tests pass. Let me check whether
distis gitignored (which affects whether my rebuilt dist is submitted).Assistant
distis gitignored and regenerated by the build step (postinstall / webServer build), so my source fix is what gets submitted and rebuilt. Now let me clean up all the temporary test files I created (per instructions, I must not add new tests). Let me remove them.Assistant
All temporary test files removed — the only change is
packages/vinext/src/shims/link.tsx. Let me review the final diff.Assistant
The diff is clean and minimal. Let me reconsider the issue's framing: the
onNavigatecallback itself was already implemented (my e2e tests confirmed it fires for both routers). The remaining real failure — matching the issue's "History state replacement" bullet — was external links withreplacenot usinglocation.replace(), which my fix now addresses.Let me do a final verification: confirm the dist has the fix and existing unit tests pass.
Assistant
All 1042 existing unit tests pass and the dist has the fix. Let me check
link.test.tsfor any onNavigate tests added in the earlier commit, to make sure my change is consistent with them.Assistant
Let me verify the Next.js onClick wrapping behavior to ensure my fix is consistent (the onClick→preventDefault→early-return ordering).
Assistant
The fix is consistent with Next.js's onClick→preventDefault→early-return ordering. Let me run the existing onNavigate e2e test (relative query) to confirm no regression.
Assistant
The existing onNavigate e2e test still passes — no regression. Let me clean up the dev servers and do a final review.
Assistant
The fix is complete and minimal. Let me do a final summary of my analysis and verification before submitting.
Summary
Investigation findings:
-
onNavigateis already implemented inpackages/vinext/src/shims/link.tsx(committed since the initial release, refined in commitcb2d1c9). I confirmed it fires by:- Writing a unit test (callback fires +
preventDefaultis respected). - Setting up the actual Next.js
link-on-navigate-prope2e fixture (App Router + Pages Router) and running it against a real vinext dev server.
- Writing a unit test (callback fires +
-
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.