|
|
import type { PageExtensions } from '../../build/page-extensions-type' |
|
|
import { normalizePathSep } from '../../shared/lib/page-path/normalize-path-sep' |
|
|
import { normalizeAppPath } from '../../shared/lib/router/utils/app-paths' |
|
|
import { isAppRouteRoute } from '../is-app-route-route' |
|
|
|
|
|
export const STATIC_METADATA_IMAGES = { |
|
|
icon: { |
|
|
filename: 'icon', |
|
|
extensions: ['ico', 'jpg', 'jpeg', 'png', 'svg'], |
|
|
}, |
|
|
apple: { |
|
|
filename: 'apple-icon', |
|
|
extensions: ['jpg', 'jpeg', 'png'], |
|
|
}, |
|
|
favicon: { |
|
|
filename: 'favicon', |
|
|
extensions: ['ico'], |
|
|
}, |
|
|
openGraph: { |
|
|
filename: 'opengraph-image', |
|
|
extensions: ['jpg', 'jpeg', 'png', 'gif'], |
|
|
}, |
|
|
twitter: { |
|
|
filename: 'twitter-image', |
|
|
extensions: ['jpg', 'jpeg', 'png', 'gif'], |
|
|
}, |
|
|
} as const |
|
|
|
|
|
|
|
|
|
|
|
export const DEFAULT_METADATA_ROUTE_EXTENSIONS = ['js', 'jsx', 'ts', 'tsx'] |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export const getExtensionRegexString = ( |
|
|
staticExtensions: readonly string[], |
|
|
dynamicExtensions: readonly string[] | null |
|
|
) => { |
|
|
|
|
|
if (!dynamicExtensions || dynamicExtensions.length === 0) { |
|
|
return `(\\.(?:${staticExtensions.join('|')}))` |
|
|
} |
|
|
return `(?:\\.(${staticExtensions.join('|')})|(\\.(${dynamicExtensions.join('|')})))` |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isMetadataRouteFile( |
|
|
appDirRelativePath: string, |
|
|
pageExtensions: PageExtensions, |
|
|
strictlyMatchExtensions: boolean |
|
|
) { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
const trailingMatcher = (strictlyMatchExtensions ? '' : '?') + '$' |
|
|
|
|
|
const variantsMatcher = '\\d?' |
|
|
|
|
|
const groupSuffix = strictlyMatchExtensions ? '' : '(-\\w{6})?' |
|
|
|
|
|
const suffixMatcher = `${variantsMatcher}${groupSuffix}` |
|
|
|
|
|
const metadataRouteFilesRegex = [ |
|
|
new RegExp( |
|
|
`^[\\\\/]robots${getExtensionRegexString( |
|
|
pageExtensions.concat('txt'), |
|
|
null |
|
|
)}${trailingMatcher}` |
|
|
), |
|
|
new RegExp( |
|
|
`^[\\\\/]manifest${getExtensionRegexString( |
|
|
pageExtensions.concat('webmanifest', 'json'), |
|
|
null |
|
|
)}${trailingMatcher}` |
|
|
), |
|
|
new RegExp(`^[\\\\/]favicon\\.ico$`), |
|
|
new RegExp( |
|
|
`[\\\\/]sitemap${getExtensionRegexString(['xml'], pageExtensions)}${trailingMatcher}` |
|
|
), |
|
|
new RegExp( |
|
|
`[\\\\/]${STATIC_METADATA_IMAGES.icon.filename}${suffixMatcher}${getExtensionRegexString( |
|
|
STATIC_METADATA_IMAGES.icon.extensions, |
|
|
pageExtensions |
|
|
)}${trailingMatcher}` |
|
|
), |
|
|
new RegExp( |
|
|
`[\\\\/]${STATIC_METADATA_IMAGES.apple.filename}${suffixMatcher}${getExtensionRegexString( |
|
|
STATIC_METADATA_IMAGES.apple.extensions, |
|
|
pageExtensions |
|
|
)}${trailingMatcher}` |
|
|
), |
|
|
new RegExp( |
|
|
`[\\\\/]${STATIC_METADATA_IMAGES.openGraph.filename}${suffixMatcher}${getExtensionRegexString( |
|
|
STATIC_METADATA_IMAGES.openGraph.extensions, |
|
|
pageExtensions |
|
|
)}${trailingMatcher}` |
|
|
), |
|
|
new RegExp( |
|
|
`[\\\\/]${STATIC_METADATA_IMAGES.twitter.filename}${suffixMatcher}${getExtensionRegexString( |
|
|
STATIC_METADATA_IMAGES.twitter.extensions, |
|
|
pageExtensions |
|
|
)}${trailingMatcher}` |
|
|
), |
|
|
] |
|
|
|
|
|
const normalizedAppDirRelativePath = normalizePathSep(appDirRelativePath) |
|
|
const matched = metadataRouteFilesRegex.some((r) => |
|
|
r.test(normalizedAppDirRelativePath) |
|
|
) |
|
|
|
|
|
return matched |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isStaticMetadataRoute(route: string) { |
|
|
|
|
|
const pathname = route.replace(/\/route$/, '') |
|
|
|
|
|
const matched = |
|
|
isAppRouteRoute(route) && |
|
|
isMetadataRouteFile(pathname, [], true) && |
|
|
|
|
|
|
|
|
pathname !== '/robots.txt' && |
|
|
pathname !== '/manifest.webmanifest' && |
|
|
!pathname.endsWith('/sitemap.xml') |
|
|
|
|
|
return matched |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isMetadataPage(page: string) { |
|
|
const matched = !isAppRouteRoute(page) && isMetadataRouteFile(page, [], false) |
|
|
|
|
|
return matched |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export function isMetadataRoute(route: string): boolean { |
|
|
let page = normalizeAppPath(route) |
|
|
.replace(/^\/?app\//, '') |
|
|
|
|
|
.replace('/[__metadata_id__]', '') |
|
|
|
|
|
.replace(/\/route$/, '') |
|
|
|
|
|
if (page[0] !== '/') page = '/' + page |
|
|
|
|
|
const matched = isAppRouteRoute(route) && isMetadataRouteFile(page, [], false) |
|
|
|
|
|
return matched |
|
|
} |
|
|
|