UNPKG

next

Version:

The React Framework

95 lines (94 loc) 3.81 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); 0 && (module.exports = { fillMetadataSegment: null, normalizeMetadataPageToRoute: null, normalizeMetadataRoute: null }); function _export(target, all) { for(var name in all)Object.defineProperty(target, name, { enumerable: true, get: all[name] }); } _export(exports, { fillMetadataSegment: function() { return fillMetadataSegment; }, normalizeMetadataPageToRoute: function() { return normalizeMetadataPageToRoute; }, normalizeMetadataRoute: function() { return normalizeMetadataRoute; } }); const _ismetadataroute = require("./is-metadata-route"); const _path = /*#__PURE__*/ _interop_require_default(require("../../shared/lib/isomorphic/path")); const _serverutils = require("../../server/server-utils"); const _routeregex = require("../../shared/lib/router/utils/route-regex"); const _hash = require("../../shared/lib/hash"); const _apppaths = require("../../shared/lib/router/utils/app-paths"); const _normalizepathsep = require("../../shared/lib/page-path/normalize-path-sep"); function _interop_require_default(obj) { return obj && obj.__esModule ? obj : { default: obj }; } /* * If there's special convention like (...) or @ in the page path, * Give it a unique hash suffix to avoid conflicts * * e.g. * /app/opengraph-image.tsx -> /opengraph-image * /app/(post)/opengraph-image.tsx -> /opengraph-image-[0-9a-z]{6} */ function getMetadataRouteSuffix(page) { let suffix = ''; if (page.includes('(') && page.includes(')') || page.includes('@')) { suffix = (0, _hash.djb2Hash)(page).toString(36).slice(0, 6); } return suffix; } function fillMetadataSegment(segment, params, imageSegment) { const pathname = (0, _apppaths.normalizeAppPath)(segment); const routeRegex = (0, _routeregex.getNamedRouteRegex)(pathname, false); const route = (0, _serverutils.interpolateDynamicPath)(pathname, params, routeRegex); const suffix = getMetadataRouteSuffix(segment); const routeSuffix = suffix ? `-${suffix}` : ''; const { name, ext } = _path.default.parse(imageSegment); return (0, _normalizepathsep.normalizePathSep)(_path.default.join(route, `${name}${routeSuffix}${ext}`)); } function normalizeMetadataRoute(page) { if (!(0, _ismetadataroute.isMetadataRoute)(page)) { return page; } let route = page; let suffix = ''; if (page === '/robots') { route += '.txt'; } else if (page === '/manifest') { route += '.webmanifest'; } else { // Remove the file extension, // e.g. /path/robots.txt -> /route-path // e.g. /path/opengraph-image.tsx -> /path/opengraph-image const pathnamePrefix = page.slice(0, -(_path.default.basename(page).length + 1)); suffix = getMetadataRouteSuffix(pathnamePrefix); } // Support both /<metadata-route.ext> and custom routes /<metadata-route>/route.ts. // If it's a metadata file route, we need to append /[id]/route to the page. if (!route.endsWith('/route')) { const { dir, name: baseName, ext } = _path.default.parse(route); route = _path.default.posix.join(dir, `${baseName}${suffix ? `-${suffix}` : ''}${ext}`, 'route'); } return route; } function normalizeMetadataPageToRoute(page, isDynamic) { const isRoute = page.endsWith('/route'); const routePagePath = isRoute ? page.slice(0, -'/route'.length) : page; const metadataRouteExtension = routePagePath.endsWith('/sitemap') ? '.xml' : ''; const mapped = isDynamic ? `${routePagePath}/[__metadata_id__]` : `${routePagePath}${metadataRouteExtension}`; return mapped + (isRoute ? '/route' : ''); } //# sourceMappingURL=get-metadata-route.js.map