Add sitemap integration and render robots.txt via ts

This commit is contained in:
thiloho
2025-04-27 03:38:25 +02:00
parent 6a97f847c6
commit f88fe15fe0
12 changed files with 91 additions and 15 deletions

View File

@@ -26,9 +26,12 @@ const { title, description } = Astro.props;
<link
rel="alternate"
type="application/rss+xml"
title="Your Site's Title"
href={new URL("rss.xml", Astro.site)}
title="Thilo Hohlt's Blog"
href={new URL("rss.xml", Astro.url.origin)}
/>
<head>
<link rel="sitemap" href="/sitemap-index.xml" />
</head>
<ClientRouter />
<script is:inline>
const setTheme = () => {

12
src/pages/robots.txt.ts Normal file
View File

@@ -0,0 +1,12 @@
import type { APIRoute } from "astro";
const getRobotsTxt = (sitemapURL: URL) => `User-agent: *
Allow: /
Sitemap: ${sitemapURL.href}
`;
export const GET: APIRoute = (context) => {
const sitemapURL = new URL("sitemap-index.xml", context.url.origin);
return new Response(getRobotsTxt(sitemapURL));
};

View File

@@ -1,11 +1,11 @@
import rss from "@astrojs/rss";
import type { APIContext } from "astro";
import type { APIRoute } from "astro";
import { getCollection } from "astro:content";
import sanitizeHtml from "sanitize-html";
import MarkdownIt from "markdown-it";
const parser = new MarkdownIt();
export const GET = async (context: APIContext) => {
export const GET: APIRoute = async (context) => {
const blog = await getCollection("blog");
const latestModDate = blog.reduce((latest, article) => {