/** * MITHOME-116: hreflang alternates, sitemap entries and the staging * indexing switch — one place so the page metadata, sitemap.xml and * robots.txt can never disagree. * * DECISION (user): every page is announced in BOTH languages, regardless of * the MITHOME-113 English approval status. * DECISION (user): staging is never indexed (robots.txt disallows all, no * sitemap, every page carries noindex). */ import type { MetadataRoute } from 'next' import { siteConfig } from '@/config/site' import { DEFAULT_LOCALE, LOCALES, PAGE_SLUGS, localePath, type Locale, type PageKey } from './i18n' type SitePage = PageKey | undefined // undefined = home const PAGES: readonly SitePage[] = [undefined, ...(Object.keys(PAGE_SLUGS) as PageKey[])] function siteOrigin(siteUrl: string = siteConfig.general.url): string { return siteUrl.replace(/\/+$/, '') } export function absoluteUrl(path: string, siteUrl?: string): string { return `${siteOrigin(siteUrl)}${path}` } /** hreflang map for one page: every locale + x-default (the Hungarian page). */ export function languageAlternates(page: SitePage, siteUrl?: string): Record { const languages: Record = Object.fromEntries( LOCALES.map((locale) => [locale, absoluteUrl(localePath(locale, page), siteUrl)]), ) return { ...languages, 'x-default': languages[DEFAULT_LOCALE] } } /** `alternates` for Next.js page metadata: self canonical + hreflang links. */ export function pageAlternates(locale: Locale, page: SitePage, siteUrl?: string) { return { canonical: absoluteUrl(localePath(locale, page), siteUrl), languages: languageAlternates(page, siteUrl), } } export function isIndexingDisabled(deployEnv: string | undefined = process.env.NEXT_PUBLIC_DEPLOY_ENV): boolean { return deployEnv === 'staging' } export function sitemapEntries(siteUrl?: string): MetadataRoute.Sitemap { return PAGES.flatMap((page) => LOCALES.map((locale) => ({ url: absoluteUrl(localePath(locale, page), siteUrl), changeFrequency: 'monthly' as const, priority: page === undefined ? 1 : page === 'privacy' || page === 'terms' ? 0.3 : 0.8, alternates: { languages: languageAlternates(page, siteUrl) }, })), ) } export function robotsRules(deployEnv?: string, siteUrl?: string): MetadataRoute.Robots { if (isIndexingDisabled(deployEnv)) { return { rules: { userAgent: '*', disallow: '/' } } } return { rules: { userAgent: '*', allow: '/', disallow: ['/admin', '/api/'] }, sitemap: absoluteUrl('/sitemap.xml', siteUrl), } }