|
| 1 | +import type { MetadataRoute } from "next"; |
| 2 | +import { articleLocales } from "@modules/i18n/available"; |
| 3 | +import items from "@modules/article/items"; |
| 4 | + |
| 5 | +const BASE = process.env.BASE_URL; |
| 6 | + |
| 7 | +if (!BASE) { |
| 8 | + throw new Error("BASE_URL is not defined"); |
| 9 | +} |
| 10 | + |
| 11 | +export default function sitemap(): MetadataRoute.Sitemap { |
| 12 | + const urls: MetadataRoute.Sitemap = [ |
| 13 | + { url: `${BASE}/`, changeFrequency: "weekly" }, |
| 14 | + { url: `${BASE}/portfolio`, changeFrequency: "monthly" }, |
| 15 | + ]; |
| 16 | + |
| 17 | + for (const item of items) { |
| 18 | + const locales = articleLocales[item.slug] ?? []; |
| 19 | + |
| 20 | + // If there are locales available for the article, add entries for each locale |
| 21 | + if (locales.length > 0) { |
| 22 | + const languages = Object.fromEntries( |
| 23 | + locales.map((l) => [l, `${BASE}/${l}/${item.slug}`]), |
| 24 | + ); |
| 25 | + for (const l of locales) { |
| 26 | + urls.push({ |
| 27 | + url: `${BASE}/${l}/${item.slug}`, |
| 28 | + changeFrequency: "monthly", |
| 29 | + lastModified: item.updatedAt, |
| 30 | + alternates: { languages }, |
| 31 | + }); |
| 32 | + } |
| 33 | + continue; |
| 34 | + } |
| 35 | + |
| 36 | + // Default entry without locale |
| 37 | + urls.push({ |
| 38 | + url: `${BASE}/${item.slug}`, |
| 39 | + changeFrequency: "monthly", |
| 40 | + lastModified: item.updatedAt, |
| 41 | + }); |
| 42 | + } |
| 43 | + |
| 44 | + return urls; |
| 45 | +} |
0 commit comments