From 0275c8f02fc2ee86ff1e83db4cd4a3cb82fb4fab Mon Sep 17 00:00:00 2001 From: Cory Dransfeldt Date: Sat, 19 Oct 2024 13:43:07 -0700 Subject: [PATCH] feat: remote sitemap --- package-lock.json | 4 +-- package.json | 2 +- src/data/sitemap.js | 19 ----------- src/pages/meta/sitemap.liquid | 16 --------- views/feeds/sitemap.psql | 38 +++++++++++++-------- workers/sitemap/index.js | 47 ++++++++++++++++++++++++++ workers/sitemap/wrangler.template.toml | 12 +++++++ 7 files changed, 86 insertions(+), 52 deletions(-) delete mode 100644 src/data/sitemap.js delete mode 100644 src/pages/meta/sitemap.liquid create mode 100644 workers/sitemap/index.js create mode 100644 workers/sitemap/wrangler.template.toml diff --git a/package-lock.json b/package-lock.json index eb073793..34ad471a 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,12 +1,12 @@ { "name": "coryd.dev", - "version": "1.4.0", + "version": "1.5.0", "lockfileVersion": 3, "requires": true, "packages": { "": { "name": "coryd.dev", - "version": "1.4.0", + "version": "1.5.0", "license": "MIT", "dependencies": { "@cdransf/api-text": "^1.5.0", diff --git a/package.json b/package.json index 9b064dd0..59d09535 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "coryd.dev", - "version": "1.4.0", + "version": "1.5.0", "description": "The source for my personal site. Built using 11ty (and other tools).", "type": "module", "engines": { diff --git a/src/data/sitemap.js b/src/data/sitemap.js deleted file mode 100644 index 00b702a4..00000000 --- a/src/data/sitemap.js +++ /dev/null @@ -1,19 +0,0 @@ -import { createClient } from '@supabase/supabase-js' - -const SUPABASE_URL = process.env.SUPABASE_URL -const SUPABASE_KEY = process.env.SUPABASE_KEY -const supabase = createClient(SUPABASE_URL, SUPABASE_KEY) - -export default async function fetchSitemap() { - const { data, error } = await supabase - .from('optimized_sitemap') - .select('sitemap') - - if (error) { - console.error('Error fetching sitemap data:', error) - return [] - } - - const [{ sitemap } = {}] = data - return sitemap || [] -} diff --git a/src/pages/meta/sitemap.liquid b/src/pages/meta/sitemap.liquid deleted file mode 100644 index 68c0f740..00000000 --- a/src/pages/meta/sitemap.liquid +++ /dev/null @@ -1,16 +0,0 @@ ---- -permalink: "/sitemap.xml" -layout: null -eleventyExcludeFromCollections: true ---- - - - {% for page in sitemap -%} - - {{ page.url }} - {{ page.date | date: '%Y-%m-%dT%H:%M:%S%:z' }} - monthly - 0.5 - - {% endfor %} - \ No newline at end of file diff --git a/views/feeds/sitemap.psql b/views/feeds/sitemap.psql index 5c6a051e..84628298 100644 --- a/views/feeds/sitemap.psql +++ b/views/feeds/sitemap.psql @@ -3,7 +3,9 @@ WITH sitemap_data AS ( SELECT p.date AS content_date, p.title, - CONCAT('https://coryd.dev', p.url) AS url + CONCAT('https://coryd.dev', p.url) AS url, + 'monthly' AS changefreq, + 0.7 AS priority FROM optimized_posts p UNION ALL @@ -11,7 +13,9 @@ WITH sitemap_data AS ( SELECT b.date_finished AS content_date, b.title, - CONCAT('https://coryd.dev', b.url) AS url + CONCAT('https://coryd.dev', b.url) AS url, + 'monthly' AS changefreq, + 0.5 AS priority FROM optimized_books b UNION ALL @@ -19,7 +23,9 @@ WITH sitemap_data AS ( SELECT m.last_watched AS content_date, m.title, - CONCAT('https://coryd.dev', m.url) AS url + CONCAT('https://coryd.dev', m.url) AS url, + 'weekly' AS changefreq, + 0.6 AS priority FROM optimized_movies m UNION ALL @@ -27,7 +33,9 @@ WITH sitemap_data AS ( SELECT NULL AS content_date, ar.name AS title, - CONCAT('https://coryd.dev', ar.url) AS url + CONCAT('https://coryd.dev', ar.url) AS url, + 'monthly' AS changefreq, + 0.5 AS priority FROM optimized_artists ar UNION ALL @@ -35,7 +43,9 @@ WITH sitemap_data AS ( SELECT NULL AS content_date, g.name AS title, - CONCAT('https://coryd.dev', g.url) AS url + CONCAT('https://coryd.dev', g.url) AS url, + 'yearly' AS changefreq, + 0.3 AS priority FROM optimized_genres g UNION ALL @@ -43,16 +53,16 @@ WITH sitemap_data AS ( SELECT s.last_watched_at AS content_date, s.title, - CONCAT('https://coryd.dev', s.url) AS url + CONCAT('https://coryd.dev', s.url) AS url, + 'weekly' AS changefreq, + 0.8 AS priority FROM optimized_shows s ) SELECT - json_agg( - json_build_object( - 'url', sd.url, - 'title', sd.title, - 'date', sd.content_date - ) - ) AS sitemap -FROM sitemap_data sd; \ No newline at end of file + url, + title, + content_date AS lastmod, + changefreq, + priority +FROM sitemap_data; \ No newline at end of file diff --git a/workers/sitemap/index.js b/workers/sitemap/index.js new file mode 100644 index 00000000..82f591b1 --- /dev/null +++ b/workers/sitemap/index.js @@ -0,0 +1,47 @@ +import { createClient } from '@supabase/supabase-js' + +export default { + async fetch(request, env) { + const supabaseUrl = env.SUPABASE_URL || process.env.SUPABASE_URL + const supabaseKey = env.SUPABASE_KEY || process.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} + ${lastmod ? `${new Date(lastmod).toISOString()}` : ''} + ${changefreq} + ${priority} + + `).join('') + + return ` + + ${urls} + ` +} \ No newline at end of file diff --git a/workers/sitemap/wrangler.template.toml b/workers/sitemap/wrangler.template.toml new file mode 100644 index 00000000..aa682af4 --- /dev/null +++ b/workers/sitemap/wrangler.template.toml @@ -0,0 +1,12 @@ +name = "sitemap-worker" +main = "./index.js" +compatibility_date = "2023-01-01" + +account_id = "${CF_ACCOUNT_ID}" +workers_dev = true + +[env.production] +name = "sitemap-worker-production" +routes = [ + { pattern = "coryd.dev/sitemap.xml", zone_id = "${CF_ZONE_ID}" }, +] \ No newline at end of file