Unverified Commit b5f92c44 authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Fix: canonical should allow relative urls (#46584)

alternate urls should allow string type for relative paths

## Bug

Fixes #45824

- [x] Related issues linked using `fixes #number`
- [x] Integration tests added
- [ ] Errors have a helpful link attached, see
[`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)

---------
parent 5532b6a9
...@@ -51,7 +51,9 @@ export const resolveAlternates: FieldResolverWithMetadataBase<'alternates'> = ( ...@@ -51,7 +51,9 @@ export const resolveAlternates: FieldResolverWithMetadataBase<'alternates'> = (
) => { ) => {
if (!alternates) return null if (!alternates) return null
const result: ResolvedAlternateURLs = { const result: ResolvedAlternateURLs = {
canonical: resolveUrl(alternates.canonical, metadataBase), canonical: metadataBase
? resolveUrl(alternates.canonical, metadataBase)
: alternates.canonical || null,
languages: null, languages: null,
media: null, media: null,
types: null, types: null,
......
...@@ -33,11 +33,11 @@ function resolveUrl( ...@@ -33,11 +33,11 @@ function resolveUrl(
function resolveUrlValuesOfObject( function resolveUrlValuesOfObject(
obj: Record<string, string | URL | null> | null | undefined, obj: Record<string, string | URL | null> | null | undefined,
metadataBase: ResolvedMetadata['metadataBase'] metadataBase: ResolvedMetadata['metadataBase']
): null | Record<string, URL | null> { ): null | Record<string, string | URL | null> {
if (!obj) return null if (!obj) return null
const result: Record<string, URL | null> = {} const result: Record<string, URL | string | null> = {}
for (const [key, value] of Object.entries(obj)) { for (const [key, value] of Object.entries(obj)) {
result[key as keyof typeof obj] = resolveUrl(value, metadataBase) result[key] = metadataBase ? resolveUrl(value, metadataBase) : value
} }
return result return result
} }
......
...@@ -434,12 +434,12 @@ export type AlternateURLs = { ...@@ -434,12 +434,12 @@ export type AlternateURLs = {
} }
export type ResolvedAlternateURLs = { export type ResolvedAlternateURLs = {
canonical: null | URL canonical: null | string | URL
languages: null | Languages<null | URL> languages: null | Languages<null | string | URL>
media: null | { media: null | {
[media: string]: null | URL [media: string]: null | string | URL
} }
types: null | { types: null | {
[types: string]: null | URL [types: string]: null | string | URL
} }
} }
...@@ -10,7 +10,7 @@ export const metadata = { ...@@ -10,7 +10,7 @@ export const metadata = {
'de-DE': 'https://example.com/de-DE', 'de-DE': 'https://example.com/de-DE',
}, },
media: { media: {
'only screen and (max-width: 600px)': 'https://example.com/mobile', 'only screen and (max-width: 600px)': '/mobile',
}, },
types: { types: {
'application/rss+xml': 'https://example.com/rss', 'application/rss+xml': 'https://example.com/rss',
......
...@@ -204,7 +204,7 @@ createNextDescribe( ...@@ -204,7 +204,7 @@ createNextDescribe(
it('should support alternate tags', async () => { it('should support alternate tags', async () => {
const browser = await next.browser('/alternate') const browser = await next.browser('/alternate')
await checkLink(browser, 'canonical', 'https://example.com/') await checkLink(browser, 'canonical', 'https://example.com')
await checkMeta( await checkMeta(
browser, browser,
'en-US', 'en-US',
...@@ -224,7 +224,7 @@ createNextDescribe( ...@@ -224,7 +224,7 @@ createNextDescribe(
await checkMeta( await checkMeta(
browser, browser,
'only screen and (max-width: 600px)', 'only screen and (max-width: 600px)',
'https://example.com/mobile', '/mobile',
'media', 'media',
'link', 'link',
'href' 'href'
......
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