|
| 1 | +import { and, db, eq, organization, uptimeSchedules } from "@databuddy/db"; |
| 2 | +import type { MetadataRoute } from "next"; |
| 3 | +import { unstable_cache } from "next/cache"; |
| 4 | +import { APP_URL } from "@/lib/app-url"; |
| 5 | +import type { StatusSitemapRow } from "@/types/sitemap"; |
| 6 | + |
| 7 | +export const revalidate = 86_400; |
| 8 | + |
| 9 | +const getPublicStatusPages = unstable_cache( |
| 10 | + async (): Promise<StatusSitemapRow[]> => { |
| 11 | + const rows = await db |
| 12 | + .select({ |
| 13 | + slug: organization.slug, |
| 14 | + updatedAt: uptimeSchedules.updatedAt, |
| 15 | + }) |
| 16 | + .from(organization) |
| 17 | + .innerJoin( |
| 18 | + uptimeSchedules, |
| 19 | + and( |
| 20 | + eq(uptimeSchedules.organizationId, organization.id), |
| 21 | + eq(uptimeSchedules.isPublic, true), |
| 22 | + eq(uptimeSchedules.isPaused, false) |
| 23 | + ) |
| 24 | + ); |
| 25 | + |
| 26 | + const statusPagesBySlug = new Map<string, Date>(); |
| 27 | + |
| 28 | + for (const row of rows) { |
| 29 | + if (!row.slug) { |
| 30 | + continue; |
| 31 | + } |
| 32 | + |
| 33 | + const existingUpdatedAt = statusPagesBySlug.get(row.slug); |
| 34 | + if (!existingUpdatedAt || row.updatedAt > existingUpdatedAt) { |
| 35 | + statusPagesBySlug.set(row.slug, row.updatedAt); |
| 36 | + } |
| 37 | + } |
| 38 | + |
| 39 | + return [...statusPagesBySlug.entries()] |
| 40 | + .map(([slug, updatedAt]) => ({ slug, updatedAt })) |
| 41 | + .sort((a, b) => a.slug.localeCompare(b.slug)); |
| 42 | + }, |
| 43 | + ["dashboard-status-sitemap"], |
| 44 | + { revalidate: 86_400, tags: ["status-page"] } |
| 45 | +); |
| 46 | + |
| 47 | +export default async function sitemap(): Promise<MetadataRoute.Sitemap> { |
| 48 | + const statusPages = await getPublicStatusPages(); |
| 49 | + |
| 50 | + return statusPages.map((statusPage) => ({ |
| 51 | + url: `${APP_URL}/status/${statusPage.slug}`, |
| 52 | + lastModified: statusPage.updatedAt, |
| 53 | + changeFrequency: "daily", |
| 54 | + priority: 0.7, |
| 55 | + })); |
| 56 | +} |
0 commit comments