UNPKG

next

Version:

The React Framework

151 lines (150 loc) 5.89 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 0 && (module.exports = { DEFAULT_METADATA_ROUTE_EXTENSIONS: null, STATIC_METADATA_IMAGES: null, getExtensionRegexString: null, isMetadataPage: null, isMetadataRoute: null, isMetadataRouteFile: null, isStaticMetadataRoute: null }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { DEFAULT_METADATA_ROUTE_EXTENSIONS: function() { return DEFAULT_METADATA_ROUTE_EXTENSIONS; }, STATIC_METADATA_IMAGES: function() { return STATIC_METADATA_IMAGES; }, getExtensionRegexString: function() { return getExtensionRegexString; }, isMetadataPage: function() { return isMetadataPage; }, isMetadataRoute: function() { return isMetadataRoute; }, isMetadataRouteFile: function() { return isMetadataRouteFile; }, isStaticMetadataRoute: function() { return isStaticMetadataRoute; } }); const _normalizepathsep = require("../../shared/lib/page-path/normalize-path-sep"); const _apppaths = require("../../shared/lib/router/utils/app-paths"); const _isapprouteroute = require("../is-app-route-route"); 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' ] } }; const DEFAULT_METADATA_ROUTE_EXTENSIONS = [ 'js', 'jsx', 'ts', 'tsx' ]; const getExtensionRegexString = (staticExtensions, dynamicExtensions)=>{ // If there's no possible multi dynamic routes, will not match any <name>[].<ext> files if (!dynamicExtensions || dynamicExtensions.length === 0) { return `(\\.(?:${staticExtensions.join('|')}))`; } return `(?:\\.(${staticExtensions.join('|')})|(\\.(${dynamicExtensions.join('|')})))`; }; function isMetadataRouteFile(appDirRelativePath, pageExtensions, strictlyMatchExtensions) { // End with the extension or optional to have the extension // When strictlyMatchExtensions is true, it's used for match file path; // When strictlyMatchExtensions, the dynamic extension is skipped but // static extension is kept, which is usually used for matching route path. const trailingMatcher = (strictlyMatchExtensions ? '' : '?') + '$'; // Match the optional variants like /opengraph-image2, /icon-a102f4.png, etc. const variantsMatcher = '\\d?'; // The -\w{6} is the suffix that normalized from group routes; 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 = (0, _normalizepathsep.normalizePathSep)(appDirRelativePath); const matched = metadataRouteFilesRegex.some((r)=>r.test(normalizedAppDirRelativePath)); return matched; } function isStaticMetadataRoute(route) { // extract ext with regex const pathname = route.replace(/\/route$/, ''); const matched = (0, _isapprouteroute.isAppRouteRoute)(route) && isMetadataRouteFile(pathname, [], true) && // These routes can either be built by static or dynamic entrypoints, // so we assume they're dynamic pathname !== '/robots.txt' && pathname !== '/manifest.webmanifest' && !pathname.endsWith('/sitemap.xml'); return matched; } function isMetadataPage(page) { const matched = !(0, _isapprouteroute.isAppRouteRoute)(page) && isMetadataRouteFile(page, [], false); return matched; } function isMetadataRoute(route) { let page = (0, _apppaths.normalizeAppPath)(route).replace(/^\/?app\//, '')// Remove the dynamic route id .replace('/[__metadata_id__]', '')// Remove the /route suffix .replace(/\/route$/, ''); if (page[0] !== '/') page = '/' + page; const matched = (0, _isapprouteroute.isAppRouteRoute)(route) && isMetadataRouteFile(page, [], false); return matched; } //# sourceMappingURL=is-metadata-route.js.map