Merged in fix/seo-whitelist-scandic-friends (pull request #2071)

fix: SEO whitelist /scandic-friends

* fix: SEO whitelist /scandic-friends

* fix: SEO whitelist /scandic-friends


Approved-by: Linus Flood
This commit is contained in:
Joakim Jäderberg
2025-05-13 09:35:28 +00:00
committed by Linus Flood
parent 13261d425c
commit aceb88cb1a
2 changed files with 44 additions and 9 deletions

View File

@@ -242,6 +242,6 @@ export const metadataQueryRouter = router({
}
}
return metadata
return { metadata, alternates }
}),
})

View File

@@ -1,6 +1,8 @@
import { env } from "@/env/server"
import { serverClient } from "@/lib/trpc/server"
import type { AlternateURLs } from "next/dist/lib/metadata/types/alternative-urls-types"
import type {
ContentTypeParams,
LangParams,
@@ -20,11 +22,12 @@ export async function generateMetadata({
// 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,
})
const { metadata, alternates } =
await serverClient().contentstack.metadata.get({
subpage,
filterFromUrl,
noIndex: noIndexOnSearchParams,
})
if (!metadata) {
return {
@@ -43,16 +46,48 @@ export async function generateMetadata({
...metadata,
robots: {
...(metadata.robots ?? {}),
index: isIndexable(metadata.robots?.index, params.lang),
follow: isIndexable(metadata.robots?.follow, params.lang),
index: isIndexable(metadata.robots?.index, params.lang, alternates),
follow: isIndexable(metadata.robots?.follow, params.lang, alternates),
},
}
}
function isIndexable(
pageIndexableFromSettings: boolean | null | undefined,
lang: Lang
lang: Lang,
alternates: AlternateURLs | null
) {
// This is a special case for whitelisting the scandic friends pages, this can be removed when all pages are live
const url = getUrl(alternates)
const firstNonLangSegment = (url ?? "").substring(3)
if (firstNonLangSegment.startsWith("/scandic-friends")) {
return true
}
// 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)
}
function getUrl(alternates: AlternateURLs | null): string | null {
try {
if (!alternates?.canonical) {
return null
}
if (typeof alternates.canonical === "string") {
return alternates.canonical
}
if ("href" in alternates.canonical) {
return alternates.canonical.href
}
if (typeof alternates.canonical.url === "string") {
return alternates.canonical.url
}
return alternates.canonical.url.href
} catch {
return null
}
}