Skip to content

Commit

Permalink
manual sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
viperML committed Sep 15, 2024
1 parent a031334 commit 8edf915
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 60 deletions.
2 changes: 0 additions & 2 deletions astro.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import mdx from '@astrojs/mdx';
import { remarkMark } from 'remark-mark-highlight'
import rehypeSectionHeadings from "@maxmmyron/rehype-section-headings";

import sitemap from "@astrojs/sitemap";
import { remarkCodeMeta } from './src/remark';

// https://astro.build/config
Expand All @@ -22,7 +21,6 @@ export default defineConfig({
applyBaseStyles: false
}),
react(),
sitemap(),
mdx()
],
devToolbar: {
Expand Down
58 changes: 4 additions & 54 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 0 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
"@astrojs/mdx": "^3.1.5",
"@astrojs/react": "^3.6.0",
"@astrojs/rss": "^4.0.6",
"@astrojs/sitemap": "^3.1.6",
"@astrojs/tailwind": "^5.1.0",
"@eslint/js": "^9.6.0",
"@fontsource/geist-sans": "^5.0.3",
Expand Down
2 changes: 1 addition & 1 deletion src/components/SEO.astro
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ const site: URL = (() => {
}

<!-- Sitemap -->
<link rel="sitemap" href="/sitemap-index.xml" />
<link rel="sitemap" href="/sitemap.xml" />

<!-- OpenGraph -->
<meta property="og:title" content={finalTitle} />
Expand Down
4 changes: 2 additions & 2 deletions src/pages/robots.txt.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
export async function GET(): Promise<Response> {

const resp = new Response((`User-agent: *
const resp = new Response(`User-agent: *
Allow: /
Sitemap: ${import.meta.env.SITE}/sitemap-index.xml`).trim());
Sitemap: ${import.meta.env.SITE}/sitemap.xml`.trim());

resp.headers.set("Content-Type", "text/plain; charset=utf-8");

Expand Down
29 changes: 29 additions & 0 deletions src/pages/sitemap.xml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { getCollection } from "astro:content";

function mkUrl(url: string): string {
return `<url><loc>${url}</loc></url>`
}

export async function GET(): Promise<Response> {
const site = import.meta.env.SITE;
const posts = await getCollection("blog");



const text = `
<?xml version="1.0" encoding="UTF-8"?>
<urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">
${mkUrl(site)}
${mkUrl(`${site}/blog`)}
${posts.filter(p => !(p.data.draft ?? false)).map(p =>
mkUrl(`${site}/blog/${p.slug}`)
).join("\n")}
</urlset>
`.trim();

const resp = new Response(text);

resp.headers.set("Content-Type", "application/xml");

return resp;
}

0 comments on commit 8edf915

Please sign in to comment.