feat: add sitemap (#9)

pull/42/head
Ivan Reshetnikov 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
1 changed files with 23 additions and 0 deletions

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
}