-
Notifications
You must be signed in to change notification settings - Fork 27.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Support metadata icons field (#45105)
NEXT-400 ## Feature - [x] Implements an existing feature request or RFC. Make sure the feature request has been accepted for implementation before opening a PR. - [ ] Related issues linked using `fixes #number` - [x] [e2e](https://github.com/vercel/next.js/blob/canary/contributing/core/testing.md#writing-tests-for-nextjs) tests added - [ ] Documentation added - [ ] Telemetry added. In case of a feature if it's used or not. - [ ] Errors have a helpful link attached, see [`contributing.md`](https://github.com/vercel/next.js/blob/canary/contributing.md)
- Loading branch information
Showing
15 changed files
with
295 additions
and
54 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,69 @@ | ||
import type { ResolvedMetadata } from '../types/metadata-interface' | ||
import type { Icon, IconDescriptor } from '../types/metadata-types' | ||
|
||
import React from 'react' | ||
|
||
const resolveUrl = (url: string | URL) => | ||
typeof url === 'string' ? url : url.toString() | ||
|
||
function IconDescriptorLink({ icon }: { icon: IconDescriptor }) { | ||
const { url, rel = 'icon', ...props } = icon | ||
|
||
return <link rel={rel} href={resolveUrl(url)} {...props} /> | ||
} | ||
|
||
function IconLink({ rel, icon }: { rel?: string; icon: Icon }) { | ||
if (typeof icon === 'object' && !(icon instanceof URL)) { | ||
if (rel) icon.rel = rel | ||
return <IconDescriptorLink icon={icon} /> | ||
} else { | ||
const href = resolveUrl(icon) | ||
return <link rel={rel} href={href} /> | ||
} | ||
} | ||
|
||
export function ResolvedIconsMetadata({ | ||
icons, | ||
}: { | ||
icons: ResolvedMetadata['icons'] | ||
}) { | ||
if (!icons) return null | ||
|
||
const shortcutList = icons.shortcut | ||
const iconList = icons.icon | ||
const appleList = icons.apple | ||
const otherList = icons.other | ||
|
||
return ( | ||
<> | ||
{shortcutList | ||
? shortcutList.map((icon, index) => ( | ||
<IconLink | ||
key={`shortcut-${index}`} | ||
rel="shortcut icon" | ||
icon={icon} | ||
/> | ||
)) | ||
: null} | ||
{iconList | ||
? iconList.map((icon, index) => ( | ||
<IconLink key={`shortcut-${index}`} rel="icon" icon={icon} /> | ||
)) | ||
: null} | ||
{appleList | ||
? appleList.map((icon, index) => ( | ||
<IconLink | ||
key={`apple-${index}`} | ||
rel="apple-touch-icon" | ||
icon={icon} | ||
/> | ||
)) | ||
: null} | ||
{otherList | ||
? otherList.map((icon, index) => ( | ||
<IconDescriptorLink key={`other-${index}`} icon={icon} /> | ||
)) | ||
: null} | ||
</> | ||
) | ||
} |
File renamed without changes.
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
export function resolveAsArrayOrUndefined<T = any>( | ||
value: T | T[] | undefined | null | ||
): undefined | T[] { | ||
if (typeof value === 'undefined' || value === null) { | ||
return undefined | ||
} | ||
if (Array.isArray(value)) { | ||
return value | ||
} | ||
return [value] | ||
} |
8 changes: 6 additions & 2 deletions
8
packages/next/src/lib/metadata/ui.tsx → packages/next/src/lib/metadata/metadata.tsx
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,23 @@ | ||
import React from 'react' | ||
|
||
import type { ResolvedMetadata } from './types/metadata-interface' | ||
|
||
import React from 'react' | ||
import { ResolvedBasicMetadata } from './generate/basic' | ||
import { ResolvedAlternatesMetadata } from './generate/alternate' | ||
import { ResolvedOpenGraphMetadata } from './generate/opengraph' | ||
import { resolveMetadata } from './resolve-metadata' | ||
import { ResolvedIconsMetadata } from './generate/icons' | ||
|
||
// Generate the actual React elements from the resolved metadata. | ||
export async function Metadata({ metadata }: { metadata: any }) { | ||
if (!metadata) return null | ||
|
||
const resolved: ResolvedMetadata = await resolveMetadata(metadata) | ||
return ( | ||
<> | ||
<ResolvedBasicMetadata metadata={resolved} /> | ||
<ResolvedAlternatesMetadata metadata={resolved} /> | ||
<ResolvedOpenGraphMetadata openGraph={resolved.openGraph} /> | ||
<ResolvedIconsMetadata icons={resolved.icons} /> | ||
</> | ||
) | ||
} |
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export default function page() { | ||
return 'icons' | ||
} | ||
|
||
export const metadata = { | ||
icons: { | ||
icon: [{ url: '/icon.png' }, new URL('/icon.png', 'https://example.com')], | ||
shortcut: ['/shortcut-icon.png'], | ||
apple: [ | ||
{ url: '/apple-icon.png' }, | ||
{ url: '/apple-icon-x3.png', sizes: '180x180', type: 'image/png' }, | ||
], | ||
other: [ | ||
{ | ||
rel: 'apple-touch-icon-precomposed', | ||
url: '/apple-touch-icon-precomposed.png', | ||
}, | ||
], | ||
}, | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
export default function page() { | ||
return 'icons' | ||
} | ||
|
||
export const metadata = { | ||
icons: { | ||
icon: '/icon.png', | ||
shortcut: '/shortcut-icon.png', | ||
apple: '/apple-icon.png', | ||
other: { | ||
rel: 'apple-touch-icon-precomposed', | ||
url: '/apple-touch-icon-precomposed.png', | ||
}, | ||
}, | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
export default function page() { | ||
return 'icons' | ||
} | ||
|
||
export const metadata = { | ||
icons: '/icon.png', | ||
} |
Oops, something went wrong.