Files
anouma/lib/seo/redirects.ts
T

24 lines
902 B
TypeScript

import { cache } from "react";
import { getCMS } from "@/lib/payload/getPayload";
/**
* Looks up a CMS-configured redirect for a changed slug (see
* collections/Redirects.ts). Only called from the three dynamic [slug]
* detail pages right before they'd otherwise 404 - not a catch-all
* middleware, so this never adds a database lookup to the other ~30 routes
* that never need it.
*/
export const resolveRedirect = cache(async (fromPath: string): Promise<{ to: string; permanent: boolean } | null> => {
const payload = await getCMS();
const { docs } = await payload.find({
collection: "redirects",
where: { and: [{ fromPath: { equals: fromPath } }, { enabled: { equals: true } }] },
limit: 1,
depth: 0,
overrideAccess: false,
});
const redirect = docs[0];
if (!redirect) return null;
return { to: redirect.toPath, permanent: redirect.type !== "temporary" };
});