Unverified Commit bc14f192 authored by F7b5's avatar F7b5 Committed by GitHub

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

## What?

This PR fixes and implements test for an URL resolution issue that
stripped the searchparam when `metadataBase` was defined and a search
param was set on the root path. This created a behavior where :

```
export const metadata: Metadata = {
  title: "Create Next App",
  description: "Generated by create next app",
  metadataBase: new URL("https://www.example.com"),
  alternates: {
    canonical: "/?foo=bar",
  },
};
```

would result in 

`<link rel="canonical" href="https://www.example.com">` 

instead of 

`<link rel="canonical" href="https://www.example.com/?foo=bar">`



## Related issues

- Fixes #65776
- Fixes #74689
Co-authored-by: 's avatarJiwon Choi <devjiwonchoi@gmail.com>
parent 3f6ffba8
......@@ -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', () => {
......@@ -102,6 +108,9 @@ 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', () => {
......@@ -109,6 +118,10 @@ describe('resolveAbsoluteUrlWithPathname', () => {
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', () => {
......
......@@ -114,7 +114,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
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment