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 locate relevant files. Let's first get an overview.
- Assistant
Let me explore the repository structure more broadly first.
- Assistant
Let me look at the test file mentioned in the issue and the Link component implementation.
- Assistant
Let me check the e2e test directory and find the link-on-navigate-prop test.
- Assistant
The issue mentions
test/e2e/link-on-navigate-prop/index.test.tsbut I don't see it in the e2e directory. Let me search more broadly. - Assistant
Let me look for any onNavigate related files.
- Assistant
Let me look at the key files: the Link shim implementation, the existing tests, and the Next.js reference implementation.
- Assistant
Let me now look at the Next.js reference implementation of onNavigate:
- Assistant
Let me look at the test file mentioned in the issue description to understand what behavior is expected:
- Assistant
Let me check the shims.test.ts and link-navigation.test.ts for onNavigate-related tests:
- Assistant
Let me check the actual test file that's failing.
- Assistant
Let me check the e2e test files:
- Assistant
Now let me check the pages-router link-advanced spec:
- Assistant
Let me check the link-navigation.test.ts file more closely:
- Assistant
Let me look at the specific onNavigate tests:
- Assistant
Let me search for the actual test cases related to onNavigate to understand what's expected:
- Assistant
Let me look at the test file more carefully to find all the onNavigate tests:
- Assistant
Let me look at the specific test section for onNavigate:
- Assistant
Let me look at the shims.test.ts for onNavigate tests:
- Assistant
Now let me look at the Next.js reference implementation more closely to understand how
onNavigateworks: - Assistant
Let me now check the test suite for the link-on-navigate-prop tests that are failing:
- Assistant
Let me look at the pages router link-test page to understand the existing tests:
- Assistant
Now let me look at the app-router relative query link:
- Assistant
Now let me run the existing test suite to understand which tests are failing:
- Assistant
Let me check how tests are run in this repo:
- Assistant
Let me try to run the tests using the vp command:
- Assistant
Let me check the vite config and test config.
- Assistant
Let me try running the specific test with the vp command:
- Assistant
The unit tests pass. Let me look at the link.test.ts for onNavigate tests:
- Assistant
Let me look for the Next.js reference test:
- Assistant
Let me look at what the issue actually mentions. The issue says the tests are from
test/e2e/link-on-navigate-prop/index.test.tsin Next.js. Let me search for the.nextjs-refdirectory: - Assistant
Let me run the e2e tests for the onNavigate features:
- Assistant
Let me look at the Next.js reference implementation for
linkClickedmore carefully: - Assistant
Let me look at the full Link component to see how props are destructured:
- Assistant
Let me see the full Link component destructuring and the
handleClickfunction: - Assistant
Let me look at the destructured props in the Link component.
- Assistant
Let me see the full handleClick function.
- Assistant
Let me look at the full handleClick implementation:
- Assistant
Let me look at the Next.js reference test to understand the exact behavior expected:
- Assistant
Let me look at the issue description more carefully. It says the test file is
test/e2e/link-on-navigate-prop/index.test.tsbut that's from the Next.js repo, not from vinext. Let me look at the Next.js repo for that test file: - Assistant
Let me read the AGENTS.md for context:
- Assistant
Let me clone the Next.js repo to look at the test file mentioned in the issue:
- Assistant
Good. Now let me look at the Next.js test for
link-on-navigate-prop: - Assistant
No results. Let me search for onNavigate in the Next.js test suite:
- Assistant
Let me search for onNavigate tests in the Next.js repo:
- Assistant
Let me look at those files:
- Assistant
Now let me look at the shared OnNavigate component:
- Assistant
Let me try again.
- Assistant
Let me understand what the issue really is. Let me look at the url-utils.ts for
resolveRelativeHref: - Assistant
Now let me run the relevant tests to see which are actually failing. Let me look at the shims.test.ts for the onNavigate tests:
- Assistant
Good, the onNavigate unit tests pass. Now let me understand what the Next.js reference test expects and what vinext is currently doing. Let me look at the app-dir link.tsx too:
- Assistant
- Assistant
Let me check the git history for the link.tsx file to understand the state of the code:
- Assistant
Let me look at the app-dir/link.tsx in the next.js source to understand what the expected behavior should be:
- Assistant
Let me check when onNavigate was first added to vinext:
- Assistant
Let me check when onNavigate was added to link.tsx:
- Assistant
Let me now check the actual test fixtures for app-router to understand what test pages exist:
- Assistant
Let me look at the nav-link-test page.tsx:
- Assistant
Let me check the full structure of the Next.js test for onNavigate:
- Assistant
Now let me look at the fixture pages to understand the test structure:
- Assistant
Let me look at the layout that wraps with OnNavigate:
- Assistant
Now let me understand the issue better. Let me look at the tests in the Next.js test file and the pages router version:
- Assistant
Let me now carefully trace through the issue. Looking at the current implementation and the Next.js test expectations, I need to understand what's actually failing. Let me look at the link.tsx more carefully and trace the behavior for each test case:
- Assistant
Let me look at the key section of handleClick more carefully, especially around the external link handling:
- Assistant
Let me understand the overall issue better. Let me look at the Next.js test expectations vs what vinext does for each test case:
- Assistant
Let me carefully study the exact differences between the Next.js and vinext implementations. Let me look at what the actual Next.js
linkClickedfunction does: - Assistant
Now I have a complete understanding. Let me trace through the exact behavior for each test case. The main issues I can identify are:
- External links with
replace: Next.js callse.preventDefault()andlocation.replace(href), vinext doesn't - The
try/catcharound onNavigate could mask errors: Ifnew URL()throws, onNavigate is silently skipped
Let me now write a reproduction test first, then fix the code:
- External links with
- Assistant
Let me run the existing tests to make sure everything passes before I make changes:
- Assistant
Now I understand the full picture. Let me carefully study the differences between the Next.js implementation and vinext's implementation. Let me now look at the exact Next.js
linkClickedbehavior for all the edge cases: - Assistant
Now I have a clear understanding of the issue. Let me trace through each test case to identify what's different between the Next.js behavior and vinext:
The key differences in the
linkClickedfunction:- External links with
replace: Next.js callse.preventDefault()thenlocation.replace(href). Vinext just returns without this handling. - The onNavigate callback: Both implementations call it for local URLs after
e.preventDefault(). - The onNavigate event: Next.js only provides
{ preventDefault }. Vinext provides{ url, preventDefault, defaultPrevented }.
Looking at the Next.js test
test/e2e/link-on-navigate-prop/index.test.ts, I need to trace through each test case:- Modifier key click: Both fire onClick but not onNavigate → ✅ Vinext handles this correctly
- Internal navigation: Both should fire → ✅ Handled correctly
- External links with
- Assistant
The
new URL()doesn't throw for these cases, so thetry/catchisn't the issue. Now let me focus on the actual fix needed. The main issue is that for external links withreplace, vinext doesn't handlee.preventDefault()+location.replace()like Next.js does.Let me now implement the fix:
- Assistant
Now I have a clear understanding. Let me look at the full
handleClickfunction and implement the fix for external links withreplace: - Assistant
Now I have a clear understanding of the issue. Let me trace through the problem:
Looking at the Next.js
linkClickedfunction (both pages and app-dir), the key behavior is:- For non-local URLs with
replace:e.preventDefault()+location.replace(href), then return (noonNavigate) - For local URLs:
e.preventDefault(), thenonNavigate({ preventDefault }), then navigate
In vinext's current implementation:
- For external URLs: just returns without handling
replace✗ - For local URLs:
e.preventDefault(), thenonNavigate, then navigate ✓
The main bug is #1 - external links with
replacedon't getlocation.replace()treatment. But looking at the test expectations more carefully, I also notice that for ALL external links (with or without replace), Next.js does NOT callonNavigate. Let me verify vinext handles this correctly too.Let me now fix the code:
- For non-local URLs with
- Assistant
Now I understand the full picture. Let me implement the fix. The key issue is that for external links with
replace, vinext needs to:- Call
e.preventDefault() - Call
location.replace(href)
This matches Next.js behavior. Let me make the edit:
- Call
- Assistant
Let me use the proper edit syntax:
- Assistant
Let me use the correct format for the edit command.
- Assistant
Good, I've fixed the external links with
replaceissue. Now let me also check the existing tests and build to make sure the changes are correct: - Assistant
Let me also run the link.test.ts and shims.test.ts to make sure nothing is broken:
- Assistant
The filter was too narrow. Let me run the full test files:
- Assistant
Good, all existing tests still pass. Now let me also build the vinext package to make sure the TypeScript changes compile:
- Assistant
Build completed successfully. Now let me verify the changes are correct by looking at the final file:
- Assistant
Good. Now let me also check the built dist files to make sure the fix is properly compiled:
- Assistant
Let me find the specific section in the built dist:
- Assistant
Good, the fix is in the dist output. Let me verify the surrounding code:
- Assistant
Good, the dist file has the fix. Now let me verify the full test suite passes and think about whether there are any other changes needed. Let me look at the actual Next.js test to understand ALL the test cases:
- Assistant
The test file has 7 tests that run for 2 routers. Let me now check the issue more carefully. The issue says the "onNavigate callback never fires" but looking at the code, it DOES fire for local URLs. The issue was created by an automated agent analyzing CI failures. Let me check if there's a test to verify the behavior.
Let me now also make sure the existing e2e tests pass by checking the test fixture pages:
- Assistant
Now let me look at the existing tests and see if there are additional things to fix. Let me also look at the url-utils to see if there are any related issues:
- Assistant
Now let me verify the fix is correct by running all relevant tests. Let me also make sure the dist output is correct:
- Assistant
All 1042 tests pass. Let me verify the dist files are up to date:
- Assistant
Good. Let me also verify the type declaration file:
- Assistant
Good. Now let me look at the complete change I made and verify it's correct:
- Assistant
The change looks clean and minimal. Let me verify the fix by reviewing the exact behavior:
- Before the fix: External links with
replaceprop just returned early, letting the browser handle navigation normally (adding a new history entry). - After the fix: External links with
replacenow calle.preventDefault()to stop the default browser navigation, thenlocation.replace(resolvedHref)to replace the current history entry. This matches Next.js'slinkClickedbehavior exactly.
This handles:
- The "should replace history state for external links with replace prop" test case from
test/e2e/link-on-navigate-prop/index.test.ts - External links without
replacestill return early and let the browser handle them (unchanged) onNavigateis NOT called for external links (matching Next.js behavior)
Let me also verify the existing e2e tests still reference the correct test expectations:
- Before the fix: External links with
- Assistant
All 113 tests pass. Now let me rebuild the dist to make sure it's up to date:
- Assistant
Build completed successfully. Let me now do a final review of the entire change:
- Assistant
Let me now verify the full test suite one more time before submitting:
Other models, same or similar prompt
Responses were produced in different, uncontrolled contexts (system prompts, settings, prior turns). This is not a benchmark.