From 10fe7b3da3b6ef8a13c58b5512910c1b44d8f133 Mon Sep 17 00:00:00 2001 From: ccbikai Date: Mon, 5 Aug 2024 12:04:34 +0800 Subject: [PATCH] feat: add sitemap generation for SEO enhancement Implements dynamic sitemap generation to improve SEO by providing search engines with updated site structure and content availability. This includes a root sitemap index and paginated sitemap files for individual posts, enhancing crawl efficiency and content discoverability. --- public/robots.txt | 3 +++ src/pages/sitemap.xml.js | 36 +++++++++++++++++++++++++++++++ src/pages/sitemap/[cursor].xml.js | 28 ++++++++++++++++++++++++ 3 files changed, 67 insertions(+) create mode 100644 public/robots.txt create mode 100644 src/pages/sitemap.xml.js create mode 100644 src/pages/sitemap/[cursor].xml.js diff --git a/public/robots.txt b/public/robots.txt new file mode 100644 index 0000000..0a7fb6a --- /dev/null +++ b/public/robots.txt @@ -0,0 +1,3 @@ +User-agent: * + +Sitemap: /sitemap.xml diff --git a/src/pages/sitemap.xml.js b/src/pages/sitemap.xml.js new file mode 100644 index 0000000..8725f08 --- /dev/null +++ b/src/pages/sitemap.xml.js @@ -0,0 +1,36 @@ +import { getChannelInfo } from '../lib/telegram' + +export const prerender = false + +export async function GET(Astro) { + const request = Astro.request + const url = new URL(request.url) + const channel = await getChannelInfo(Astro) + const posts = channel.posts || [] + + const pageSize = 20 + let count = +posts[0]?.id + + const pages = [] + pages.push(count) + while (count > pageSize) { + count -= pageSize + pages.push(count) + } + + const sitemaps = pages.map((page) => { + return ` + + ${url.origin}/sitemap/${page}.xml +` + }) + + return new Response(` + + ${sitemaps.join('')} +`, { + headers: { + 'Content-Type': 'application/xml', + }, + }) +} diff --git a/src/pages/sitemap/[cursor].xml.js b/src/pages/sitemap/[cursor].xml.js new file mode 100644 index 0000000..52763ed --- /dev/null +++ b/src/pages/sitemap/[cursor].xml.js @@ -0,0 +1,28 @@ +import { getChannelInfo } from '../../lib/telegram' + +export const prerender = false + +export async function GET(Astro) { + const request = Astro.request + const url = new URL(request.url) + const channel = await getChannelInfo(Astro, { + before: Astro.params.cursor, + }) + const posts = channel.posts || [] + + const xmlUrls = posts.map(post => ` + + ${url.origin}/posts/${post.id} + ${new Date(post.datetime).toISOString()} + + `).join('') + + return new Response(` + + ${xmlUrls} +`, { + headers: { + 'Content-Type': 'application/xml', + }, + }) +}