initial commit
This commit is contained in:
commit
c70fc72952
143 changed files with 13594 additions and 0 deletions
51
workers/sitemap/index.js
Normal file
51
workers/sitemap/index.js
Normal file
|
@ -0,0 +1,51 @@
|
|||
import { createClient } from "@supabase/supabase-js";
|
||||
|
||||
export default {
|
||||
async fetch(request, env) {
|
||||
const supabaseUrl = env.SUPABASE_URL;
|
||||
const supabaseKey = env.SUPABASE_KEY;
|
||||
const supabase = createClient(supabaseUrl, supabaseKey);
|
||||
|
||||
try {
|
||||
const { data, error } = await supabase
|
||||
.from("optimized_sitemap")
|
||||
.select("url, lastmod, changefreq, priority");
|
||||
|
||||
if (error) {
|
||||
console.error("Error fetching sitemap data:", error);
|
||||
return new Response("Error fetching sitemap data", { status: 500 });
|
||||
}
|
||||
|
||||
const sitemapXml = generateSitemapXml(data);
|
||||
return new Response(sitemapXml, {
|
||||
headers: {
|
||||
"Content-Type": "application/xml",
|
||||
"Access-Control-Allow-Origin": "*",
|
||||
},
|
||||
});
|
||||
} catch (error) {
|
||||
console.error("Unexpected error:", error);
|
||||
return new Response("Internal Server Error", { status: 500 });
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
function generateSitemapXml(data) {
|
||||
const urls = data
|
||||
.map(
|
||||
({ url, lastmod, changefreq, priority }) => `
|
||||
<url>
|
||||
<loc>${url}</loc>
|
||||
${lastmod ? `<lastmod>${new Date(lastmod).toISOString()}</lastmod>` : ""}
|
||||
<changefreq>${changefreq}</changefreq>
|
||||
<priority>${priority}</priority>
|
||||
</url>
|
||||
`
|
||||
)
|
||||
.join("");
|
||||
|
||||
return `<?xml version="1.0" encoding="UTF-8"?>
|
||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
||||
${urls}
|
||||
</urlset>`;
|
||||
}
|
Reference in a new issue