Unverified Commit 41e2613a authored by Jiachi Liu's avatar Jiachi Liu Committed by GitHub

Update robots meta and add verification rendering (#45409)

Follow up for: #45237

* Adding more fields for robots meta, most related to googlebot
* Add `me` and `yandex` field and render for verification field
parent d3a9f5a5
import type { ResolvedMetadata } from '../types/metadata-interface' import type { ResolvedMetadata } from '../types/metadata-interface'
import React from 'react' import React from 'react'
import { Meta } from './meta' import { Meta, MultiMeta } from './meta'
export function BasicMetadata({ metadata }: { metadata: ResolvedMetadata }) { export function BasicMetadata({ metadata }: { metadata: ResolvedMetadata }) {
return ( return (
...@@ -100,9 +100,7 @@ export function AppleWebAppMeta({ ...@@ -100,9 +100,7 @@ export function AppleWebAppMeta({
{capable ? ( {capable ? (
<meta name="apple-mobile-web-app-capable" content="yes" /> <meta name="apple-mobile-web-app-capable" content="yes" />
) : null} ) : null}
{title ? ( <Meta name="apple-mobile-web-app-title" content={title} />
<meta name="apple-mobile-web-app-title" content={title} />
) : null}
{startupImage {startupImage
? startupImage.map((image, index) => ( ? startupImage.map((image, index) => (
<link <link
...@@ -122,3 +120,31 @@ export function AppleWebAppMeta({ ...@@ -122,3 +120,31 @@ export function AppleWebAppMeta({
</> </>
) )
} }
export function VerificationMeta({
verification,
}: {
verification: ResolvedMetadata['verification']
}) {
if (!verification) return null
return (
<>
<MultiMeta
namePrefix="google-site-verification"
contents={verification.google}
/>
<MultiMeta namePrefix="y_key" contents={verification.yahoo} />
<MultiMeta
namePrefix="yandex-verification"
contents={verification.yandex}
/>
<MultiMeta namePrefix="me" contents={verification.me} />
{verification.other
? Object.entries(verification.other).map(([key, value], index) => (
<MultiMeta key={key + index} namePrefix={key} contents={value} />
))
: null}
</>
)
}
...@@ -91,6 +91,7 @@ export function MultiMeta({ ...@@ -91,6 +91,7 @@ export function MultiMeta({
} else { } else {
return ( return (
<ExtendMeta <ExtendMeta
key={keyPrefix + '_' + index}
namePrefix={namePrefix} namePrefix={namePrefix}
propertyPrefix={propertyPrefix} propertyPrefix={propertyPrefix}
content={content} content={content}
......
...@@ -6,6 +6,7 @@ import { ...@@ -6,6 +6,7 @@ import {
FormatDetectionMeta, FormatDetectionMeta,
ItunesMeta, ItunesMeta,
BasicMetadata, BasicMetadata,
VerificationMeta,
} from './generate/basic' } from './generate/basic'
import { AlternatesMetadata } from './generate/alternate' import { AlternatesMetadata } from './generate/alternate'
import { import {
...@@ -27,6 +28,7 @@ export async function Metadata({ metadata }: { metadata: any }) { ...@@ -27,6 +28,7 @@ export async function Metadata({ metadata }: { metadata: any }) {
<AlternatesMetadata alternates={resolved.alternates} /> <AlternatesMetadata alternates={resolved.alternates} />
<ItunesMeta itunes={resolved.itunes} /> <ItunesMeta itunes={resolved.itunes} />
<FormatDetectionMeta formatDetection={resolved.formatDetection} /> <FormatDetectionMeta formatDetection={resolved.formatDetection} />
<VerificationMeta verification={resolved.verification} />
<AppleWebAppMeta appleWebApp={resolved.appleWebApp} /> <AppleWebAppMeta appleWebApp={resolved.appleWebApp} />
<OpenGraphMetadata openGraph={resolved.openGraph} /> <OpenGraphMetadata openGraph={resolved.openGraph} />
<TwitterMetadata twitter={resolved.twitter} /> <TwitterMetadata twitter={resolved.twitter} />
......
...@@ -10,6 +10,7 @@ import type { ...@@ -10,6 +10,7 @@ import type {
Icon, Icon,
IconDescriptor, IconDescriptor,
Icons, Icons,
ResolvedVerification,
} from './types/metadata-types' } from './types/metadata-types'
import { createDefaultMetadata } from './default-metadata' import { createDefaultMetadata } from './default-metadata'
import { resolveOpenGraph } from './resolve-opengraph' import { resolveOpenGraph } from './resolve-opengraph'
...@@ -76,22 +77,26 @@ const resolveViewport: FieldResolver<'viewport'> = (viewport) => { ...@@ -76,22 +77,26 @@ const resolveViewport: FieldResolver<'viewport'> = (viewport) => {
return resolved return resolved
} }
const VerificationKeys = ['google', 'yahoo', 'yandex', 'me', 'other'] as const
const resolveVerification: FieldResolver<'verification'> = (verification) => { const resolveVerification: FieldResolver<'verification'> = (verification) => {
const google = resolveAsArrayOrUndefined(verification?.google) if (!verification) return null
const yahoo = resolveAsArrayOrUndefined(verification?.yahoo) const res = {} as ResolvedVerification
let other: Record<string, (string | number)[]> | undefined
if (verification?.other) { for (const key of VerificationKeys) {
other = {} const value = verification[key]
for (const key in verification.other) { if (value) {
const value = resolveAsArrayOrUndefined(verification.other[key]) if (key === 'other') {
if (value) other[key] = value res.other = {}
for (const otherKey in verification.other) {
const otherValue = resolveAsArrayOrUndefined(
verification.other[otherKey]
)
if (otherValue) res.other[otherKey] = otherValue
}
} else res[key] = resolveAsArrayOrUndefined(value) as (string | number)[]
} }
} }
return { return res
google,
yahoo,
other,
}
} }
function isStringOrURL(icon: any): icon is string | URL { function isStringOrURL(icon: any): icon is string | URL {
...@@ -203,23 +208,39 @@ const resolveAppLinks: FieldResolver<'appLinks'> = (appLinks) => { ...@@ -203,23 +208,39 @@ const resolveAppLinks: FieldResolver<'appLinks'> = (appLinks) => {
return appLinks as ResolvedMetadata['appLinks'] return appLinks as ResolvedMetadata['appLinks']
} }
const robotsKeys = [
'noarchive',
'nosnippet',
'noimageindex',
'nocache',
'notranslate',
'indexifembedded',
'nositelinkssearchbox',
'unavailable_after',
'max-video-preview',
'max-image-preview',
'max-snippet',
] as const
const resolveRobotsValue: (robots: Metadata['robots']) => string | null = ( const resolveRobotsValue: (robots: Metadata['robots']) => string | null = (
robots robots
) => { ) => {
if (!robots) return null if (!robots) return null
if (typeof robots === 'string') return robots if (typeof robots === 'string') return robots
const values = [] const values: string[] = []
if (robots.index) values.push('index') if (robots.index) values.push('index')
else if (typeof robots.index === 'boolean') values.push('noindex') else if (typeof robots.index === 'boolean') values.push('noindex')
if (robots.follow) values.push('follow') if (robots.follow) values.push('follow')
else if (typeof robots.follow === 'boolean') values.push('nofollow') else if (typeof robots.follow === 'boolean') values.push('nofollow')
if (robots.noarchive) values.push('noarchive')
if (robots.nosnippet) values.push('nosnippet') for (const key of robotsKeys) {
if (robots.noimageindex) values.push('noimageindex') const value = robots[key]
if (robots.nocache) values.push('nocache') if (typeof value !== 'undefined' && value !== false) {
values.push(typeof value === 'boolean' ? key : `${key}:${value}`)
}
}
return values.join(', ') return values.join(', ')
} }
......
...@@ -59,6 +59,13 @@ type RobotsInfo = { ...@@ -59,6 +59,13 @@ type RobotsInfo = {
nosnippet?: boolean nosnippet?: boolean
noimageindex?: boolean noimageindex?: boolean
nocache?: boolean nocache?: boolean
notranslate?: boolean
indexifembedded?: boolean
nositelinkssearchbox?: boolean
unavailable_after?: string
'max-video-preview'?: number | string
'max-image-preview'?: 'none' | 'standard' | 'large'
'max-snippet'?: number
} }
export type Robots = RobotsInfo & { export type Robots = RobotsInfo & {
// if you want to specify an alternate robots just for google // if you want to specify an alternate robots just for google
...@@ -93,6 +100,8 @@ export type Icons = { ...@@ -93,6 +100,8 @@ export type Icons = {
export type Verification = { export type Verification = {
google?: null | string | number | (string | number)[] google?: null | string | number | (string | number)[]
yahoo?: null | string | number | (string | number)[] yahoo?: null | string | number | (string | number)[]
yandex?: null | string | number | (string | number)[]
me?: null | string | number | (string | number)[]
// if you ad-hoc additional verification // if you ad-hoc additional verification
other?: { other?: {
[name: string]: string | number | (string | number)[] [name: string]: string | number | (string | number)[]
...@@ -102,6 +111,8 @@ export type Verification = { ...@@ -102,6 +111,8 @@ export type Verification = {
export type ResolvedVerification = { export type ResolvedVerification = {
google?: null | (string | number)[] google?: null | (string | number)[]
yahoo?: null | (string | number)[] yahoo?: null | (string | number)[]
yandex?: null | (string | number)[]
me?: null | (string | number)[]
other?: { other?: {
[name: string]: (string | number)[] [name: string]: (string | number)[]
} }
......
...@@ -12,6 +12,10 @@ export const metadata = { ...@@ -12,6 +12,10 @@ export const metadata = {
index: true, index: true,
follow: false, follow: false,
noimageindex: true, noimageindex: true,
'max-video-preview': 'standard',
'max-image-preview': -1,
'max-snippet': -1,
}, },
}, },
} }
export default function page() {
return 'verification'
}
export const metadata = {
verification: {
google: 'google',
yandex: 'yandex',
yahoo: 'yahoo',
other: {
me: ['my-email', 'my-link'],
},
},
}
...@@ -235,10 +235,27 @@ createNextDescribe( ...@@ -235,10 +235,27 @@ createNextDescribe(
await checkMetaNameContentPair( await checkMetaNameContentPair(
browser, browser,
'googlebot', 'googlebot',
'index, nofollow, noimageindex' 'index, nofollow, noimageindex, max-video-preview:standard, max-image-preview:-1, max-snippet:-1'
) )
}) })
it('should support verification tags', async () => {
const browser = await next.browser('/verification')
await checkMetaNameContentPair(
browser,
'google-site-verification',
'google'
)
await checkMetaNameContentPair(browser, 'y_key', 'yahoo')
await checkMetaNameContentPair(
browser,
'yandex-verification',
'yandex'
)
await checkMetaNameContentPair(browser, 'me', ['my-email', 'my-link'])
})
it('should support appLinks tags', async () => { it('should support appLinks tags', async () => {
const browser = await next.browser('/app-links') const browser = await next.browser('/app-links')
await checkMetaPropertyContentPair( await checkMetaPropertyContentPair(
......
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