[PR #336] [MERGED] fix: normalize same-origin absolute URLs for client-side navigation #488

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

📋 Pull Request Information

Original PR: https://github.com/cloudflare/vinext/pull/336
Author: @Divkix
Created: 3/8/2026
Status: Merged
Merged: 3/8/2026
Merged by: @james-elicx

Base: mainHead: fix/link-same-origin-absolute-url


📝 Commits (3)

  • 0f91200 fix: normalize same-origin absolute URLs for client-side navigation
  • 4f0ac02 fix: handle protocol-relative URLs in toSameOriginPath()
  • 9c2a132 Update tests/link.test.ts

📊 Changes

5 files changed (+193 additions, -33 deletions)

View changed files

📝 packages/vinext/src/shims/link.tsx (+25 -9)
📝 packages/vinext/src/shims/navigation.ts (+14 -7)
📝 packages/vinext/src/shims/router.ts (+33 -16)
packages/vinext/src/shims/url-utils.ts (+26 -0)
📝 tests/link.test.ts (+95 -1)

📄 Description

Summary

Fixes #335

  • Adds toSameOriginPath() utility that extracts the local path (pathname + search + hash) from a same-origin absolute URL, returning null for truly external URLs or server-side contexts
  • Updates link.tsx handleClick() and prefetch paths to normalize same-origin URLs before the external URL bail-out
  • Updates navigation.ts navigateImpl() (App Router) to normalize before external URL check
  • Updates router.ts push()/replace() in both the useRouter hook and Router singleton (Pages Router)

Root Cause

isExternalUrl() treated any URL with a scheme as external — no same-origin comparison. When <Link href="http://localhost:3000/about"> was used and the origin matched, it triggered a full page reload instead of SPA navigation.

Test plan

  • Unit tests for toSameOriginPath() — same-origin returns local path, cross-origin returns null, server returns null, preserves search/hash
  • SSR rendering tests for <Link> with absolute URLs
  • isExternalUrl() still correctly identifies truly external URLs
  • pnpm test tests/link.test.ts — 35 tests pass
  • pnpm test tests/shims.test.ts — 568 tests pass
  • pnpm run typecheck — clean
  • pnpm run lint — clean

🔄 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/336 **Author:** [@Divkix](https://github.com/Divkix) **Created:** 3/8/2026 **Status:** ✅ Merged **Merged:** 3/8/2026 **Merged by:** [@james-elicx](https://github.com/james-elicx) **Base:** `main` ← **Head:** `fix/link-same-origin-absolute-url` --- ### 📝 Commits (3) - [`0f91200`](https://github.com/cloudflare/vinext/commit/0f912004e1a9803d96adf7fde7dd6361dc17c02e) fix: normalize same-origin absolute URLs for client-side navigation - [`4f0ac02`](https://github.com/cloudflare/vinext/commit/4f0ac02b0c9c545b8f97a73330265bddfc6d6ad6) fix: handle protocol-relative URLs in toSameOriginPath() - [`9c2a132`](https://github.com/cloudflare/vinext/commit/9c2a13207bdb4aecf23dbb74e1240cffd1c37be6) Update tests/link.test.ts ### 📊 Changes **5 files changed** (+193 additions, -33 deletions) <details> <summary>View changed files</summary> 📝 `packages/vinext/src/shims/link.tsx` (+25 -9) 📝 `packages/vinext/src/shims/navigation.ts` (+14 -7) 📝 `packages/vinext/src/shims/router.ts` (+33 -16) ➕ `packages/vinext/src/shims/url-utils.ts` (+26 -0) 📝 `tests/link.test.ts` (+95 -1) </details> ### 📄 Description ## Summary Fixes #335 - Adds `toSameOriginPath()` utility that extracts the local path (pathname + search + hash) from a same-origin absolute URL, returning `null` for truly external URLs or server-side contexts - Updates `link.tsx` `handleClick()` and prefetch paths to normalize same-origin URLs before the external URL bail-out - Updates `navigation.ts` `navigateImpl()` (App Router) to normalize before external URL check - Updates `router.ts` `push()`/`replace()` in both the `useRouter` hook and `Router` singleton (Pages Router) ## Root Cause `isExternalUrl()` treated any URL with a scheme as external — no same-origin comparison. When `<Link href="http://localhost:3000/about">` was used and the origin matched, it triggered a full page reload instead of SPA navigation. ## Test plan - [x] Unit tests for `toSameOriginPath()` — same-origin returns local path, cross-origin returns null, server returns null, preserves search/hash - [x] SSR rendering tests for `<Link>` with absolute URLs - [x] `isExternalUrl()` still correctly identifies truly external URLs - [x] `pnpm test tests/link.test.ts` — 35 tests pass - [x] `pnpm test tests/shims.test.ts` — 568 tests pass - [x] `pnpm run typecheck` — clean - [x] `pnpm run lint` — clean --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
BreizhHardware 2026-05-06 13:08:21 +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#488
No description provided.