This repository has been archived on 2025-03-28. You can view files and clone it, but cannot push or open issues or pull requests.
coryd.dev-astro/scripts/sitemap-generate.mjs

52 lines
1.3 KiB
JavaScript

import { createClient } from "@supabase/supabase-js";
import { writeFileSync } from "fs";
import dotenv from "dotenv";
dotenv.config({ path: ".env.local" });
const SUPABASE_URL = process.env.SUPABASE_URL;
const SUPABASE_KEY = process.env.SUPABASE_KEY;
const supabase = createClient(SUPABASE_URL, SUPABASE_KEY);
const PAGE_SIZE = 1000;
async function fetchPaginatedData() {
let allEntries = [];
let currentPage = 0;
while (true) {
const { data, error } = await supabase
.from("optimized_sitemap")
.select("url, title, lastmod, changefreq, priority")
.range(currentPage * PAGE_SIZE, (currentPage + 1) * PAGE_SIZE - 1);
if (error) throw new Error(`Error fetching sitemap data: ${error.message}`);
if (!data || data.length === 0) break;
allEntries = allEntries.concat(data);
currentPage += 1;
}
return allEntries;
}
async function generateSitemap() {
try {
const data = await fetchPaginatedData();
const sitemapEntries = data.map((entry) => ({ loc: entry.url }));
writeFileSync(
"src/utils/data/static/sitemapData.json",
JSON.stringify(sitemapEntries, null, 2),
"utf8"
);
console.log("Sitemap data generated successfully!");
} catch (error) {
console.error("Error generating sitemap:", error);
}
}
generateSitemap();