/* * kudos to leerob from vercel * https://leerob.io/blog/nextjs-sitemap-robots */ import { createClient } from '@supabase/supabase-js' import fs from 'fs' import { globby } from 'globby' import matter from 'gray-matter' import { format } from 'oxfmt' import { checkGuidePageEnabled } from '~/features/docs/NavigationPageStatus.utils' import { fetchGuidePageDates } from '~/lib/last-changed.utils' import { type Database } from '~/lib/supabase' import { generateReferencePages } from './files/reference-lib' const LOOKUP_TIMEOUT_MS = 30_000 /** * Loads each guide's last content change from the `last_changed` table, the * same source as the "Last updated" date on guide pages. Returns an empty map * when credentials are missing or the read fails, so the sitemap still builds * (without ``) in local and preview environments. */ async function loadGuideDates(): Promise> { const url = process.env.NEXT_PUBLIC_SUPABASE_URL const key = process.env.SUPABASE_SECRET_KEY if (!url || !key) { console.warn('Supabase credentials not set, so the sitemap omits last-modified dates') return new Map() } try { return await fetchGuidePageDates( createClient(url, key), AbortSignal.timeout(LOOKUP_TIMEOUT_MS) ) } catch (error) { console.warn('Failed to load last-modified dates, so the sitemap omits them:', error) return new Map() } } async function generate() { const [referencePages, guideDates] = await Promise.all([ generateReferencePages(), loadGuideDates(), ]) const contentFiles = await globby(['content/guides/**/!(_)*.mdx']) const contentPages = await Promise.all( contentFiles.map(async (filePath) => { const fileContents = await fs.promises.readFile(filePath, 'utf8') const { data: { sitemapPriority }, } = matter(fileContents) const lastUpdated = guideDates.get(`/${filePath}`) return { link: filePath.replace(/^content\//, '').replace(/\.mdx$/, ''), priority: sitemapPriority ?? 0.8, lastmod: lastUpdated ? new Date(lastUpdated).toISOString() : undefined, } }) ) const allPages = (contentPages as Array<{ link: string; priority: number; lastmod?: string }>) .filter(({ link }) => checkGuidePageEnabled(`/${link}`)) .concat(referencePages) // Keep the output order stable across builds allPages.sort((a, b) => (a.link < b.link ? -1 : a.link > b.link ? 1 : 0)) const sitemap = ` ${allPages .map(({ link, priority, lastmod }) => { const finalPriority = priority ?? 0.8 return ` ${`https://supabase.com/docs/${link}`}${lastmod ? `\n${lastmod}` : ''} weekly ${finalPriority} ` }) .join('')} ` const { code: formatted, errors } = await format('sitemap.html', sitemap, { tabWidth: 2, printWidth: 100, endOfLine: 'lf', }) if (errors.length > 0) { throw new Error(`Failed to format docs sitemap: ${errors.map((e) => e.message).join('; ')}`) } const sitemapFilePath = `public/sitemap.xml` const datedCount = allPages.filter((page) => page.lastmod).length console.log( `Total of ${allPages.length} pages in sitemap (${datedCount} with last-modified dates), located at /apps/docs/${sitemapFilePath}` ) fs.writeFileSync(sitemapFilePath, formatted) } generate()