sitegen/framework/meta/nextjs/get-metadata-route.ts
chloe caruso af60d1172f i accidentally deleted the repo, but recovered it. i'll start committing
it was weird. i pressed delete on a subfolder, i think one of the
pages.off folders that i was using. and then, suddenly, nvim on windows
7 decided to delete every file in the directory. they weren't shred off
the space time continuum, but just marked deleted. i had to pay $80 to
get access to a software that could see them. bleh!

just seeing all my work, a little over a week, was pretty heart
shattering. but i remembered that long ago, a close friend said i could
call them whenever i was feeling sad. i finally took them up on that
offer. the first time i've ever called someone for emotional support.
but it's ok. i got it back. and the site framework is better than ever.

i'm gonna commit and push more often. the repo is private anyways.
2025-06-06 23:38:02 -07:00

67 lines
2.1 KiB
TypeScript

import { isMetadataRoute, isMetadataRouteFile } from "./is-metadata-route";
import path from "../../shared/lib/isomorphic/path";
import { djb2Hash } from "../../shared/lib/hash";
/*
* If there's special convention like (...) or @ in the page path,
* Give it a unique hash suffix to avoid conflicts
*
* e.g.
* /app/open-graph.tsx -> /open-graph/route
* /app/(post)/open-graph.tsx -> /open-graph/route-[0-9a-z]{6}
*/
export function getMetadataRouteSuffix(page: string) {
let suffix = "";
if ((page.includes("(") && page.includes(")")) || page.includes("@")) {
suffix = djb2Hash(page).toString(36).slice(0, 6);
}
return suffix;
}
/**
* Map metadata page key to the corresponding route
*
* static file page key: /app/robots.txt -> /robots.xml -> /robots.txt/route
* dynamic route page key: /app/robots.tsx -> /robots -> /robots.txt/route
*
* @param page
* @returns
*/
export function normalizeMetadataRoute(page: string) {
let route = page;
if (isMetadataRoute(page)) {
// Remove the file extension, e.g. /route-path/robots.txt -> /route-path
const pathnamePrefix = page.slice(0, -(path.basename(page).length + 1));
const suffix = getMetadataRouteSuffix(pathnamePrefix);
if (route === "/sitemap") {
route += ".xml";
}
if (route === "/robots") {
route += ".txt";
}
if (route === "/manifest") {
route += ".webmanifest";
}
// 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 isStaticMetadataFile = isMetadataRouteFile(route, [], true);
const { dir, name: baseName, ext } = path.parse(route);
const isSingleRoute = page.startsWith("/sitemap") ||
page.startsWith("/robots") ||
page.startsWith("/manifest") ||
isStaticMetadataFile;
route = path.join(
dir,
`${baseName}${suffix ? `-${suffix}` : ""}${ext}`,
isSingleRoute ? "" : "[[...__metadata_id__]]",
"route",
);
}
}
return route;
}