Unverified Commit 259cbc18 authored by JJ Kasper's avatar JJ Kasper Committed by GitHub

Revert "Revert "Initial support for metadata (#44729)"" (#45113)

parent 0a5d2728
...@@ -7,6 +7,7 @@ import { sep } from 'path' ...@@ -7,6 +7,7 @@ import { sep } from 'path'
import { verifyRootLayout } from '../../../lib/verifyRootLayout' import { verifyRootLayout } from '../../../lib/verifyRootLayout'
import * as Log from '../../../build/output/log' import * as Log from '../../../build/output/log'
import { APP_DIR_ALIAS } from '../../../lib/constants' import { APP_DIR_ALIAS } from '../../../lib/constants'
import { resolveFileBasedMetadataForLoader } from '../../../lib/metadata/resolve-metadata'
const FILE_TYPES = { const FILE_TYPES = {
layout: 'layout', layout: 'layout',
...@@ -36,7 +37,10 @@ async function createTreeCodeFromPath({ ...@@ -36,7 +37,10 @@ async function createTreeCodeFromPath({
resolveParallelSegments, resolveParallelSegments,
}: { }: {
pagePath: string pagePath: string
resolve: (pathname: string) => Promise<string | undefined> resolve: (
pathname: string,
resolveDir?: boolean
) => Promise<string | undefined>
resolveParallelSegments: ( resolveParallelSegments: (
pathname: string pathname: string
) => [key: string, segment: string][] ) => [key: string, segment: string][]
...@@ -44,6 +48,7 @@ async function createTreeCodeFromPath({ ...@@ -44,6 +48,7 @@ async function createTreeCodeFromPath({
const splittedPath = pagePath.split(/[\\/]/) const splittedPath = pagePath.split(/[\\/]/)
const appDirPrefix = splittedPath[0] const appDirPrefix = splittedPath[0]
const pages: string[] = [] const pages: string[] = []
let rootLayout: string | undefined let rootLayout: string | undefined
let globalError: string | undefined let globalError: string | undefined
...@@ -51,6 +56,7 @@ async function createTreeCodeFromPath({ ...@@ -51,6 +56,7 @@ async function createTreeCodeFromPath({
segments: string[] segments: string[]
): Promise<{ ): Promise<{
treeCode: string treeCode: string
treeMetadataCode: string
}> { }> {
const segmentPath = segments.join('/') const segmentPath = segments.join('/')
...@@ -65,12 +71,26 @@ async function createTreeCodeFromPath({ ...@@ -65,12 +71,26 @@ async function createTreeCodeFromPath({
parallelSegments.push(...resolveParallelSegments(segmentPath)) parallelSegments.push(...resolveParallelSegments(segmentPath))
} }
let metadataCode = ''
for (const [parallelKey, parallelSegment] of parallelSegments) { for (const [parallelKey, parallelSegment] of parallelSegments) {
if (parallelSegment === PAGE_SEGMENT) { if (parallelSegment === PAGE_SEGMENT) {
const matchedPagePath = `${appDirPrefix}${segmentPath}/page` const matchedPagePath = `${appDirPrefix}${segmentPath}/page`
const resolvedPagePath = await resolve(matchedPagePath) const resolvedPagePath = await resolve(matchedPagePath)
if (resolvedPagePath) pages.push(resolvedPagePath) if (resolvedPagePath) pages.push(resolvedPagePath)
metadataCode += `{
type: 'page',
layer: ${
// There's an extra virtual segment.
segments.length - 1
},
mod: () => import(/* webpackMode: "eager" */ ${JSON.stringify(
resolvedPagePath
)}),
path: ${JSON.stringify(resolvedPagePath)},
},`
// Use '' for segment as it's the page. There can't be a segment called '' so this is the safest way to add it. // Use '' for segment as it's the page. There can't be a segment called '' so this is the safest way to add it.
props[parallelKey] = `['', {}, { props[parallelKey] = `['', {}, {
page: [() => import(/* webpackMode: "eager" */ ${JSON.stringify( page: [() => import(/* webpackMode: "eager" */ ${JSON.stringify(
...@@ -80,9 +100,8 @@ async function createTreeCodeFromPath({ ...@@ -80,9 +100,8 @@ async function createTreeCodeFromPath({
} }
const parallelSegmentPath = segmentPath + '/' + parallelSegment const parallelSegmentPath = segmentPath + '/' + parallelSegment
const { treeCode: subtreeCode } = await createSubtreePropsFromSegmentPath( const { treeCode: subtreeCode, treeMetadataCode: subTreeMetadataCode } =
[...segments, parallelSegment] await createSubtreePropsFromSegmentPath([...segments, parallelSegment])
)
// `page` is not included here as it's added above. // `page` is not included here as it's added above.
const filePaths = await Promise.all( const filePaths = await Promise.all(
...@@ -101,6 +120,27 @@ async function createTreeCodeFromPath({ ...@@ -101,6 +120,27 @@ async function createTreeCodeFromPath({
rootLayout = layoutPath rootLayout = layoutPath
} }
// Collect metadata for the layout
if (layoutPath) {
metadataCode += `{
type: 'layout',
layer: ${segments.length},
mod: () => import(/* webpackMode: "eager" */ ${JSON.stringify(
layoutPath
)}),
path: ${JSON.stringify(layoutPath)},
},`
}
metadataCode += await resolveFileBasedMetadataForLoader(
segments.length,
(await resolve(`${appDirPrefix}${parallelSegmentPath}/`, true))!
)
metadataCode += subTreeMetadataCode
if (!rootLayout) {
rootLayout = layoutPath
}
if (!globalError) { if (!globalError) {
globalError = await resolve( globalError = await resolve(
`${appDirPrefix}${parallelSegmentPath}/${GLOBAL_ERROR_FILE_TYPE}` `${appDirPrefix}${parallelSegmentPath}/${GLOBAL_ERROR_FILE_TYPE}`
...@@ -133,13 +173,16 @@ async function createTreeCodeFromPath({ ...@@ -133,13 +173,16 @@ async function createTreeCodeFromPath({
.map(([key, value]) => `${key}: ${value}`) .map(([key, value]) => `${key}: ${value}`)
.join(',\n')} .join(',\n')}
}`, }`,
treeMetadataCode: metadataCode,
} }
} }
const { treeCode } = await createSubtreePropsFromSegmentPath([]) const { treeCode, treeMetadataCode } =
await createSubtreePropsFromSegmentPath([])
return { return {
treeCode: `const tree = ${treeCode}.children;`, treeCode: `const tree = ${treeCode}.children;`,
pages, treeMetadataCode: `const metadata = [${treeMetadataCode}];`,
pages: `const pages = ${JSON.stringify(pages)};`,
rootLayout, rootLayout,
globalError, globalError,
} }
...@@ -197,7 +240,7 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{ ...@@ -197,7 +240,7 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{
const rest = path.slice(pathname.length + 1).split('/') const rest = path.slice(pathname.length + 1).split('/')
let matchedSegment = rest[0] let matchedSegment = rest[0]
// It is the actual page, mark it sepcially. // It is the actual page, mark it specially.
if (rest.length === 1 && matchedSegment === 'page') { if (rest.length === 1 && matchedSegment === 'page') {
matchedSegment = PAGE_SEGMENT matchedSegment = PAGE_SEGMENT
} }
...@@ -212,7 +255,11 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{ ...@@ -212,7 +255,11 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{
return Object.entries(matched) return Object.entries(matched)
} }
const resolver = async (pathname: string) => { const resolver = async (pathname: string, resolveDir?: boolean) => {
if (resolveDir) {
return createAbsolutePath(appDir, pathname)
}
try { try {
const resolved = await resolve(this.rootContext, pathname) const resolved = await resolve(this.rootContext, pathname)
this.addDependency(resolved) this.addDependency(resolved)
...@@ -230,8 +277,13 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{ ...@@ -230,8 +277,13 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{
} }
} }
const { treeCode, pages, rootLayout, globalError } = const {
await createTreeCodeFromPath({ treeCode,
treeMetadataCode,
pages: pageListCode,
rootLayout,
globalError,
} = await createTreeCodeFromPath({
pagePath, pagePath,
resolve: resolver, resolve: resolver,
resolveParallelSegments, resolveParallelSegments,
...@@ -263,7 +315,8 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{ ...@@ -263,7 +315,8 @@ const nextAppLoader: webpack.LoaderDefinitionFunction<{
const result = ` const result = `
export ${treeCode} export ${treeCode}
export const pages = ${JSON.stringify(pages)} export ${treeMetadataCode}
export ${pageListCode}
export { default as AppRouter } from 'next/dist/client/components/app-router' export { default as AppRouter } from 'next/dist/client/components/app-router'
export { default as LayoutRouter } from 'next/dist/client/components/layout-router' export { default as LayoutRouter } from 'next/dist/client/components/layout-router'
......
...@@ -64,6 +64,7 @@ interface IEntry { ...@@ -64,6 +64,7 @@ interface IEntry {
? "runtime?: 'nodejs' | 'experimental-edge' | 'edge'" ? "runtime?: 'nodejs' | 'experimental-edge' | 'edge'"
: '' : ''
} }
metadata?: any
} }
// ============= // =============
......
import React from 'react'
export function DefaultHead() {
return (
<>
<meta charSet="utf-8" />
<meta name="viewport" content="width=device-width, initial-scale=1" />
</>
)
}
import type { ResolvedMetadata } from './types/metadata-interface'
export const createDefaultMetadata = (): ResolvedMetadata => {
return {
viewport: 'width=device-width, initial-scale=1',
// Other values are all null
metadataBase: null,
title: null,
description: null,
applicationName: null,
authors: null,
generator: null,
keywords: null,
referrer: null,
themeColor: null,
colorScheme: null,
creator: null,
publisher: null,
robots: null,
alternates: {
canonical: null,
languages: {},
},
icons: null,
openGraph: null,
twitter: null,
verification: {},
appleWebApp: null,
formatDetection: null,
itunes: null,
abstract: null,
appLinks: null,
archives: null,
assets: null,
bookmarks: null,
category: null,
classification: null,
other: {},
}
}
import type { ResolvedMetadata } from '../types/metadata-interface'
import React from 'react'
export function ResolvedAlternatesMetadata({
metadata,
}: {
metadata: ResolvedMetadata
}) {
return (
<>
{metadata.alternates.canonical ? (
<link rel="canonical" href={metadata.alternates.canonical.toString()} />
) : null}
{Object.entries(metadata.alternates.languages).map(([locale, url]) =>
url ? (
<link
key={locale}
rel="alternate"
hrefLang={locale}
href={url.toString()}
/>
) : null
)}
{metadata.alternates.media
? Object.entries(metadata.alternates.media).map(([media, url]) =>
url ? (
<link
key={media}
rel="alternate"
media={media}
href={url.toString()}
/>
) : null
)
: null}
{metadata.alternates.types
? Object.entries(metadata.alternates.types).map(([type, url]) =>
url ? (
<link
key={type}
rel="alternate"
type={type}
href={url.toString()}
/>
) : null
)
: null}
</>
)
}
import type { ResolvedMetadata } from '../types/metadata-interface'
import React from 'react'
import { Meta } from './utils'
export function ResolvedBasicMetadata({
metadata,
}: {
metadata: ResolvedMetadata
}) {
return (
<>
<meta charSet="utf-8" />
{metadata.title !== null ? (
<title>{metadata.title.absolute}</title>
) : null}
<Meta name="description" content={metadata.description} />
<Meta name="application-name" content={metadata.applicationName} />
<Meta name="author" content={metadata.authors?.join(',')} />
<Meta name="generator" content={metadata.generator} />
<Meta name="keywords" content={metadata.keywords?.join(',')} />
<Meta name="referrer" content={metadata.referrer} />
<Meta name="theme-color" content={metadata.themeColor} />
<Meta name="color-scheme" content={metadata.colorScheme} />
<Meta name="viewport" content={metadata.viewport} />
<Meta name="creator" content={metadata.creator} />
<Meta name="publisher" content={metadata.publisher} />
<Meta name="robots" content={metadata.robots} />
<Meta name="abstract" content={metadata.abstract} />
{metadata.archives
? metadata.archives.map((archive) => (
<link rel="archives" href={archive} key={archive} />
))
: null}
{metadata.assets
? metadata.assets.map((asset) => (
<link rel="assets" href={asset} key={asset} />
))
: null}
{metadata.bookmarks
? metadata.bookmarks.map((bookmark) => (
<link rel="bookmarks" href={bookmark} key={bookmark} />
))
: null}
<Meta name="category" content={metadata.category} />
<Meta name="classification" content={metadata.classification} />
{Object.entries(metadata.other).map(([name, content]) => (
<Meta
key={name}
name={name}
content={Array.isArray(content) ? content.join(',') : content}
/>
))}
</>
)
}
import type { ResolvedMetadata } from '../types/metadata-interface'
import React from 'react'
import { Meta, MultiMeta } from './utils'
export function ResolvedOpenGraphMetadata({
openGraph,
}: {
openGraph: ResolvedMetadata['openGraph']
}) {
if (!openGraph) {
return null
}
let typedOpenGraph
if ('type' in openGraph) {
switch (openGraph.type) {
case 'website':
typedOpenGraph = <Meta property="og:type" content="website" />
break
case 'article':
typedOpenGraph = (
<>
<Meta property="og:type" content="article" />
<Meta
property="article:published_time"
content={openGraph.publishedTime?.toString()}
/>
<Meta
property="article:modified_time"
content={openGraph.modifiedTime?.toString()}
/>
<Meta
property="article:expiration_time"
content={openGraph.expirationTime?.toString()}
/>
<MultiMeta
propertyPrefix="article:author"
contents={openGraph.authors}
/>
<Meta property="article:section" content={openGraph.section} />
<MultiMeta propertyPrefix="article:tag" contents={openGraph.tags} />
</>
)
break
case 'book':
typedOpenGraph = (
<>
<Meta property="og:type" content="book" />
<Meta property="book:isbn" content={openGraph.isbn} />
<Meta
property="book:release_date"
content={openGraph.releaseDate}
/>
<MultiMeta
propertyPrefix="book:author"
contents={openGraph.authors}
/>
<MultiMeta propertyPrefix="book:tag" contents={openGraph.tags} />
</>
)
break
case 'profile':
typedOpenGraph = (
<>
<Meta property="og:type" content="profile" />
<Meta property="profile:first_name" content={openGraph.firstName} />
<Meta property="profile:last_name" content={openGraph.lastName} />
<Meta property="profile:username" content={openGraph.username} />
<Meta property="profile:gender" content={openGraph.gender} />
</>
)
break
case 'music.song':
typedOpenGraph = (
<>
<Meta property="og:type" content="music.song" />
<Meta
property="music:duration"
content={openGraph.duration?.toString()}
/>
<MultiMeta
propertyPrefix="music:album"
contents={openGraph.albums}
/>
<MultiMeta
propertyPrefix="music:musician"
contents={openGraph.musicians}
/>
</>
)
break
case 'music.album':
typedOpenGraph = (
<>
<Meta property="og:type" content="music.album" />
<MultiMeta propertyPrefix="music:song" contents={openGraph.songs} />
<MultiMeta
propertyPrefix="music:musician"
contents={openGraph.musicians}
/>
<Meta
property="music:release_date"
content={openGraph.releaseDate}
/>
</>
)
break
case 'music.playlist':
typedOpenGraph = (
<>
<Meta property="og:type" content="music.playlist" />
<MultiMeta propertyPrefix="music:song" contents={openGraph.songs} />
<MultiMeta
propertyPrefix="music:creator"
contents={openGraph.creators}
/>
</>
)
break
case 'music.radio_station':
typedOpenGraph = (
<>
<Meta property="og:type" content="music.radio_station" />
<MultiMeta
propertyPrefix="music:creator"
contents={openGraph.creators}
/>
</>
)
break
case 'video.movie':
typedOpenGraph = (
<>
<Meta property="og:type" content="video.movie" />
<MultiMeta
propertyPrefix="video:actor"
contents={openGraph.actors}
/>
<MultiMeta
propertyPrefix="video:director"
contents={openGraph.directors}
/>
<MultiMeta
propertyPrefix="video:writer"
contents={openGraph.writers}
/>
<Meta property="video:duration" content={openGraph.duration} />
<Meta
property="video:release_date"
content={openGraph.releaseDate}
/>
<MultiMeta propertyPrefix="video:tag" contents={openGraph.tags} />
</>
)
break
case 'video.episode':
typedOpenGraph = (
<>
<Meta property="og:type" content="video.episode" />
<MultiMeta
propertyPrefix="video:actor"
contents={openGraph.actors}
/>
<MultiMeta
propertyPrefix="video:director"
contents={openGraph.directors}
/>
<MultiMeta
propertyPrefix="video:writer"
contents={openGraph.writers}
/>
<Meta property="video:duration" content={openGraph.duration} />
<Meta
property="video:release_date"
content={openGraph.releaseDate}
/>
<MultiMeta propertyPrefix="video:tag" contents={openGraph.tags} />
<Meta property="video:series" content={openGraph.series} />
</>
)
break
case 'video.tv_show':
typedOpenGraph = <Meta property="og:type" content="video.tv_show" />
break
case 'video.other':
typedOpenGraph = <Meta property="og:type" content="video.other" />
break
default:
throw new Error('Invalid OpenGraph type: ' + (openGraph as any).type)
}
}
return (
<>
<Meta property="og:determiner" content={openGraph.determiner} />
<Meta property="og:title" content={openGraph.title?.absolute} />
<Meta property="og:description" content={openGraph.description} />
<Meta property="og:url" content={openGraph.url?.toString()} />
<Meta property="og:site_name" content={openGraph.siteName} />
<Meta property="og:locale" content={openGraph.locale} />
<Meta property="og:country_name" content={openGraph.countryName} />
<Meta property="og:ttl" content={openGraph.ttl?.toString()} />
<MultiMeta propertyPrefix="og:image" contents={openGraph.images} />
<MultiMeta propertyPrefix="og:video" contents={openGraph.videos} />
<MultiMeta propertyPrefix="og:audio" contents={openGraph.audio} />
<MultiMeta propertyPrefix="og:email" contents={openGraph.emails} />
<MultiMeta
propertyPrefix="og:phone_number"
contents={openGraph.phoneNumbers}
/>
<MultiMeta
propertyPrefix="og:fax_number"
contents={openGraph.faxNumbers}
/>
<MultiMeta
propertyPrefix="og:locale:alternate"
contents={openGraph.alternateLocale}
/>
{typedOpenGraph}
</>
)
}
import React from 'react'
export function Meta({
name,
property,
content,
}: {
name?: string
property?: string
content: string | number | URL | null | undefined
}): React.ReactElement | null {
if (typeof content !== 'undefined' && content !== null) {
return (
<meta
{...(name ? { name } : { property })}
content={typeof content === 'string' ? content : content.toString()}
/>
)
}
return null
}
export function MultiMeta({
propertyPrefix,
namePrefix,
contents,
}: {
propertyPrefix?: string
namePrefix?: string
contents:
| (
| Record<string, undefined | string | URL | number>
| string
| URL
| number
)[]
| null
| undefined
}) {
if (typeof contents === 'undefined' || contents === null) {
return null
}
const keyPrefix = propertyPrefix || namePrefix
return (
<>
{contents.map((content, index) => {
if (
typeof content === 'string' ||
typeof content === 'number' ||
content instanceof URL
) {
return (
<Meta
key={keyPrefix + '_' + index}
{...(propertyPrefix
? { property: propertyPrefix }
: { name: namePrefix })}
content={content}
/>
)
} else {
return (
<React.Fragment key={keyPrefix + '_' + index}>
{Object.entries(content).map(([k, v]) => {
return typeof v === 'undefined' ? null : (
<Meta
key={keyPrefix + ':' + k + '_' + index}
{...(propertyPrefix
? { property: propertyPrefix + ':' + k }
: { name: namePrefix + ':' + k })}
content={typeof v === 'string' ? v : v.toString()}
/>
)
})}
</React.Fragment>
)
}
})}
</>
)
}
import type {
Metadata,
ResolvedMetadata,
ResolvingMetadata,
} from './types/metadata-interface'
import type { Viewport } from './types/extra-types'
import type { ResolvedTwitterMetadata } from './types/twitter-types'
import type { AbsoluteTemplateString } from './types/metadata-types'
import { createDefaultMetadata } from './default-metadata'
import { resolveOpenGraph } from './resolve-opengraph'
import { mergeTitle } from './resolve-title'
const viewPortKeys = {
width: 'width',
height: 'height',
initialScale: 'initial-scale',
minimumScale: 'minimum-scale',
maximumScale: 'maximum-scale',
viewportFit: 'viewport-fit',
} as const
type Item =
| {
type: 'layout' | 'page'
// A number that represents which layer or routes that the item is in. Starting from 0.
// Layout and page in the same level will share the same `layer`.
layer: number
mod: () => Promise<{
metadata?: Metadata
generateMetadata?: (
props: any,
parent: ResolvingMetadata
) => Promise<Metadata>
}>
path: string
}
| {
type: 'icon'
// A number that represents which layer the item is in. Starting from 0.
layer: number
mod?: () => Promise<{
metadata?: Metadata
generateMetadata?: (
props: any,
parent: ResolvingMetadata
) => Promise<Metadata>
}>
path?: string
}
// Merge the source metadata into the resolved target metadata.
function merge(
target: ResolvedMetadata,
source: Metadata,
templateStrings: {
title: string | null
openGraph: string | null
twitter: string | null
}
) {
for (const key_ in source) {
const key = key_ as keyof Metadata
switch (key) {
case 'other': {
Object.assign(target.other, source.other)
break
}
case 'title': {
if (source.title) {
target.title = source.title as AbsoluteTemplateString
mergeTitle(target, templateStrings.title)
}
break
}
case 'openGraph': {
if (typeof source.openGraph !== 'undefined') {
target.openGraph = resolveOpenGraph(source.openGraph)
if (source.openGraph) {
mergeTitle(target.openGraph, templateStrings.openGraph)
}
} else {
target.openGraph = null
}
break
}
case 'twitter': {
if (source.twitter) {
target.twitter = source.twitter as ResolvedTwitterMetadata
mergeTitle(target.twitter, templateStrings.twitter)
} else {
target.twitter = null
}
break
}
case 'viewport': {
let content: string | null = null
const { viewport } = source
if (typeof viewport === 'string') {
content = viewport
} else if (viewport) {
content = ''
for (const viewportKey_ in viewPortKeys) {
const viewportKey = viewportKey_ as keyof Viewport
if (viewport[viewportKey]) {
if (content) content += ', '
content += `${viewPortKeys[viewportKey]}=${viewport[viewportKey]}`
}
}
}
target.viewport = content
break
}
default: {
// TODO: Make sure the type is correct.
// @ts-ignore
target[key] = source[key]
break
}
}
}
}
export async function resolveMetadata(metadataItems: Item[]) {
const resolvedMetadata = createDefaultMetadata()
let committedTitleTemplate: string | null = null
let committedOpenGraphTitleTemplate: string | null = null
let committedTwitterTitleTemplate: string | null = null
let lastLayer = 0
// from root layout to page metadata
for (let i = 0; i < metadataItems.length; i++) {
const item = metadataItems[i]
const isLayout = item.type === 'layout'
const isPage = item.type === 'page'
if (isLayout || isPage) {
let layerMod = await item.mod()
// Layer is a client component, we just skip it. It can't have metadata
// exported. Note that during our SWC transpilation, it should check if
// the exports are valid and give specific error messages.
if (
'$$typeof' in layerMod &&
(layerMod as any).$$typeof === Symbol.for('react.module.reference')
) {
continue
}
if (layerMod.metadata && layerMod.generateMetadata) {
throw new Error(
`A ${item.type} is exporting both metadata and generateMetadata which is not supported. If all of the metadata you want to associate to this ${item.type} is static use the metadata export, otherwise use generateMetadata. File: ` +
item.path
)
}
// If we resolved all items in this layer, commit the stashed titles.
if (item.layer >= lastLayer) {
committedTitleTemplate = resolvedMetadata.title?.template || null
committedOpenGraphTitleTemplate =
resolvedMetadata.openGraph?.title?.template || null
committedTwitterTitleTemplate =
resolvedMetadata.twitter?.title?.template || null
lastLayer = item.layer
}
if (layerMod.metadata) {
merge(resolvedMetadata, layerMod.metadata, {
title: committedTitleTemplate,
openGraph: committedOpenGraphTitleTemplate,
twitter: committedTwitterTitleTemplate,
})
} else if (layerMod.generateMetadata) {
merge(
resolvedMetadata,
await layerMod.generateMetadata(
// TODO: Rewrite this to pass correct params and resolving metadata value.
{},
Promise.resolve(resolvedMetadata)
),
{
title: committedTitleTemplate,
openGraph: committedOpenGraphTitleTemplate,
twitter: committedTwitterTitleTemplate,
}
)
}
}
}
return resolvedMetadata
}
// TODO: Implement this function.
export async function resolveFileBasedMetadataForLoader(
_layer: number,
_dir: string
) {
let metadataCode = ''
// const files = await fs.readdir(path.normalize(dir))
// for (const file of files) {
// // TODO: Get a full list and filter out directories.
// if (file === 'icon.svg') {
// metadataCode += `{
// type: 'icon',
// layer: ${layer},
// path: ${JSON.stringify(path.join(dir, file))},
// },`
// } else if (file === 'icon.jsx') {
// metadataCode += `{
// type: 'icon',
// layer: ${layer},
// mod: () => import(/* webpackMode: "eager" */ ${JSON.stringify(
// path.join(dir, file)
// )}),
// path: ${JSON.stringify(path.join(dir, file))},
// },`
// }
// }
return metadataCode
}
import type { Metadata } from './types/metadata-interface'
import type {
OpenGraphType,
OpenGraph,
ResolvedOpenGraph,
} from './types/opengraph-types'
const OgTypFields = {
article: ['authors', 'tags'],
song: ['albums', 'musicians'],
playlist: ['albums', 'musicians'],
radio: ['creators'],
video: ['actors', 'directors', 'writers', 'tags'],
basic: [
'emails',
'phoneNumbers',
'faxNumbers',
'alternateLocale',
'images',
'audio',
'videos',
],
} as const
function resolveAsArrayOrUndefined<T = any>(value: T): undefined | any[] {
if (typeof value === 'undefined' || value === null) {
return undefined
}
if (Array.isArray(value)) {
return value
}
return [value]
}
function getFieldsByOgType(ogType: OpenGraphType | undefined) {
switch (ogType) {
case 'article':
case 'book':
return OgTypFields.article
case 'music.song':
case 'music.album':
return OgTypFields.song
case 'music.playlist':
return OgTypFields.playlist
case 'music.radio_station':
return OgTypFields.radio
case 'video.movie':
case 'video.episode':
return OgTypFields.video
default:
return OgTypFields.basic
}
}
export function resolveOpenGraph(
openGraph: Metadata['openGraph']
): ResolvedOpenGraph {
const url = openGraph
? typeof openGraph.url === 'string'
? new URL(openGraph.url)
: openGraph.url
: undefined
// TODO: improve typing
const resolved: { [x: string]: any } = openGraph || {}
function assignProps(og: OpenGraph) {
const ogType = og && 'type' in og ? og.type : undefined
const keys = getFieldsByOgType(ogType)
for (const k of keys) {
const key = k as keyof OpenGraph
if (key in og) {
// TODO: fix typing inferring
// @ts-ignore
const value = resolveAsArrayOrUndefined(og[key])
if (value != null) {
;(resolved as any)[key] = value
}
}
}
}
if (openGraph) {
assignProps(openGraph)
}
if (url) {
resolved.url = url
}
return resolved as ResolvedOpenGraph
}
import type { Metadata } from './types/metadata-interface'
import type { AbsoluteTemplateString } from './types/metadata-types'
function resolveTitleTemplate(template: string | null, title: string) {
return template ? template.replace(/%s/g, title) : title
}
export function mergeTitle<T extends { title?: Metadata['title'] }>(
source: T,
stashedTemplate: string | null
) {
const { title } = source
let resolved
const template =
typeof source.title !== 'string' &&
source.title &&
'template' in source.title
? source.title.template
: null
if (typeof title === 'string') {
resolved = resolveTitleTemplate(stashedTemplate, title)
} else if (title) {
if ('default' in title) {
resolved = resolveTitleTemplate(stashedTemplate, title.default)
}
if ('absolute' in title && title.absolute) {
resolved = title.absolute
}
}
const target = source
if (source.title && typeof source.title !== 'string') {
const targetTitle = source.title as AbsoluteTemplateString
targetTitle.template = template
targetTitle.absolute = resolved || ''
} else {
target.title = { absolute: resolved || source.title || '', template }
}
}
// Reference: https://hreflang.org/what-is-a-valid-hreflang
type LangCode =
| 'af-ZA'
| 'am-ET'
| 'ar-AE'
| 'ar-BH'
| 'ar-DZ'
| 'ar-EG'
| 'ar-IQ'
| 'ar-JO'
| 'ar-KW'
| 'ar-LB'
| 'ar-LY'
| 'ar-MA'
| 'arn-CL'
| 'ar-OM'
| 'ar-QA'
| 'ar-SA'
| 'ar-SD'
| 'ar-SY'
| 'ar-TN'
| 'ar-YE'
| 'as-IN'
| 'az-az'
| 'az-Cyrl-AZ'
| 'az-Latn-AZ'
| 'ba-RU'
| 'be-BY'
| 'bg-BG'
| 'bn-BD'
| 'bn-IN'
| 'bo-CN'
| 'br-FR'
| 'bs-Cyrl-BA'
| 'bs-Latn-BA'
| 'ca-ES'
| 'co-FR'
| 'cs-CZ'
| 'cy-GB'
| 'da-DK'
| 'de-AT'
| 'de-CH'
| 'de-DE'
| 'de-LI'
| 'de-LU'
| 'dsb-DE'
| 'dv-MV'
| 'el-CY'
| 'el-GR'
| 'en-029'
| 'en-AU'
| 'en-BZ'
| 'en-CA'
| 'en-cb'
| 'en-GB'
| 'en-IE'
| 'en-IN'
| 'en-JM'
| 'en-MT'
| 'en-MY'
| 'en-NZ'
| 'en-PH'
| 'en-SG'
| 'en-TT'
| 'en-US'
| 'en-ZA'
| 'en-ZW'
| 'es-AR'
| 'es-BO'
| 'es-CL'
| 'es-CO'
| 'es-CR'
| 'es-DO'
| 'es-EC'
| 'es-ES'
| 'es-GT'
| 'es-HN'
| 'es-MX'
| 'es-NI'
| 'es-PA'
| 'es-PE'
| 'es-PR'
| 'es-PY'
| 'es-SV'
| 'es-US'
| 'es-UY'
| 'es-VE'
| 'et-EE'
| 'eu-ES'
| 'fa-IR'
| 'fi-FI'
| 'fil-PH'
| 'fo-FO'
| 'fr-BE'
| 'fr-CA'
| 'fr-CH'
| 'fr-FR'
| 'fr-LU'
| 'fr-MC'
| 'fy-NL'
| 'ga-IE'
| 'gd-GB'
| 'gd-ie'
| 'gl-ES'
| 'gsw-FR'
| 'gu-IN'
| 'ha-Latn-NG'
| 'he-IL'
| 'hi-IN'
| 'hr-BA'
| 'hr-HR'
| 'hsb-DE'
| 'hu-HU'
| 'hy-AM'
| 'id-ID'
| 'ig-NG'
| 'ii-CN'
| 'in-ID'
| 'is-IS'
| 'it-CH'
| 'it-IT'
| 'iu-Cans-CA'
| 'iu-Latn-CA'
| 'iw-IL'
| 'ja-JP'
| 'ka-GE'
| 'kk-KZ'
| 'kl-GL'
| 'km-KH'
| 'kn-IN'
| 'kok-IN'
| 'ko-KR'
| 'ky-KG'
| 'lb-LU'
| 'lo-LA'
| 'lt-LT'
| 'lv-LV'
| 'mi-NZ'
| 'mk-MK'
| 'ml-IN'
| 'mn-MN'
| 'mn-Mong-CN'
| 'moh-CA'
| 'mr-IN'
| 'ms-BN'
| 'ms-MY'
| 'mt-MT'
| 'nb-NO'
| 'ne-NP'
| 'nl-BE'
| 'nl-NL'
| 'nn-NO'
| 'no-no'
| 'nso-ZA'
| 'oc-FR'
| 'or-IN'
| 'pa-IN'
| 'pl-PL'
| 'prs-AF'
| 'ps-AF'
| 'pt-BR'
| 'pt-PT'
| 'qut-GT'
| 'quz-BO'
| 'quz-EC'
| 'quz-PE'
| 'rm-CH'
| 'ro-mo'
| 'ro-RO'
| 'ru-mo'
| 'ru-RU'
| 'rw-RW'
| 'sah-RU'
| 'sa-IN'
| 'se-FI'
| 'se-NO'
| 'se-SE'
| 'si-LK'
| 'sk-SK'
| 'sl-SI'
| 'sma-NO'
| 'sma-SE'
| 'smj-NO'
| 'smj-SE'
| 'smn-FI'
| 'sms-FI'
| 'sq-AL'
| 'sr-BA'
| 'sr-CS'
| 'sr-Cyrl-BA'
| 'sr-Cyrl-CS'
| 'sr-Cyrl-ME'
| 'sr-Cyrl-RS'
| 'sr-Latn-BA'
| 'sr-Latn-CS'
| 'sr-Latn-ME'
| 'sr-Latn-RS'
| 'sr-ME'
| 'sr-RS'
| 'sr-sp'
| 'sv-FI'
| 'sv-SE'
| 'sw-KE'
| 'syr-SY'
| 'ta-IN'
| 'te-IN'
| 'tg-Cyrl-TJ'
| 'th-TH'
| 'tk-TM'
| 'tlh-QS'
| 'tn-ZA'
| 'tr-TR'
| 'tt-RU'
| 'tzm-Latn-DZ'
| 'ug-CN'
| 'uk-UA'
| 'ur-PK'
| 'uz-Cyrl-UZ'
| 'uz-Latn-UZ'
| 'uz-uz'
| 'vi-VN'
| 'wo-SN'
| 'xh-ZA'
| 'yo-NG'
| 'zh-CN'
| 'zh-HK'
| 'zh-MO'
| 'zh-SG'
| 'zh-TW'
| 'zu-ZA'
type UnmatchedLang = 'x-default'
type HrefLang = LangCode | UnmatchedLang
type Languages<T> = {
[s in HrefLang]?: T
}
export type AlternateURLs = {
canonical?: null | string | URL
languages?: Languages<null | string | URL>
media?: {
[media: string]: null | string | URL
}
types?: {
[types: string]: null | string | URL
}
}
export type ResolvedAlternateURLs = {
canonical: null | URL
languages: Languages<null | URL>
media?: {
[media: string]: null | URL
}
types?: {
[types: string]: null | URL
}
}
// When rendering applink meta tags add a namespace tag before each array instance
// if more than one member exists.
// ref: https://developers.facebook.com/docs/applinks/metadata-reference
export type AppLinks = {
ios?: AppLinksApple | Array<AppLinksApple>
iphone?: AppLinksApple | Array<AppLinksApple>
ipad?: AppLinksApple | Array<AppLinksApple>
android?: AppLinksAndroid | Array<AppLinksAndroid>
windows_phone?: AppLinksWindows | Array<AppLinksWindows>
windows?: AppLinksWindows | Array<AppLinksWindows>
windows_universal?: AppLinksWindows | Array<AppLinksWindows>
web?: AppLinksWeb | Array<AppLinksWeb>
}
export type AppLinksApple = {
url: string | URL
app_store_id?: string | number
app_name?: string
}
export type AppLinksAndroid = {
package: string
url?: string | URL
class?: string
app_name?: string
}
export type AppLinksWindows = {
url: string | URL
app_id?: string
app_name?: string
}
export type AppLinksWeb = {
url: string | URL
should_fallback?: boolean
}
// Apple Itunes APp
// https://developer.apple.com/documentation/webkit/promoting_apps_with_smart_app_banners
export type ItunesApp = {
appId: string
appArgument?: string
}
// Viewport meta structure
// https://developer.mozilla.org/en-US/docs/Web/HTML/Viewport_meta_tag
// intentionally leaving out user-scalable, use a string if you want that behavior
export type Viewport = {
width?: string | number
height?: string | number
initialScale?: number
minimumScale?: number
maximumScale?: number
}
// Apple Web App
// https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariHTMLRef/Articles/MetaTags.html
// https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariWebContent/ConfiguringWebApplications/ConfiguringWebApplications.html
export type AppleWebApp = {
// default true
capable?: boolean
title?: string
startupImage?: AppleImage | Array<AppleImage>
// default "default"
statusBarStyle?: 'default' | 'black' | 'black-translucent'
}
export type AppleImage = string | AppleImageDescriptor
export type AppleImageDescriptor = {
url: string
media?: string
}
// Format Detection
// This is a poorly specified metadata export type that is supposed to
// control whether the device attempts to conver text that matches
// certain formats into links for action. The most supported example
// is how mobile devices detect phone numbers and make them into links
// that can initiate a phone call
// https://www.goodemailcode.com/email-code/template.html
export type FormatDetection = {
telephone?: boolean
date?: boolean
address?: boolean
email?: boolean
url?: boolean
}
export type Manifest = {
// fill this out
}
import type {
AlternateURLs,
ResolvedAlternateURLs,
} from './alternative-urls-types'
import type {
AppleWebApp,
AppLinks,
FormatDetection,
ItunesApp,
Viewport,
} from './extra-types'
import type {
AbsoluteTemplateString,
Author,
ColorSchemeEnum,
Icon,
Icons,
ReferrerEnum,
Robots,
TemplateString,
Verification,
} from './metadata-types'
import type { OpenGraph, ResolvedOpenGraph } from './opengraph-types'
import { ResolvedTwitterMetadata, Twitter } from './twitter-types'
export interface Metadata {
// origin and base path for absolute urls for various metadata links such as
// opengraph-image
metadataBase: null | URL
// The Document title
title?: null | string | TemplateString
// The Document description, and optionally the opengraph and twitter descriptions
description?: null | string
// Standard metadata names
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta/name
applicationName?: null | string | Array<string>
authors?: null | Author | Array<Author>
generator?: null | string
// if you provide an array it will be flattened into a single tag with comma separation
keywords?: null | string | Array<string>
referrer?: null | ReferrerEnum
themeColor?: null | string
colorScheme?: null | ColorSchemeEnum
viewport?: null | string | Viewport
creator?: null | string
publisher?: null | string
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta/name#other_metadata_names
robots?: null | string | Robots
// The canonical and alternate URLs for this location
alternates: AlternateURLs
// Defaults to rel="icon" but the Icons type can be used
// to get more specific about rel types
icons?: null | Array<Icon> | Icons
openGraph?: null | OpenGraph
twitter?: null | Twitter
// common verification tokens
verification?: Verification
// Apple web app metadata
// https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariHTMLRef/Articles/MetaTags.html
appleWebApp?: null | boolean | AppleWebApp
// Should devices try to interpret various formats and make actionable links
// out of them? The canonical example is telephone numbers on mobile that can
// be clicked to dial
formatDetection?: null | FormatDetection
// meta name="apple-itunes-app"
itunes?: null | ItunesApp
// meta name="abstract"
// A brief description of what this web-page is about.
// Not recommended, superceded by description.
// https://www.metatags.org/all-meta-tags-overview/meta-name-abstract/
abstract?: null | string
// Facebook AppLinks
appLinks?: null | AppLinks
// link rel properties
archives?: null | string | Array<string>
assets?: null | string | Array<string>
bookmarks?: null | string | Array<string> // This is technically against HTML spec but is used in wild
// meta name properties
category?: null | string
classification?: null | string
// Arbitrary name/value pairs
other?: {
[name: string]: string | number | Array<string | number>
}
/**
* Deprecated options that have a preferred method
* */
// Use appWebApp to configure apple-mobile-web-app-capable which provides
// https://www.appsloveworld.com/coding/iphone/11/difference-between-apple-mobile-web-app-capable-and-apple-touch-fullscreen-ipho
'apple-touch-fullscreen'?: never
// Obsolete since iOS 7. use icons.apple or "app-touch-icon" instead
// https://web.dev/apple-touch-icon/
'apple-touch-icon-precomposed'?: never
}
export interface ResolvedMetadata {
// origin and base path for absolute urls for various metadata links such as
// opengraph-image
metadataBase: null | URL
// The Document title and template if defined
title: null | AbsoluteTemplateString
// The Document description, and optionally the opengraph and twitter descriptions
description: null | string
// Standard metadata names
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta/name
applicationName: null | string
authors: null | Array<Author>
generator: null | string
// if you provide an array it will be flattened into a single tag with comma separation
keywords: null | Array<string>
referrer: null | ReferrerEnum
themeColor: null | string
colorScheme: null | ColorSchemeEnum
viewport: null | string
creator: null | string
publisher: null | string
// https://developer.mozilla.org/en-US/docs/Web/HTML/Element/meta/name#other_metadata_names
robots: null | string
// The canonical and alternate URLs for this location
alternates: ResolvedAlternateURLs
// Defaults to rel="icon" but the Icons type can be used
// to get more specific about rel types
icons: null | Icons
openGraph: null | ResolvedOpenGraph
twitter: null | ResolvedTwitterMetadata
// common verification tokens
verification: Verification
// Apple web app metadata
// https://developer.apple.com/library/archive/documentation/AppleApplications/Reference/SafariHTMLRef/Articles/MetaTags.html
appleWebApp: null | AppleWebApp
// Should devices try to interpret various formats and make actionable links
// out of them? The canonical example is telephone numbers on mobile that can
// be clicked to dial
formatDetection: null | FormatDetection
// meta name="apple-itunes-app"
itunes: null | ItunesApp
// meta name="abstract"
// A brief description of what this web-page is about.
// Not recommended, superceded by description.
// https://www.metatags.org/all-meta-tags-overview/meta-name-abstract/
abstract: null | string
// Facebook AppLinks
appLinks: null | AppLinks
// link rel properties
archives: null | Array<string>
assets: null | Array<string>
bookmarks: null | Array<string> // This is technically against HTML spec but is used in wild
// meta name properties
category: null | string
classification: null | string
// Arbitrary name/value pairs
other: {
[name: string]: string | number | Array<string | number>
}
/**
* Deprecated options that have a preferred method
* */
// Use appWebApp to configure apple-mobile-web-app-capable which provides
// https://www.appsloveworld.com/coding/iphone/11/difference-between-apple-mobile-web-app-capable-and-apple-touch-fullscreen-ipho
'apple-touch-fullscreen'?: never
// Obsolete since iOS 7. use icons.apple or "app-touch-icon" instead
// https://web.dev/apple-touch-icon/
'apple-touch-icon-precomposed'?: never
}
export type ResolvingMetadata = Promise<ResolvedMetadata>
/**
*
* Metadata types
*
*/
export type TemplateString =
| DefaultTemplateString
| AbsoluteTemplateString
| AbsoluteString
export type DefaultTemplateString = {
default: string
template: string
}
export type AbsoluteTemplateString = {
absolute: string
template: string | null
}
export type AbsoluteString = {
absolute: string
}
export type Author = {
// renders as <link rel="author"...
url?: string | URL
// renders as <meta name="author"...
name?: string
}
// does not include "unsafe-URL". to use this users should
// use '"unsafe-URL" as ReferrerEnum'
export type ReferrerEnum =
| 'no-referrer'
| 'origin'
| 'no-referrer-when-downgrade'
| 'origin-when-cross-origin'
| 'same-origin'
| 'strict-origin'
| 'strict-origin-when-cross-origin'
export type ColorSchemeEnum =
| 'normal'
| 'light'
| 'dark'
| 'light dark'
| 'dark light'
| 'only light'
export type Robots = {
// all and none will be inferred from index/follow boolean options
index?: boolean
follow?: boolean
/** @deprecated set index to false instead */
noindex?: never
/** @deprecated set follow to false instead */
nofollow?: never
noarchive?: boolean
nosnippet?: boolean
noimageindex?: boolean
nocache?: boolean
// if you want to specify an alternate robots just for google
googleBot?: string | Robots
}
export type Icon = string | IconDescriptor | URL
export type IconDescriptor = {
url: string | URL
type?: string
sizes?: string
// defaults to rel="icon" unless superceded by Icons map
rel?: string
}
export type Icons = {
// rel="icon"
icon?: Icon | Array<Icon>
// rel="shortcut icon"
shortcut?: Icon | Array<Icon>
// rel="apple-touch-icon"
apple?: Icon | Array<Icon>
// rel inferred from descriptor, defaults to "icon"
other?: Icon | Array<Icon>
}
export type Verification = {
google?: null | string | number | Array<string | number>
yahoo?: null | string | number | Array<string | number>
// if you ad-hoc additional verification
other?: {
[name: string]: string | number | Array<string | number>
}
}
import type { AbsoluteTemplateString, TemplateString } from './metadata-types'
export type OpenGraphType =
| 'article'
| 'book'
| 'music.song'
| 'music.album'
| 'music.playlist'
| 'music.radio_station'
| 'profile'
| 'website'
| 'video.tv_show'
| 'video.other'
| 'video.movie'
| 'video.episode'
export type OpenGraph =
| OpenGraphWebsite
| OpenGraphArticle
| OpenGraphBook
| OpenGraphProfile
| OpenGraphMusicSong
| OpenGraphMusicAlbum
| OpenGraphMusicPlaylist
| OpenGraphRadioStation
| OpenGraphVideoMovie
| OpenGraphVideoEpisode
| OpenGraphVideoTVShow
| OpenGraphVideoOther
| OpenGraphMetadata
// update this type to reflect actual locales
type Locale = string
type OpenGraphMetadata = {
determiner?: 'a' | 'an' | 'the' | 'auto' | ''
title?: TemplateString
description?: string
emails?: string | Array<string>
phoneNumbers?: string | Array<string>
faxNumbers?: string | Array<string>
siteName?: string
locale?: Locale
alternateLocale?: Locale | Array<Locale>
images?: OGImage | Array<OGImage>
audio?: OGAudio | Array<OGAudio>
videos?: OGVideo | Array<OGVideo>
url?: string | URL
countryName?: string
ttl?: number
}
type OpenGraphWebsite = OpenGraphMetadata & {
type: 'website'
}
type OpenGraphArticle = OpenGraphMetadata & {
type: 'article'
publishedTime?: string // datetime
modifiedTime?: string // datetime
expirationTime?: string // datetime
authors?: null | string | URL | Array<string | URL>
section?: null | string
tags?: null | string | Array<string>
}
type OpenGraphBook = OpenGraphMetadata & {
type: 'book'
isbn?: null | string
releaseDate?: null | string // datetime
authors?: null | string | URL | Array<string | URL>
tags?: null | string | Array<string>
}
type OpenGraphProfile = OpenGraphMetadata & {
type: 'profile'
firstName?: null | string
lastName?: null | string
username?: null | string
gender?: null | string
}
type OpenGraphMusicSong = OpenGraphMetadata & {
type: 'music.song'
duration?: null | number
albums?: null | string | URL | OGAlbum | Array<string | URL | OGAlbum>
musicians?: null | string | URL | Array<string | URL>
}
type OpenGraphMusicAlbum = OpenGraphMetadata & {
type: 'music.album'
songs?: null | string | URL | OGSong | Array<string | URL | OGSong>
musicians?: null | string | URL | Array<string | URL>
releaseDate?: null | string // datetime
}
type OpenGraphMusicPlaylist = OpenGraphMetadata & {
type: 'music.playlist'
songs?: null | string | URL | OGSong | Array<string | URL | OGSong>
creators?: null | string | URL | Array<string | URL>
}
type OpenGraphRadioStation = OpenGraphMetadata & {
type: 'music.radio_station'
creators?: null | string | URL | Array<string | URL>
}
type OpenGraphVideoMovie = OpenGraphMetadata & {
type: 'video.movie'
actors?: null | string | URL | OGActor | Array<string | URL | OGActor>
directors?: null | string | URL | Array<string | URL>
writers?: null | string | URL | Array<string | URL>
duration?: null | number
releaseDate?: null | string // datetime
tags?: null | string | Array<string>
}
type OpenGraphVideoEpisode = OpenGraphMetadata & {
type: 'video.episode'
actors?: null | string | URL | OGActor | Array<string | URL | OGActor>
directors?: null | string | URL | Array<string | URL>
writers?: null | string | URL | Array<string | URL>
duration?: null | number
releaseDate?: null | string // datetime
tags?: null | string | Array<string>
series?: null | string | URL
}
type OpenGraphVideoTVShow = OpenGraphMetadata & {
type: 'video.tv_show'
}
type OpenGraphVideoOther = OpenGraphMetadata & {
type: 'video.other'
}
type OGImage = string | OGImageDescriptor | URL
type OGImageDescriptor = {
url: string | URL
secureUrl?: string | URL
alt?: string
type?: string
width?: string | number
height?: string | number
}
type OGAudio = string | OGAudioDescriptor | URL
type OGAudioDescriptor = {
url: string | URL
secure_url?: string | URL
type?: string
}
type OGVideo = string | OGVideoDescriptor | URL
type OGVideoDescriptor = {
url: string | URL
secureUrl?: string | URL
type?: string
width?: string | number
height?: string | number
}
export type ResolvedOpenGraph =
| ResolvedOpenGraphWebsite
| ResolvedOpenGraphArticle
| ResolvedOpenGraphBook
| ResolvedOpenGraphProfile
| ResolvedOpenGraphMusicSong
| ResolvedOpenGraphMusicAlbum
| ResolvedOpenGraphMusicPlaylist
| ResolvedOpenGraphRadioStation
| ResolvedOpenGraphVideoMovie
| ResolvedOpenGraphVideoEpisode
| ResolvedOpenGraphVideoTVShow
| ResolvedOpenGraphVideoOther
| ResolvedOpenGraphMetadata
type ResolvedOpenGraphMetadata = {
determiner?: 'a' | 'an' | 'the' | 'auto' | ''
title?: AbsoluteTemplateString
description?: string
emails?: Array<string>
phoneNumbers?: Array<string>
faxNumbers?: Array<string>
siteName?: string
locale?: Locale
alternateLocale?: Array<Locale>
images?: Array<OGImage>
audio?: Array<OGAudio>
videos?: Array<OGVideo>
url?: URL
countryName?: string
ttl?: number
}
type ResolvedOpenGraphWebsite = ResolvedOpenGraphMetadata & {
type: 'website'
}
type ResolvedOpenGraphArticle = ResolvedOpenGraphMetadata & {
type: 'article'
publishedTime?: string // datetime
modifiedTime?: string // datetime
expirationTime?: string // datetime
authors?: Array<string>
section?: string
tags?: Array<string>
}
type ResolvedOpenGraphBook = ResolvedOpenGraphMetadata & {
type: 'book'
isbn?: string
releaseDate?: string // datetime
authors?: Array<string>
tags?: Array<string>
}
type ResolvedOpenGraphProfile = ResolvedOpenGraphMetadata & {
type: 'profile'
firstName?: string
lastName?: string
username?: string
gender?: string
}
type ResolvedOpenGraphMusicSong = ResolvedOpenGraphMetadata & {
type: 'music.song'
duration?: number
albums?: Array<OGAlbum>
musicians?: Array<string | URL>
}
type ResolvedOpenGraphMusicAlbum = ResolvedOpenGraphMetadata & {
type: 'music.album'
songs?: Array<string | URL | OGSong>
musicians?: Array<string | URL>
releaseDate?: string // datetime
}
type ResolvedOpenGraphMusicPlaylist = ResolvedOpenGraphMetadata & {
type: 'music.playlist'
songs?: Array<string | URL | OGSong>
creators?: Array<string | URL>
}
type ResolvedOpenGraphRadioStation = ResolvedOpenGraphMetadata & {
type: 'music.radio_station'
creators?: Array<string | URL>
}
type ResolvedOpenGraphVideoMovie = ResolvedOpenGraphMetadata & {
type: 'video.movie'
actors?: Array<string | URL | OGActor>
directors?: Array<string | URL>
writers?: Array<string | URL>
duration?: number
releaseDate?: string // datetime
tags?: Array<string>
}
type ResolvedOpenGraphVideoEpisode = ResolvedOpenGraphMetadata & {
type: 'video.episode'
actors?: Array<string | URL | OGActor>
directors?: Array<string | URL>
writers?: Array<string | URL>
duration?: number
releaseDate?: string // datetime
tags?: Array<string>
series?: string | URL
}
type ResolvedOpenGraphVideoTVShow = ResolvedOpenGraphMetadata & {
type: 'video.tv_show'
}
type ResolvedOpenGraphVideoOther = ResolvedOpenGraphMetadata & {
type: 'video.other'
}
type OGSong = {
url: string | URL
disc?: number
track?: number
}
type OGAlbum = {
url: string | URL
disc?: number
track?: number
}
type OGActor = {
url: string | URL
role?: string
}
import type { AbsoluteTemplateString, TemplateString } from './metadata-types'
export type Twitter =
| TwitterSummary
| TwitterSummaryLargeImage
| TwitterPlayer
| TwitterApp
| TwitterMetadata
type TwitterMetadata = {
// defaults to card="summary"
site?: string // username for account associated to the site itself
siteId?: string // id for account associated to the site itself
creator?: string // username for the account associated to the creator of the content on the site
creatorId?: string // id for the account associated to the creator of the content on the site
title?: string | TemplateString
description?: string
images?: TwitterImage | Array<TwitterImage>
}
type TwitterSummary = TwitterMetadata & {
card: 'summary'
}
type TwitterSummaryLargeImage = TwitterMetadata & {
card: 'summary_large_image'
}
type TwitterPlayer = TwitterMetadata & {
card: 'player'
players: TwitterPlayerDescriptor | Array<TwitterPlayerDescriptor>
}
type TwitterApp = TwitterMetadata & {
card: 'app'
app: TwitterAppDescriptor
}
type TwitterAppDescriptor = {
id: {
iphone?: string | number
ipad?: string | number
googleplay?: string
}
url?: {
iphone?: string | URL
ipad?: string | URL
googleplay?: string | URL
}
country?: string
}
type TwitterImage = string | TwitterImageDescriptor | URL
type TwitterImageDescriptor = {
url: string | URL
secureUrl?: string | URL
alt?: string
type?: string
width?: string | number
height?: string | number
}
type TwitterPlayerDescriptor = {
playerUrl: string | URL
streamUrl: string | URL
width: number
height: number
}
export type ResolvedTwitterMetadata = Omit<TwitterMetadata, 'title'> & {
title: AbsoluteTemplateString | null
}
import React from 'react'
import type { ResolvedMetadata } from './types/metadata-interface'
import { ResolvedBasicMetadata } from './generate/basic'
import { ResolvedAlternatesMetadata } from './generate/alternate'
import { ResolvedOpenGraphMetadata } from './generate/opengraph'
import { resolveMetadata } from './resolve-metadata'
// Generate the actual React elements from the resolved metadata.
export async function Metadata({ metadata }: { metadata: any }) {
const resolved: ResolvedMetadata = await resolveMetadata(metadata)
return (
<>
<ResolvedBasicMetadata metadata={resolved} />
<ResolvedAlternatesMetadata metadata={resolved} />
<ResolvedOpenGraphMetadata openGraph={resolved.openGraph} />
</>
)
}
...@@ -45,8 +45,8 @@ import { ...@@ -45,8 +45,8 @@ import {
RSC, RSC,
} from '../client/components/app-router-headers' } from '../client/components/app-router-headers'
import type { StaticGenerationAsyncStorage } from '../client/components/static-generation-async-storage' import type { StaticGenerationAsyncStorage } from '../client/components/static-generation-async-storage'
import { DefaultHead } from '../client/components/head'
import { formatServerError } from '../lib/format-server-error' import { formatServerError } from '../lib/format-server-error'
import { Metadata } from '../lib/metadata/ui'
import type { RequestAsyncStorage } from '../client/components/request-async-storage' import type { RequestAsyncStorage } from '../client/components/request-async-storage'
import { runWithRequestAsyncStorage } from './run-with-request-async-storage' import { runWithRequestAsyncStorage } from './run-with-request-async-storage'
import { runWithStaticGenerationAsyncStorage } from './run-with-static-generation-async-storage' import { runWithStaticGenerationAsyncStorage } from './run-with-static-generation-async-storage'
...@@ -949,6 +949,12 @@ export async function renderToHTMLOrFlight( ...@@ -949,6 +949,12 @@ export async function renderToHTMLOrFlight(
*/ */
const loaderTree: LoaderTree = ComponentMod.tree const loaderTree: LoaderTree = ComponentMod.tree
/**
* The metadata items array created in next-app-loader with all relevant information
* that we need to resolve the final metadata.
*/
const metadataItems = ComponentMod.metadata
stripInternalQueries(query) stripInternalQueries(query)
const LayoutRouter = const LayoutRouter =
...@@ -1032,8 +1038,7 @@ export async function renderToHTMLOrFlight( ...@@ -1032,8 +1038,7 @@ export async function renderToHTMLOrFlight(
async function resolveHead( async function resolveHead(
[segment, parallelRoutes, { head }]: LoaderTree, [segment, parallelRoutes, { head }]: LoaderTree,
parentParams: { [key: string]: any }, parentParams: { [key: string]: any }
isRootHead: boolean
): Promise<React.ReactNode> { ): Promise<React.ReactNode> {
// Handle dynamic segment params. // Handle dynamic segment params.
const segmentParam = getDynamicParamFromSegment(segment) const segmentParam = getDynamicParamFromSegment(segment)
...@@ -1051,7 +1056,7 @@ export async function renderToHTMLOrFlight( ...@@ -1051,7 +1056,7 @@ export async function renderToHTMLOrFlight(
parentParams parentParams
for (const key in parallelRoutes) { for (const key in parallelRoutes) {
const childTree = parallelRoutes[key] const childTree = parallelRoutes[key]
const returnedHead = await resolveHead(childTree, currentParams, false) const returnedHead = await resolveHead(childTree, currentParams)
if (returnedHead) { if (returnedHead) {
return returnedHead return returnedHead
} }
...@@ -1060,8 +1065,6 @@ export async function renderToHTMLOrFlight( ...@@ -1060,8 +1065,6 @@ export async function renderToHTMLOrFlight(
if (head) { if (head) {
const Head = await interopDefault(await head[0]()) const Head = await interopDefault(await head[0]())
return <Head params={currentParams} /> return <Head params={currentParams} />
} else if (isRootHead) {
return <DefaultHead />
} }
return null return null
...@@ -1664,7 +1667,7 @@ export async function renderToHTMLOrFlight( ...@@ -1664,7 +1667,7 @@ export async function renderToHTMLOrFlight(
return [actualSegment] return [actualSegment]
} }
const rscPayloadHead = await resolveHead(loaderTree, {}, true) const rscPayloadHead = await resolveHead(loaderTree, {})
// Flight data that is going to be passed to the browser. // Flight data that is going to be passed to the browser.
// Currently a single item array but in the future multiple patches might be combined in a single request. // Currently a single item array but in the future multiple patches might be combined in a single request.
const flightData: FlightData = [ const flightData: FlightData = [
...@@ -1675,7 +1678,13 @@ export async function renderToHTMLOrFlight( ...@@ -1675,7 +1678,13 @@ export async function renderToHTMLOrFlight(
parentParams: {}, parentParams: {},
flightRouterState: providedFlightRouterState, flightRouterState: providedFlightRouterState,
isFirst: true, isFirst: true,
rscPayloadHead, rscPayloadHead: (
<>
{/* @ts-expect-error allow to use async server component */}
<Metadata metadata={metadataItems} />
{rscPayloadHead}
</>
),
injectedCSS: new Set(), injectedCSS: new Set(),
rootLayoutIncluded: false, rootLayoutIncluded: false,
}) })
...@@ -1742,7 +1751,7 @@ export async function renderToHTMLOrFlight( ...@@ -1742,7 +1751,7 @@ export async function renderToHTMLOrFlight(
} }
: {} : {}
const initialHead = await resolveHead(loaderTree, {}, true) const initialHead = await resolveHead(loaderTree, {})
/** /**
* A new React Component that renders the provided React Component * A new React Component that renders the provided React Component
...@@ -1759,18 +1768,27 @@ export async function renderToHTMLOrFlight( ...@@ -1759,18 +1768,27 @@ export async function renderToHTMLOrFlight(
injectedCSS: new Set(), injectedCSS: new Set(),
rootLayoutIncluded: false, rootLayoutIncluded: false,
}) })
const initialTree = createFlightRouterStateFromLoaderTree(loaderTree) const initialTree = createFlightRouterStateFromLoaderTree(loaderTree)
return ( return (
<>
<AppRouter <AppRouter
assetPrefix={assetPrefix} assetPrefix={assetPrefix}
initialCanonicalUrl={initialCanonicalUrl} initialCanonicalUrl={initialCanonicalUrl}
initialTree={initialTree} initialTree={initialTree}
initialHead={initialHead} initialHead={
<>
{/* @ts-expect-error allow to use async server component */}
<Metadata metadata={metadataItems} />
{initialHead}
</>
}
globalErrorComponent={GlobalError} globalErrorComponent={GlobalError}
> >
<ComponentTree /> <ComponentTree />
</AppRouter> </AppRouter>
</>
) )
}, },
ComponentMod, ComponentMod,
......
...@@ -85,7 +85,7 @@ createNextDescribe( ...@@ -85,7 +85,7 @@ createNextDescribe(
it('should pass props from getServerSideProps in root layout', async () => { it('should pass props from getServerSideProps in root layout', async () => {
const $ = await next.render$('/dashboard') const $ = await next.render$('/dashboard')
expect($('title').text()).toBe('hello world') expect($('title').first().text()).toBe('hello world')
}) })
it('should serve from pages', async () => { it('should serve from pages', async () => {
......
export default function Page() {
return <p>hello</p>
}
export const metadata = {
alternates: {
canonical: 'https://example.com',
languages: {
'en-US': 'https://example.com/en-US',
'de-DE': 'https://example.com/de-DE',
},
media: {
'only screen and (max-width: 600px)': 'https://example.com/mobile',
},
types: {
'application/rss+xml': 'https://example.com/rss',
},
},
}
import Link from 'next/link'
export default function Page() {
return (
<div id="basic">
<Link id="to-index" href="/">
to index
</Link>
</div>
)
}
export const metadata = {
generator: 'next.js',
applicationName: 'test',
referrer: 'origin-when-crossorigin',
keywords: ['next.js', 'react', 'javascript'],
authors: ['John Doe', 'Jane Doe'],
themeColor: 'cyan',
colorScheme: 'dark',
viewport: 'width=device-width, initial-scale=1, shrink-to-fit=no',
creator: 'shu',
publisher: 'vercel',
robots: 'index, follow',
}
export default function Layout({ children }) {
return (
<html>
<head></head>
<body>{children}</body>
</html>
)
}
export const metadata = {
title: 'this is the layout title',
description: 'this is the layout description',
}
export default function Page() {
return <p>hello</p>
}
export const metadata = {
openGraph: {
title: 'My custom title',
description: 'My custom description',
type: 'article',
publishedTime: '2023-01-01T00:00:00.000Z',
authors: ['author1', 'author2', 'author3'],
},
}
export default function Page() {
return <p>hello</p>
}
export const metadata = {
openGraph: {
title: 'My custom title',
description: 'My custom description',
url: 'https://example.com',
siteName: 'My custom site name',
images: [
{
url: 'https://example.com/image.png',
width: 800,
height: 600,
},
{
url: 'https://example.com/image2.png',
width: 1800,
height: 1600,
alt: 'My custom alt',
},
],
locale: 'en-US',
type: 'website',
},
}
import Link from 'next/link'
export default function Page() {
return (
<>
<p id="index">index page</p>
<Link href="/basic" id="to-basic">
to /basic
</Link>
<br />
<Link href="/title" id="to-title">
to /title
</Link>
<br />
</>
)
}
export const metadata = {
title: 'index page',
}
export default function Page() {
return <p>hello</p>
}
export const metadata = {
title: 'Inner Page',
}
export default function Layout(props) {
return props.children
}
export const metadata = {
title: {
template: '%s | Extra Layout',
},
}
export default function Page() {
return <p>hello</p>
}
export const metadata = {
title: 'Extra Page',
}
export default function Layout(props) {
return props.children
}
export const metadata = {
title: {
template: '%s | Layout',
},
}
export default function Page() {
return <p>hello</p>
}
export const metadata = {
title: 'Page',
}
import Link from 'next/link'
export default function Page() {
return (
<div id="title">
<Link id="to-index" href="/">
to index
</Link>
</div>
)
}
export const metadata = {
title: 'this is the page title',
}
export default function Page() {
return <p>viewport</p>
}
export const metadata = {
viewport: {
width: 'device-width',
initialScale: 1,
maximumScale: 1,
},
}
import { createNextDescribe } from 'e2e-utils'
createNextDescribe(
'app dir - metadata',
{
files: __dirname,
},
({ next, isNextDeploy }) => {
describe('metadata', () => {
if (isNextDeploy) {
it('should skip for deploy currently', () => {})
return
}
async function checkMeta(
browser,
name,
content,
property = 'property',
tag = 'meta',
field = 'content'
) {
const values = await browser.eval(
`[...document.querySelectorAll('${tag}[${property}="${name}"]')].map((el) => el.${field})`
)
if (Array.isArray(content)) {
expect(values).toEqual(content)
} else {
console.log('expect', values[0], 'toContain', content)
expect(values[0]).toContain(content)
}
}
describe('basic', () => {
it('should support title and description', async () => {
const browser = await next.browser('/title')
expect(await browser.eval(`document.title`)).toBe(
'this is the page title'
)
await checkMeta(
browser,
'description',
'this is the layout description',
'name'
)
})
it('should support title template', async () => {
const browser = await next.browser('/title-template')
expect(await browser.eval(`document.title`)).toBe('Page | Layout')
})
it('should support stashed title in one layer of page and layout', async () => {
const browser = await next.browser('/title-template/extra')
expect(await browser.eval(`document.title`)).toBe(
'Extra Page | Extra Layout'
)
})
it('should support stashed title in two layers of page and layout', async () => {
const browser = await next.browser('/title-template/extra/inner')
expect(await browser.eval(`document.title`)).toBe(
'Inner Page | Extra Layout'
)
})
it('should support other basic tags', async () => {
const browser = await next.browser('/basic')
await checkMeta(browser, 'generator', 'next.js', 'name')
await checkMeta(browser, 'application-name', 'test', 'name')
await checkMeta(
browser,
'referrer',
'origin-when-crossorigin',
'name'
)
await checkMeta(
browser,
'keywords',
'next.js,react,javascript',
'name'
)
await checkMeta(browser, 'author', 'John Doe,Jane Doe', 'name')
await checkMeta(browser, 'theme-color', 'cyan', 'name')
await checkMeta(browser, 'color-scheme', 'dark', 'name')
await checkMeta(
browser,
'viewport',
'width=device-width, initial-scale=1, shrink-to-fit=no',
'name'
)
await checkMeta(browser, 'creator', 'shu', 'name')
await checkMeta(browser, 'publisher', 'vercel', 'name')
await checkMeta(browser, 'robots', 'index, follow', 'name')
})
it('should support object viewport', async () => {
const browser = await next.browser('/viewport/object')
await checkMeta(
browser,
'viewport',
'width=device-width, initial-scale=1, maximum-scale=1',
'name'
)
})
it('should support alternate tags', async () => {
const browser = await next.browser('/alternate')
await checkMeta(
browser,
'canonical',
'https://example.com',
'rel',
'link',
'href'
)
await checkMeta(
browser,
'en-US',
'https://example.com/en-US',
'hreflang',
'link',
'href'
)
await checkMeta(
browser,
'de-DE',
'https://example.com/de-DE',
'hreflang',
'link',
'href'
)
await checkMeta(
browser,
'only screen and (max-width: 600px)',
'https://example.com/mobile',
'media',
'link',
'href'
)
await checkMeta(
browser,
'application/rss+xml',
'https://example.com/rss',
'type',
'link',
'href'
)
})
it('should apply metadata when navigating client-side', async () => {
const browser = await next.browser('/')
const getTitle = () => browser.elementByCss('title').text()
expect(await getTitle()).toBe('index page')
await browser
.elementByCss('#to-basic')
.click()
.waitForElementByCss('#basic', 2000)
await checkMeta(
browser,
'referrer',
'origin-when-crossorigin',
'name'
)
await browser.back().waitForElementByCss('#index', 2000)
expect(await getTitle()).toBe('index page')
await browser
.elementByCss('#to-title')
.click()
.waitForElementByCss('#title', 2000)
expect(await getTitle()).toBe('this is the page title')
})
})
describe('opengraph', () => {
it('should support opengraph tags', async () => {
const browser = await next.browser('/opengraph')
await checkMeta(browser, 'og:title', 'My custom title')
await checkMeta(browser, 'og:description', 'My custom description')
await checkMeta(browser, 'og:url', 'https://example.com')
await checkMeta(browser, 'og:site_name', 'My custom site name')
await checkMeta(browser, 'og:locale', 'en-US')
await checkMeta(browser, 'og:type', 'website')
await checkMeta(browser, 'og:image:url', [
'https://example.com/image.png',
'https://example.com/image2.png',
])
await checkMeta(browser, 'og:image:width', ['800', '1800'])
await checkMeta(browser, 'og:image:height', ['600', '1600'])
await checkMeta(browser, 'og:image:alt', 'My custom alt')
})
it('should support opengraph with article type', async () => {
const browser = await next.browser('/opengraph/article')
await checkMeta(browser, 'og:title', 'My custom title')
await checkMeta(browser, 'og:description', 'My custom description')
await checkMeta(browser, 'og:type', 'article')
await checkMeta(
browser,
'article:published_time',
'2023-01-01T00:00:00.000Z'
)
await checkMeta(browser, 'article:author', [
'author1',
'author2',
'author3',
])
})
})
})
}
)
module.exports = {
experimental: { appDir: true },
}
...@@ -103,9 +103,11 @@ describe('app dir - rsc basics', () => { ...@@ -103,9 +103,11 @@ describe('app dir - rsc basics', () => {
// should have only 1 DOCTYPE // should have only 1 DOCTYPE
expect(homeHTML).toMatch(/^<!DOCTYPE html><html/) expect(homeHTML).toMatch(/^<!DOCTYPE html><html/)
// should have default head when there's no head.js provided // should have default head when there's no head.js provided
expect(homeHTML).toContain('<meta charSet="utf-8"/>')
expect(homeHTML).toContain( expect(homeHTML).toContain(
'<meta charSet="utf-8"/><meta name="viewport" content="width=device-width, initial-scale=1"/>' '<meta name="viewport" content="width=device-width, initial-scale=1"/>'
) )
expect(homeHTML).toContain('component:index.server') expect(homeHTML).toContain('component:index.server')
expect(homeHTML).toContain('header:test-util') expect(homeHTML).toContain('header:test-util')
......
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