Skip to content

fix: Update URL resolution logic to handle search parameters on root path /?foo=bar #78262

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: canary
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions packages/next/src/lib/metadata/resolvers/resolve-url.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,12 @@ describe('resolveAbsoluteUrlWithPathname', () => {
'https://example.com/foo'
)
})

it('should resolve absolute internal url with query', () => {
expect(resolver('https://example.com/?foo=bar')).toBe(
'https://example.com/?foo=bar'
)
})
})

describe('trailingSlash is true', () => {
Expand Down Expand Up @@ -102,13 +108,20 @@ describe('resolveAbsoluteUrlWithPathname', () => {
expect(resolver(new URL('https://example.com/foo?bar'))).toBe(
'https://example.com/foo?bar'
)
expect(resolver('https://example.com/?foo=bar')).toBe(
'https://example.com/?foo=bar'
)
})

it('should not add trailing slash to relative url with query', () => {
expect(resolver('/foo?bar')).toBe('https://example.com/foo?bar')
expect(resolver(new URL('/foo?bar', metadataBase))).toBe(
'https://example.com/foo?bar'
)
expect(resolver('/?foo=bar')).toBe('https://example.com/?foo=bar')
expect(resolver(new URL('/?foo=bar', metadataBase))).toBe(
'https://example.com/?foo=bar'
)
})

it('should not add trailing slash to relative url that matches file pattern', () => {
Expand Down
5 changes: 4 additions & 1 deletion packages/next/src/lib/metadata/resolvers/resolve-url.ts
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,10 @@ function resolveAbsoluteUrlWithPathname(
if (typeof result === 'string') {
resolvedUrl = result
} else {
resolvedUrl = result.pathname === '/' ? result.origin : result.href
resolvedUrl =
result.pathname === '/' && result.searchParams.size === 0
? result.origin
: result.href
}

// Add trailing slash if it's enabled for urls matches the condition
Expand Down
Loading