[PR #483] [MERGED] fix: Link onNavigate URL resolution for relative hrefs #605

Closed
opened 2026-05-06 13:09:01 +02:00 by BreizhHardware · 0 comments

📋 Pull Request Information

Original PR: https://github.com/cloudflare/vinext/pull/483
Author: @JaredStowell
Created: 3/12/2026
Status: Merged
Merged: 3/12/2026
Merged by: @james-elicx

Base: mainHead: jstowell/fix-link-onnavigate-relative-hrefs


📝 Commits (4)

  • 74994f9 Fix onNavigate relative href report
  • d05dcd8 Verify onNavigate relative href
  • 786d92a Fix onNavigate URL mismatch
  • 18e75b0 Review url-utils changes

📊 Changes

12 files changed (+389 additions, -95 deletions)

View changed files

📝 packages/vinext/src/shims/link.tsx (+21 -48)
📝 packages/vinext/src/shims/navigation.ts (+4 -10)
📝 packages/vinext/src/shims/router.ts (+17 -35)
📝 packages/vinext/src/shims/url-utils.ts (+93 -0)
📝 tests/e2e/app-router/nextjs-compat/navigation.spec.ts (+24 -0)
📝 tests/e2e/pages-router/link-advanced.spec.ts (+22 -0)
📝 tests/fixtures/app-basic/app/nextjs-compat/nav-link-test/page.tsx (+2 -0)
tests/fixtures/app-basic/app/nextjs-compat/nav-link-test/relative-query-link.tsx (+34 -0)
📝 tests/fixtures/app-basic/next-shims.d.ts (+1 -0)
📝 tests/fixtures/pages-basic/next-shims.d.ts (+1 -0)
📝 tests/fixtures/pages-basic/pages/link-test.tsx (+14 -0)
📝 tests/link.test.ts (+156 -2)

📄 Description

Fix next/link onNavigate so its callback URL matches the actual navigation target for relative hrefs.

This aligns Link callback behavior with the browser/history destination for cases like ?page=2, #hash, and other relative targets, including when basePath is configured.

What changed

  • fix onNavigate.url in the next/link shim to use the same resolved target as navigation
  • centralize relative href + basePath browser URL resolution in shared URL helpers
  • fix same-origin absolute URL handling so existing basePath prefixes are not applied twice
  • fix rendered anchor href behavior under basePath for relative values
  • update next/navigation and next/router to use the same shared browser-target resolution path
  • keep fixture onNavigate typings aligned with Next.js and use local casts where vinext exposes event.url

Tests

Added and updated regression coverage for:

  • pure relative href resolution
  • basePath + locale-prefixed browser targets
  • same-origin absolute URLs that already include basePath
  • SSR anchor rendering under basePath
  • Pages Router Link onNavigate relative query navigation
  • App Router Link onNavigate relative query navigation

Verification

Ran targeted checks:

  • pnpm test tests/link.test.ts
  • PLAYWRIGHT_PROJECT=pages-router pnpm exec playwright test tests/e2e/pages-router/link-advanced.spec.ts -g "onNavigate reports the resolved URL for relative query hrefs"
  • PLAYWRIGHT_PROJECT=app-router pnpm exec playwright test tests/e2e/app-router/nextjs-compat/navigation.spec.ts -g "Link onNavigate reports the resolved URL for relative query hrefs"
  • pnpm run typecheck
  • pnpm run fmt

🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/cloudflare/vinext/pull/483 **Author:** [@JaredStowell](https://github.com/JaredStowell) **Created:** 3/12/2026 **Status:** ✅ Merged **Merged:** 3/12/2026 **Merged by:** [@james-elicx](https://github.com/james-elicx) **Base:** `main` ← **Head:** `jstowell/fix-link-onnavigate-relative-hrefs` --- ### 📝 Commits (4) - [`74994f9`](https://github.com/cloudflare/vinext/commit/74994f9f188356dd1296ea68a38fa1f1f2ffa00f) Fix onNavigate relative href report - [`d05dcd8`](https://github.com/cloudflare/vinext/commit/d05dcd8da197319c941d109ddb759e92721298d5) Verify onNavigate relative href - [`786d92a`](https://github.com/cloudflare/vinext/commit/786d92a520f810e35768190d4ea92aaf08109657) Fix onNavigate URL mismatch - [`18e75b0`](https://github.com/cloudflare/vinext/commit/18e75b0ef7ae60393eed0e4363e3aff5c2f6ea06) Review url-utils changes ### 📊 Changes **12 files changed** (+389 additions, -95 deletions) <details> <summary>View changed files</summary> 📝 `packages/vinext/src/shims/link.tsx` (+21 -48) 📝 `packages/vinext/src/shims/navigation.ts` (+4 -10) 📝 `packages/vinext/src/shims/router.ts` (+17 -35) 📝 `packages/vinext/src/shims/url-utils.ts` (+93 -0) 📝 `tests/e2e/app-router/nextjs-compat/navigation.spec.ts` (+24 -0) 📝 `tests/e2e/pages-router/link-advanced.spec.ts` (+22 -0) 📝 `tests/fixtures/app-basic/app/nextjs-compat/nav-link-test/page.tsx` (+2 -0) ➕ `tests/fixtures/app-basic/app/nextjs-compat/nav-link-test/relative-query-link.tsx` (+34 -0) 📝 `tests/fixtures/app-basic/next-shims.d.ts` (+1 -0) 📝 `tests/fixtures/pages-basic/next-shims.d.ts` (+1 -0) 📝 `tests/fixtures/pages-basic/pages/link-test.tsx` (+14 -0) 📝 `tests/link.test.ts` (+156 -2) </details> ### 📄 Description Fix `next/link` `onNavigate` so its callback URL matches the actual navigation target for relative hrefs. This aligns `Link` callback behavior with the browser/history destination for cases like `?page=2`, `#hash`, and other relative targets, including when `basePath` is configured. ## What changed - fix `onNavigate.url` in the `next/link` shim to use the same resolved target as navigation - centralize relative href + `basePath` browser URL resolution in shared URL helpers - fix same-origin absolute URL handling so existing `basePath` prefixes are not applied twice - fix rendered anchor `href` behavior under `basePath` for relative values - update `next/navigation` and `next/router` to use the same shared browser-target resolution path - keep fixture `onNavigate` typings aligned with Next.js and use local casts where vinext exposes `event.url` ## Tests Added and updated regression coverage for: - pure relative href resolution - `basePath` + locale-prefixed browser targets - same-origin absolute URLs that already include `basePath` - SSR anchor rendering under `basePath` - Pages Router `Link onNavigate` relative query navigation - App Router `Link onNavigate` relative query navigation ## Verification Ran targeted checks: - `pnpm test tests/link.test.ts` - `PLAYWRIGHT_PROJECT=pages-router pnpm exec playwright test tests/e2e/pages-router/link-advanced.spec.ts -g "onNavigate reports the resolved URL for relative query hrefs"` - `PLAYWRIGHT_PROJECT=app-router pnpm exec playwright test tests/e2e/app-router/nextjs-compat/navigation.spec.ts -g "Link onNavigate reports the resolved URL for relative query hrefs"` - `pnpm run typecheck` - `pnpm run fmt` --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
BreizhHardware 2026-05-06 13:09:01 +02:00
Sign in to join this conversation.
No milestone
No project
No assignees
1 participant
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set.

Reference
starred/vinext#605
No description provided.