diff --git a/packages/next/src/lib/metadata/resolvers/resolve-basics.ts b/packages/next/src/lib/metadata/resolvers/resolve-basics.ts index 9a7458f7c02c5..9a7a48c68faf0 100644 --- a/packages/next/src/lib/metadata/resolvers/resolve-basics.ts +++ b/packages/next/src/lib/metadata/resolvers/resolve-basics.ts @@ -51,7 +51,9 @@ export const resolveAlternates: FieldResolverWithMetadataBase<'alternates'> = ( ) => { if (!alternates) return null const result: ResolvedAlternateURLs = { - canonical: resolveUrl(alternates.canonical, metadataBase), + canonical: metadataBase + ? resolveUrl(alternates.canonical, metadataBase) + : alternates.canonical || null, languages: null, media: null, types: null, diff --git a/packages/next/src/lib/metadata/resolvers/resolve-url.ts b/packages/next/src/lib/metadata/resolvers/resolve-url.ts index 0f1e8f0f68b19..2236283a5b09c 100644 --- a/packages/next/src/lib/metadata/resolvers/resolve-url.ts +++ b/packages/next/src/lib/metadata/resolvers/resolve-url.ts @@ -33,11 +33,11 @@ function resolveUrl( function resolveUrlValuesOfObject( obj: Record | null | undefined, metadataBase: ResolvedMetadata['metadataBase'] -): null | Record { +): null | Record { if (!obj) return null - const result: Record = {} + const result: Record = {} 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 } diff --git a/packages/next/src/lib/metadata/types/alternative-urls-types.ts b/packages/next/src/lib/metadata/types/alternative-urls-types.ts index 3018c1e46ceb6..1df4cf92ad49b 100644 --- a/packages/next/src/lib/metadata/types/alternative-urls-types.ts +++ b/packages/next/src/lib/metadata/types/alternative-urls-types.ts @@ -434,12 +434,12 @@ export type AlternateURLs = { } export type ResolvedAlternateURLs = { - canonical: null | URL - languages: null | Languages + canonical: null | string | URL + languages: null | Languages media: null | { - [media: string]: null | URL + [media: string]: null | string | URL } types: null | { - [types: string]: null | URL + [types: string]: null | string | URL } } diff --git a/test/e2e/app-dir/metadata/app/alternate/page.tsx b/test/e2e/app-dir/metadata/app/alternate/page.tsx index ac0bd68f88509..d7a6d1f179470 100644 --- a/test/e2e/app-dir/metadata/app/alternate/page.tsx +++ b/test/e2e/app-dir/metadata/app/alternate/page.tsx @@ -10,7 +10,7 @@ export const metadata = { 'de-DE': 'https://example.com/de-DE', }, media: { - 'only screen and (max-width: 600px)': 'https://example.com/mobile', + 'only screen and (max-width: 600px)': '/mobile', }, types: { 'application/rss+xml': 'https://example.com/rss', diff --git a/test/e2e/app-dir/metadata/metadata.test.ts b/test/e2e/app-dir/metadata/metadata.test.ts index 14cc949c5bfb6..e43e36cd4053b 100644 --- a/test/e2e/app-dir/metadata/metadata.test.ts +++ b/test/e2e/app-dir/metadata/metadata.test.ts @@ -204,7 +204,7 @@ createNextDescribe( it('should support alternate tags', async () => { const browser = await next.browser('/alternate') - await checkLink(browser, 'canonical', 'https://example.com/') + await checkLink(browser, 'canonical', 'https://example.com') await checkMeta( browser, 'en-US', @@ -224,7 +224,7 @@ createNextDescribe( await checkMeta( browser, 'only screen and (max-width: 600px)', - 'https://example.com/mobile', + '/mobile', 'media', 'link', 'href'