Files
web/apps/scandic-web/utils/generateMetadata.ts
Joakim Jäderberg 45d474385c Merged in fix/noindex-contentpages (pull request #2066)
fix: add noindex+nofollow to contentpages

* fix: add noindex+nofollow to contentpages

* fix: move no-index/no-follow to the shared generateMetadata function instead

* remove comment


Approved-by: Erik Tiekstra
2025-05-13 08:29:49 +00:00

59 lines
1.5 KiB
TypeScript

import { env } from "@/env/server"
import { serverClient } from "@/lib/trpc/server"
import type {
ContentTypeParams,
LangParams,
PageArgs,
UIDParams,
} from "@/types/params"
import type { Lang } from "@/constants/languages"
export async function generateMetadata({
searchParams,
params,
}: PageArgs<
LangParams & ContentTypeParams & UIDParams,
{ subpage?: string; filterFromUrl?: string }
>) {
const { subpage, filterFromUrl, ...otherSearchParams } = searchParams
// If there are other (real) search params, we don't want to index the page as this will
// cause duplicate content issues.
const noIndexOnSearchParams = !!Object.keys(otherSearchParams).length
const metadata = await serverClient().contentstack.metadata.get({
subpage,
filterFromUrl,
noIndex: noIndexOnSearchParams,
})
if (!metadata) {
return {
robots: {
index: env.isLangLive(params.lang),
follow: env.isLangLive(params.lang),
},
}
}
if (typeof metadata?.robots === "string") {
return metadata
}
return {
...metadata,
robots: {
...(metadata.robots ?? {}),
index: isIndexable(metadata.robots?.index, params.lang),
follow: isIndexable(metadata.robots?.follow, params.lang),
},
}
}
function isIndexable(
pageIndexableFromSettings: boolean | null | undefined,
lang: Lang
) {
// If we are live we want to index the page, but if the page has been marked as noindex in contentstack we don't
return (pageIndexableFromSettings ?? true) && env.isLangLive(lang)
}