Files
portfolio/lib/seo.ts
2026-01-12 15:27:35 +00:00

31 lines
977 B
TypeScript

import { locales, type AppLocale } from "@/i18n/locales";
export function getBaseUrl(): string {
const raw =
process.env.NEXT_PUBLIC_BASE_URL ||
process.env.NEXTAUTH_URL || // fallback if ever added
"http://localhost:3000";
return raw.replace(/\/+$/, "");
}
export function toAbsoluteUrl(path: string): string {
const base = getBaseUrl();
const normalized = path.startsWith("/") ? path : `/${path}`;
return `${base}${normalized}`;
}
export function getLanguageAlternates(opts: {
/** Path without locale prefix, e.g. "/projects" or "/projects/my-slug" or "" */
pathWithoutLocale: string;
}): Record<AppLocale, string> {
const path = opts.pathWithoutLocale === "" ? "" : `/${opts.pathWithoutLocale}`.replace(/\/{2,}/g, "/");
const normalizedPath = path === "/" ? "" : path;
return locales.reduce((acc, l) => {
const url = toAbsoluteUrl(`/${l}${normalizedPath}`);
acc[l] = url;
return acc;
}, {} as Record<AppLocale, string>);
}