feat: remote sitemap
This commit is contained in:
parent
193366e946
commit
0275c8f02f
7 changed files with 86 additions and 52 deletions
4
package-lock.json
generated
4
package-lock.json
generated
|
@ -1,12 +1,12 @@
|
||||||
{
|
{
|
||||||
"name": "coryd.dev",
|
"name": "coryd.dev",
|
||||||
"version": "1.4.0",
|
"version": "1.5.0",
|
||||||
"lockfileVersion": 3,
|
"lockfileVersion": 3,
|
||||||
"requires": true,
|
"requires": true,
|
||||||
"packages": {
|
"packages": {
|
||||||
"": {
|
"": {
|
||||||
"name": "coryd.dev",
|
"name": "coryd.dev",
|
||||||
"version": "1.4.0",
|
"version": "1.5.0",
|
||||||
"license": "MIT",
|
"license": "MIT",
|
||||||
"dependencies": {
|
"dependencies": {
|
||||||
"@cdransf/api-text": "^1.5.0",
|
"@cdransf/api-text": "^1.5.0",
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
{
|
{
|
||||||
"name": "coryd.dev",
|
"name": "coryd.dev",
|
||||||
"version": "1.4.0",
|
"version": "1.5.0",
|
||||||
"description": "The source for my personal site. Built using 11ty (and other tools).",
|
"description": "The source for my personal site. Built using 11ty (and other tools).",
|
||||||
"type": "module",
|
"type": "module",
|
||||||
"engines": {
|
"engines": {
|
||||||
|
|
|
@ -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 || []
|
|
||||||
}
|
|
|
@ -1,16 +0,0 @@
|
||||||
---
|
|
||||||
permalink: "/sitemap.xml"
|
|
||||||
layout: null
|
|
||||||
eleventyExcludeFromCollections: true
|
|
||||||
---
|
|
||||||
<?xml version="1.0" encoding="utf-8"?>
|
|
||||||
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
|
|
||||||
{% for page in sitemap -%}
|
|
||||||
<url>
|
|
||||||
<loc>{{ page.url }}</loc>
|
|
||||||
<lastmod>{{ page.date | date: '%Y-%m-%dT%H:%M:%S%:z' }}</lastmod>
|
|
||||||
<changefreq>monthly</changefreq>
|
|
||||||
<priority>0.5</priority>
|
|
||||||
</url>
|
|
||||||
{% endfor %}
|
|
||||||
</urlset>
|
|
|
@ -3,7 +3,9 @@ WITH sitemap_data AS (
|
||||||
SELECT
|
SELECT
|
||||||
p.date AS content_date,
|
p.date AS content_date,
|
||||||
p.title,
|
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
|
FROM optimized_posts p
|
||||||
|
|
||||||
UNION ALL
|
UNION ALL
|
||||||
|
@ -11,7 +13,9 @@ WITH sitemap_data AS (
|
||||||
SELECT
|
SELECT
|
||||||
b.date_finished AS content_date,
|
b.date_finished AS content_date,
|
||||||
b.title,
|
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
|
FROM optimized_books b
|
||||||
|
|
||||||
UNION ALL
|
UNION ALL
|
||||||
|
@ -19,7 +23,9 @@ WITH sitemap_data AS (
|
||||||
SELECT
|
SELECT
|
||||||
m.last_watched AS content_date,
|
m.last_watched AS content_date,
|
||||||
m.title,
|
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
|
FROM optimized_movies m
|
||||||
|
|
||||||
UNION ALL
|
UNION ALL
|
||||||
|
@ -27,7 +33,9 @@ WITH sitemap_data AS (
|
||||||
SELECT
|
SELECT
|
||||||
NULL AS content_date,
|
NULL AS content_date,
|
||||||
ar.name AS title,
|
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
|
FROM optimized_artists ar
|
||||||
|
|
||||||
UNION ALL
|
UNION ALL
|
||||||
|
@ -35,7 +43,9 @@ WITH sitemap_data AS (
|
||||||
SELECT
|
SELECT
|
||||||
NULL AS content_date,
|
NULL AS content_date,
|
||||||
g.name AS title,
|
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
|
FROM optimized_genres g
|
||||||
|
|
||||||
UNION ALL
|
UNION ALL
|
||||||
|
@ -43,16 +53,16 @@ WITH sitemap_data AS (
|
||||||
SELECT
|
SELECT
|
||||||
s.last_watched_at AS content_date,
|
s.last_watched_at AS content_date,
|
||||||
s.title,
|
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
|
FROM optimized_shows s
|
||||||
)
|
)
|
||||||
|
|
||||||
SELECT
|
SELECT
|
||||||
json_agg(
|
url,
|
||||||
json_build_object(
|
title,
|
||||||
'url', sd.url,
|
content_date AS lastmod,
|
||||||
'title', sd.title,
|
changefreq,
|
||||||
'date', sd.content_date
|
priority
|
||||||
)
|
FROM sitemap_data;
|
||||||
) AS sitemap
|
|
||||||
FROM sitemap_data sd;
|
|
47
workers/sitemap/index.js
Normal file
47
workers/sitemap/index.js
Normal file
|
@ -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>
|
||||||
|
<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>`
|
||||||
|
}
|
12
workers/sitemap/wrangler.template.toml
Normal file
12
workers/sitemap/wrangler.template.toml
Normal file
|
@ -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}" },
|
||||||
|
]
|
Reference in a new issue