useRelativeHref#96068
Draft
acdlite wants to merge 1 commit into
Draft
Conversation
acdlite
force-pushed
the
use-relative-href
branch
from
July 22, 2026 17:21
74488f5 to
304efed
Compare
Contributor
Stats from current PR🔴 1 regression, 1 improvement
📊 All Metrics📖 Metrics GlossaryDev Server Metrics:
Build Metrics:
Change Thresholds:
⚡ Dev Server
📦 Dev Server (Webpack) (Legacy)📦 Dev Server (Webpack)
⚡ Production Builds
📦 Production Builds (Webpack) (Legacy)📦 Production Builds (Webpack)
📦 Bundle SizesBundle Sizes⚡ TurbopackClient Main Bundles
Server Middleware
Build DetailsBuild Manifests
📦 WebpackClient Main Bundles
Polyfills
Pages
Server Edge SSR
Middleware
Build DetailsBuild Manifests
Build Cache
🔄 Shared (bundler-independent)Runtimes
📝 Changed Files (12 files)Files with changes:
View diffsapp-page-exp..ntime.dev.jsfailed to diffapp-page-exp..time.prod.jsfailed to diffapp-page-tur..ntime.dev.jsfailed to diffapp-page-tur..time.prod.jsfailed to diffapp-page-tur..ntime.dev.jsfailed to diffapp-page-tur..time.prod.jsfailed to diffapp-page.runtime.dev.jsfailed to diffapp-page.runtime.prod.jsfailed to diffapp-route-ex..ntime.dev.jsDiff too large to display app-route-tu..ntime.dev.jsDiff too large to display app-route-tu..ntime.dev.jsDiff too large to display app-route.runtime.dev.jsDiff too large to display 📎 Tarball URLCommit: c709c9d |
Contributor
Tests PassedCommit: 33a1f85 |
acdlite
force-pushed
the
use-relative-href
branch
4 times, most recently
from
July 22, 2026 20:44
d267c18 to
c709c9d
Compare
Adds a new App Router hook, useRelativeHref(target) (currently prefixed with
unstable_), which computes a relative URL reference from the current page to a
target URL. For example, if the current page is '/chat/123/settings', then
`useRelativeHref('/chat/[id]')` returns './' and
`useRelativeHref('/chat/[id]/members')` returns '../members/'. Neither result
references the value of [id]. The result can be passed directly to a Link or
anchor element's href prop.
The benefit of relative hrefs is that you can link to pages without needing to
reference dynamic params in the shared part of the URL. This means that unlike
`usePathname`, `useRelativeHref` can often be included during static
prerendering (i.e. without being wrapped in a Suspense boundary).
There will still be cases where a relative href cannot be generated
statically. If the target is at or below the current page, the result must
spell the page's final URL segment back out (relative resolution drops it):
on '/chat/123', `useRelativeHref('/chat/[id]/settings')` returns
'./123/settings/', which includes the value of [id] and therefore deopts when
that value is only known at request time. Routes with a catch-all param
always resolve at request time, since the amount of traversal depends on how
many segments the catch-all matched. However, not all pages that use
catch-all params will necessarily trigger a dynamic deopt: as long as there's
at least one parallel route that can be fully resolved, we can know the shape
of the URL.
Targets that aren't root-relative paths — absolute URLs ('https://…',
'mailto:…'), protocol-relative URLs, and hash- or query-only references
('#faq', '?tab=1') — are returned unchanged, so any href that's valid on a
<Link> can be passed through the hook without changing its behavior.
The current page's URL parts are computed on the server and sent to the
client as part of the initial RSC payload, so the initial render — including
hydration — resolves hrefs against the same values on both sides. After the
first router state update, the hook resolves against the URL pathname
instead, the same source that populates usePathname; on the client these are
always equivalent.
acdlite
force-pushed
the
use-relative-href
branch
from
July 22, 2026 23:07
c709c9d to
33a1f85
Compare
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.
Adds a new App Router hook, useRelativeHref(target) (currently prefixed with unstable_), which computes a relative URL reference from the current page to a target URL. For example, if the current page is '/chat/123/settings', then
useRelativeHref('/chat/[id]')returns './' anduseRelativeHref('/chat/[id]/members')returns '../members/'. Neither result references the value of [id]. The result can be passed directly to a Link or anchor element's href prop.The benefit of relative hrefs is that you can link to pages without needing to reference dynamic params in the shared part of the URL. This means that unlike
usePathname,useRelativeHrefcan often be included during static prerendering (i.e. without being wrapped in a Suspense boundary).There will still be cases where a relative href cannot be generated statically. If the target is at or below the current page, the result must spell the page's final URL segment back out (relative resolution drops it): on '/chat/123',
useRelativeHref('/chat/[id]/settings')returns './123/settings/', which includes the value of [id] and therefore deopts when that value is only known at request time. Routes with a catch-all param always resolve at request time, since the amount of traversal depends on how many segments the catch-all matched. However, not all pages that use catch-all params will necessarily trigger a dynamic deopt: as long as there's at least one parallel route that can be fully resolved, we can know the shape of the URL.Targets that aren't root-relative paths — absolute URLs ('https://…', 'mailto:…'), protocol-relative URLs, and hash- or query-only references ('#faq', '?tab=1') — are returned unchanged, so any href that's valid on a
<Link>can be passed through the hook without changing its behavior.The current page's URL parts are computed on the server and sent to the client as part of the initial RSC payload, so the initial render — including hydration — resolves hrefs against the same values on both sides. After the first router state update, the hook resolves against the URL pathname instead, the same source that populates usePathname; on the client these are always equivalent.
Review notes
Most of the diff is tests. I would start by reviewing the
computeRelativeHreffunction, here:next.js/packages/next/src/shared/lib/relative-href.ts
Lines 127 to 264 in 33a1f85
That function contains the core algorithm.