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', + }, + }) +}