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 find the relevant files.
Assistant
I'll start by exploring the repository structure.
Assistant
I'll search for link-related files.
Assistant
I see there's already an
onNavigateprop in the link.tsx file. Let me look at the full file, especially the truncated middle part, and the test file.Assistant
The test file doesn't exist. Let me find it.
Assistant
Let me look at the existing tests and the Next.js reference implementation for
onNavigate.Assistant
There's already an
onNavigateimplementation. Let me look at the existing tests to understand what's expected and what might be failing.Assistant
Let me look at the e2e tests and the link-navigation tests to understand the expected behavior.
Assistant
I need to make single tool calls. Let me look at the e2e tests.
Assistant
Let me look at the link-navigation.test.ts tests around the onNavigate line, and the fixtures.
Assistant
Let me look at the broader context of the link-navigation.test.ts file, especially the onNavigate-related tests.
Assistant
Now let me look at the fixtures to understand the existing onNavigate tests.
Assistant
The issue references the Next.js implementation. Let me find the Next.js reference.
Assistant
No nextjs-ref available. Let me check if there's a Next.js installation in node_modules.
Assistant
Now I understand the Next.js implementation. Let me look at the full vinext link.tsx, particularly how props are destructured and the
onNavigateextraction.Assistant
Let me run the existing tests to see the current state and what fails.
Assistant
The vitest run is timing out. Let me look at the playwright config or the test setup. Let me check what
vp testis.Actually, let me look at the vitest config first.
Assistant
Let me look at the existing link-navigation test to understand the test setup, and try to run the specific test file related to onNavigate. Let me look at the helper
mockReactAnchorCaptureForLinkOnly_DO_NOT_REUSE.Actually, let me focus. The implementation already has onNavigate. The issue is from an agent analyzing CI failures. Maybe the implementation exists but has bugs. Let me check the git log to see the state of things.
Assistant
Let me look at the app-basic fixture for the relative-query-link and the existing onNavigate tests.
Assistant
Let me check the built dist version of link to see if it has the onNavigate implementation, since apps use the built package.
Assistant
Let me search for the Next.js test file for link-on-navigate-prop.
Assistant
There's an app-dir/link.js with a different
linkClickedsignature. Let me look at the App Router implementation, which is the more relevant one.Assistant
Let me look for tests that verify onNavigate fires on a normal (non-download) click.
Assistant
Let me look at the test setup helpers to understand how to write a reproduction test.
Assistant
Let me write a reproduction test based on the existing test infrastructure to verify
onNavigatebehavior on a normal click.Assistant
The vitest run keeps timing out. Let me check how tests are normally run with
vp test.Assistant
Let me put the repro test in the tests directory and run it with the proper test runner.
Assistant
The reproduction passes! The implementation seems to work in the source. Let me check the actual Next.js test file structure to understand what 8 tests would check. Let me search for any ported version or related test patterns.
Assistant
Let me run the existing unit tests related to link to see the current state.
Assistant
All existing tests pass. Let me run the shims.test.ts onNavigate tests too.
Assistant
Let me look at the actual Next.js test file. Let me check the
.agentsdirectory and CI config for the comparison suite.Assistant
So the deploy suite runs Next.js's own e2e tests against vinext. The failing test is
test/e2e/link-on-navigate-prop/index.test.tswhich is a Next.js test. Let me look at the actual Next.js test. I need to understand what those 8 tests check.Since I can't access the internet, let me reconstruct the Next.js
test/e2e/link-on-navigate-prop/index.test.tsfrom my knowledge of Next.js. Actually, let me check if the Next.js source is available somewhere - maybe there's anext.jsdirectory in the CI setup, or the test fixtures.Let me look at scripts/e2e-deploy.sh and the test manifest to understand how tests are run, and whether there's a local copy of next.js test files.
Actually, more importantly, let me look at the Next.js test file content. The
nextpackage's npm install wouldn't includetest/directory. But maybe there's a separate next.js checkout. Let me search the whole filesystem forlink-on-navigate-prop.Assistant
Let me compare the dist build of link.js with the source to check for any divergence in the onNavigate implementation.
Assistant
Let me look at the scripts directory for how the Next.js source/tests are fetched and run.
Assistant
Let me try to fetch the actual Next.js test file from GitHub to understand exactly what's expected.
Assistant
I have internet access. Let me fetch the full test file and the fixture app it uses.
Assistant
Now I understand the actual tests - 4 tests × 2 routers = 8 tests. Let me fetch the fixture app to understand the expected behavior.
Assistant
Let me list the directory structure of the fixture app via the GitHub API.
Assistant
Now I have a complete understanding of the 8 tests. Let me examine the vinext link.tsx more carefully, especially how
normalizedHrefand external links are handled.Assistant
I now have the complete picture of the 8 tests. Let me reconstruct the exact scenarios from the Next.js fixture (
OnNavigate.tsx). Let me write a unit test reproducing all the scenarios, especially the external-link-with-replace case (test 7) which seems to be the bug.Assistant
My test harness has issues with module caching between tests. Let me fix the helper to reset modules properly.
Assistant
I've confirmed the bug. Tests 1-6 pass (onNavigate works correctly), but test 7 fails: external links with
replacedon't calle.preventDefault()+location.replace(). This matches Next.js'slinkClickedwhich hasif (replace) { e.preventDefault(); location.replace(href); }for external links. Let me checkwithBasePathto determine the right href to use.Assistant
withBasePathreturns the path unchanged for absolute URLs. SofullHrefforhttps://nextjs.orgwould behttps://nextjs.org(assuming normalizePathTrailingSlash doesn't break it). Let me check normalizePathTrailingSlash for absolute URLs.Actually, for
location.replace, I should use the href that the browser would navigate to. The anchor's rendered href isfullHref. For an absolute external URL,fullHref=https://nextjs.org. Let me verify normalizePathTrailingSlash doesn't mangle absolute URLs.Let me check normalizePathTrailingSlash.
Assistant
Let me check
applyLocaleToHreffor absolute external URLs to ensure I use the right href value.Assistant
applyLocaleToHrefreturns the href unchanged for absolute URLs. SoresolvedHref,normalizedHref, andfullHrefare allhttps://nextjs.orgfor the external link. Now let me verify there isn't a separate App Router link shim (Next.js has bothclient/link.jsandclient/app-dir/link.js).Assistant
I've confirmed the bug: tests 1-6 pass (onNavigate fires correctly), but test 7 (external link with
replace) fails because vinext returns early without callinge.preventDefault()+location.replace(). This matches Next.js'slinkClickedwhich doesif (replace) { e.preventDefault(); location.replace(href); }for external links.Let me look at the exact lines in the source to edit.
Assistant
Now let me implement the fix for the external link with
replacecase, matching Next.js'slinkClickedbehavior.Assistant
Let me use the
replacespecial command. Let me open the file first.Assistant
Now let me edit the external link branch to handle the
replacecase.Assistant
Now let me run my reproduction test again to verify all 7 scenarios pass.
Assistant
All 7 scenarios pass now. Now I need to rebuild the dist since the deploy suite uses the built package. Let me check the build setup.
Assistant
The dist is gitignored — it's built from source at install time, so my source change will be picked up by the build. Now let me run the existing link tests to make sure I haven't broken anything.
Assistant
Existing tests pass. Let me also run the shims onNavigate tests and check for any existing tests covering external link replace behavior that I might have affected.
Assistant
No existing tests cover external link replace, so nothing breaks. Now let me investigate the "click state tracking" concern — whether vinext's App Router preserves layout state (and thus
isClicked/isNavigated) across client navigation. Let me look for existing tests on layout state persistence.Assistant
Let me check git blame on the onNavigate lines to understand when it was added.
Assistant
onNavigate was implemented back in Feb 2026. So it's been there. The remaining genuine bug is the external replace (test 7). But the issue claims 8 failures. Let me investigate the "click state tracking" concern — whether layout/
_appstate persists across navigation. Let me look at existing e2e tests that check state persistence.Assistant
Let me search for tests that verify layout/
_appstate persistence across Link navigation.Assistant
There's a
layout-persistence.spec.tstest. Let me look at it.Assistant
vinext has layout persistence tests confirming App Router preserves layout state across navigation. Now let me verify the Pages Router
_appstate persistence, since the OnNavigate component wraps pages via_app.tsx.Assistant
Let me check for Pages Router
_appstate persistence tests.Assistant
Let me check the playwright config to understand how to run the e2e tests and validate onNavigate end-to-end.
Assistant
Let me try running the existing onNavigate e2e tests to validate end-to-end. First, let me build the vinext package so the dist is fresh with my changes.
Assistant
Build complete. Let me verify the dist now has my external replace fix.
Assistant
The dist now has my fix. Let me run the existing onNavigate e2e tests to validate end-to-end. Let me try the Pages Router link-advanced onNavigate test.
Assistant
Playwright browsers aren't installed. Let me install chromium.
Assistant
Now let me run the Pages Router onNavigate e2e test.
Assistant
The Pages Router onNavigate e2e test passes. Now let me run the App Router onNavigate test.
Assistant
Both onNavigate e2e tests pass, confirming onNavigate fires end-to-end in both routers. Now let me create a temporary fixture to validate the click-state-tracking (isClicked/isNavigated persisting) and the external-replace behavior end-to-end, mirroring the Next.js
OnNavigatefixture. Let me look at the app-basic fixture structure.Assistant
Let me create a temporary fixture mirroring the Next.js
OnNavigatecomponent to validate click-state tracking and external-replace end-to-end. First the App Router fixture.Assistant
Now let me create a temporary e2e test for the App Router scenario.
Assistant
All 3 App Router temp tests pass, including the external-replace history.length check. Let me confirm the external-replace test would FAIL without my fix (to prove the fix is what resolves it). Let me temporarily revert the source, rebuild, and run just that test.
Assistant
Confirmed: without my fix, the external-replace test FAILS (history.length 2→3, a push instead of replace). Now let me restore my fix and rebuild.
Assistant
My fix is restored and rebuilt. Let me run the full temp test suite once more to confirm all pass with the fix.
Assistant
All 3 pass with the fix. Now let me validate the Pages Router scenarios too (the Next.js fixture tests both routers). Let me create a Pages Router fixture.
Assistant
For the Pages Router, the
OnNavigatestate must live in_appto persist across navigation. Let me check if there's an existing test confirming_appstate persists across Pages Router navigation.Assistant
There's a test "_app.tsx wrapper persists across client navigations" in hydration.spec.ts. Let me look at it to confirm _app persistence is a tested behavior.
Assistant
There are existing tests confirming:
- "_app.tsx wrapper persists across client navigations" — _app persists.
- "state is preserved within client navigation" — but this is about the page's own state being preserved when navigating away and back? Let me read the rest.
Actually, the "state is preserved within client navigation" test increments a counter, navigates away, comes back, and checks the count. Let me read the rest to understand — in Pages Router, when you navigate away and back, the page component remounts, so state resets. Unless vinext preserves it. Let me read.
Assistant
In Pages Router, the page component remounts but
_app.tsxpersists (confirmed by existing test). SinceOnNavigatelives in_app, its state persists — matching the Next.js fixture. Let me also validate the external-replace behavior in the Pages Router. Let me create a Pages Router fixture page and temp test.Assistant
The Pages Router external-replace test passes too. I've validated all scenarios. Now let me clean up all the temporary fixtures and tests I created.
Assistant
Only the source change to
link.tsxremains (dist is gitignored). Let me run the existing onNavigate unit tests in shims.test.ts and the full link test suites once more to confirm nothing broke.Assistant
The
-tfilter caused the other files to be skipped. Let me run link.test.ts and link-navigation.test.ts separately.Assistant
All 113 tests pass. Let me review my final change.
Assistant
My change is clean and minimal. Let me verify edge cases are handled correctly by reviewing the full handleClick once more, then run the broader unit test suite to ensure nothing else breaks.
Assistant
All 4485 unit tests pass. Let me run the linter/typecheck on the changed file to ensure it passes the repo's strict rules.
Assistant
Lint passes. Now let me investigate one remaining concern for the "click state tracking" theme: whether vinext's Pages Router preserves
_app's ReactuseStateacross client navigation (not just the DOM). Let me examine the Pages Router client navigation.Assistant
Let me look at the re-render logic around line 669 to understand whether _app is re-imported/remounted on each navigation, and how the element is constructed.
Assistant
Key insight: vinext caches
AppComponentinwindow.__VINEXT_APP__and re-renders it at a consistent position viaroot.render(), so React preserves_app'suseStateacross Pages Router client navigations. Combined with the existing "_app.tsx wrapper persists" test, this confirms_appstate (whereOnNavigatelives) persists — so the "click state tracking" works.Let me do a final cleanup check to ensure no temp files remain.
Assistant
No temp files remain. To be 100% certain about the "click state tracking" concern for the Pages Router (where
OnNavigatelives in_app), let me run an isolated Pages Router state-persistence test. I'll back up the original_app, create a temporary one with state, run the test, then fully restore.Assistant
Both Pages Router state-persistence tests pass, confirming
_appstate (isClicked/isNavigated) persists and onNavigate fires + preventDefault works for the Pages Router too. Now let me clean up all temp files and restore the original_app.tsx.Assistant
Let me verify the
_app.tsxis fully restored to its original state.Assistant
The
_app.tsxis unchanged. Only the link.tsx change remains. Let me do a final rebuild to ensure the dist (used by e2e/integration locally) is consistent with the source, and verify it has the fix.Assistant
The dist is rebuilt and has the fix. Let me do a final end-to-end confirmation by running the existing onNavigate e2e tests for both routers.
Assistant
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.