fix(metadata): preserve URL instance pathname in alternates#91493
Open
mango766 wants to merge 1 commit intovercel:canaryfrom
Open
fix(metadata): preserve URL instance pathname in alternates#91493mango766 wants to merge 1 commit intovercel:canaryfrom
mango766 wants to merge 1 commit intovercel:canaryfrom
Conversation
…ing it
When a URL instance is passed for alternates.languages or
alternates.canonical, the resolveAlternateUrl function was using
`new URL(pathname, url)` which replaced the URL's own pathname with
the current page pathname. This caused URLs like
`new URL('/nl/blog', 'http://localhost:3000')` to lose their path
and always resolve to the page's own pathname.
Convert URL instances to their href string so
resolveAbsoluteUrlWithPathname treats them as absolute URLs and
preserves the full path the user intended.
Fixes vercel#68351
Collaborator
|
Allow CI Workflow Run
Note: this should only be enabled once the PR is ready to go and can only be enabled by a maintainer |
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.
What?
Fix
resolveAlternateUrlto preserve the full pathname ofURLinstances passed inmetadata.alternates.languages,alternates.canonical,alternates.media, andalternates.types.Why?
When users pass a
URLobject for alternate language URLs (e.g.new URL('/nl/blog', 'http://localhost:3000')), the URL's own pathname was being silently discarded. The old code didnew URL(pathname, url), treating the user's URL as a base and resolving it against the current page's pathname — effectively replacing/nl/blogwith/blog.This means any locale prefix, subpath, or custom path encoded in the URL was lost. The workaround was to call
.toString()on the URL, which shouldn't be necessary.Reported by multiple users in #68351, confirmed still present as of v15.2.3.
How?
In
resolveAlternateUrl, when the input is aURLinstance, convert it to itshrefstring before passing it downstream toresolveAbsoluteUrlWithPathname. This way the full path the user specified is treated as an absolute URL and preserved as-is.Before (broken):
After (fixed):
Test plan
test/e2e/app-dir/metadata/app/alternates/url-instance/page.tsxfixture that usesURLinstances forcanonicalandlanguages'should preserve URL instance pathname in alternates'to the existing metadata e2e test suiteFixes #68351