fix: normalize same-origin absolute URLs for client-side navigation#336
Open
Divkix wants to merge 2 commits intocloudflare:mainfrom
Open
fix: normalize same-origin absolute URLs for client-side navigation#336Divkix wants to merge 2 commits intocloudflare:mainfrom
Divkix wants to merge 2 commits intocloudflare:mainfrom
Conversation
When <Link href="https://example.com/path"> is used and example.com matches the current origin, vinext now does SPA navigation instead of a full page reload. This matches Next.js behavior. Adds toSameOriginPath() utility that extracts the local path from a same-origin absolute URL. Applied consistently across: - link.tsx handleClick() and prefetch paths - navigation.ts navigateImpl() (App Router) - router.ts push()/replace() (Pages Router hook + singleton) Closes cloudflare#335
commit: |
There was a problem hiding this comment.
Pull request overview
This PR fixes client-side navigation for same-origin absolute URLs (e.g. http://localhost:3000/about) by normalizing them to local paths before “external URL” bail-outs, aligning behavior with Next.js and resolving #335.
Changes:
- Added
toSameOriginPath()utility to convert same-origin absolute URLs topathname + search + hash(or returnnullfor external/server contexts). - Updated Link, App Router navigation, and Pages Router
push/replaceto normalize same-origin absolute URLs before delegating to full-page navigation. - Added unit tests for
toSameOriginPath()and SSR rendering checks for<Link>with absolute URLs.
Reviewed changes
Copilot reviewed 5 out of 5 changed files in this pull request and generated 3 comments.
Show a summary per file
| File | Description |
|---|---|
tests/link.test.ts |
Adds tests for same-origin normalization utility and SSR output for absolute hrefs. |
packages/vinext/src/shims/url-utils.ts |
Introduces shared toSameOriginPath() helper used by multiple navigation shims. |
packages/vinext/src/shims/link.tsx |
Normalizes same-origin absolute URLs for prefetch/click handling to enable SPA navigation. |
packages/vinext/src/shims/navigation.ts |
Normalizes same-origin absolute URLs before external URL handling in App Router navigation. |
packages/vinext/src/shims/router.ts |
Normalizes same-origin absolute URLs before external URL handling in Pages Router push/replace. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Use window.location.origin as base for `//` URLs so they resolve correctly for same-origin detection, while keeping `new URL(url)` (no base) for http/https URLs to preserve existing behavior. Addresses Copilot review comment on PR cloudflare#336.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #335
toSameOriginPath()utility that extracts the local path (pathname + search + hash) from a same-origin absolute URL, returningnullfor truly external URLs or server-side contextslink.tsxhandleClick()and prefetch paths to normalize same-origin URLs before the external URL bail-outnavigation.tsnavigateImpl()(App Router) to normalize before external URL checkrouter.tspush()/replace()in both theuseRouterhook andRoutersingleton (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
toSameOriginPath()— same-origin returns local path, cross-origin returns null, server returns null, preserves search/hash<Link>with absolute URLsisExternalUrl()still correctly identifies truly external URLspnpm test tests/link.test.ts— 35 tests passpnpm test tests/shims.test.ts— 568 tests passpnpm run typecheck— cleanpnpm run lint— clean