feat: add sitemap (#9)

This commit is contained in:
Ivan R. 2024-02-27 22:45:45 +05:00
parent cc1fd12bb5
commit 53c26805bf
No known key found for this signature in database
GPG key ID: 56C7BAAE859B302C

23
src/pages/sitemap.xml.ts Normal file
View file

@ -0,0 +1,23 @@
import type { APIContext } from 'astro'
import { getCollection } from 'astro:content'
export async function GET(context: APIContext) {
const notes = await getCollection('notes')
let sitemap = `<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9" xmlns:xhtml="http://www.w3.org/1999/xhtml">
<url><loc>${context.site}</loc></url>
<url><loc>${context.site}services</loc></url>
<url><loc>${context.site}cinema</loc></url>`
notes.forEach(note => {
sitemap += `<url><loc>${context.site}notes/${note.slug}</loc></url>`
})
sitemap += '</urlset>'
const resp = new Response(sitemap)
resp.headers.set("Content-Type", "application/xml")
return resp
}