Content-Length: 272206 | pFad | http://github.com/cptchloroplast/blog/commit/b3b85efa6f0ddded1d77384843dabf27a865ccbb

E4 feat: add sitemap · cptchloroplast/blog@b3b85ef · GitHub
Skip to content

Commit

Permalink
feat: add sitemap
Browse files Browse the repository at this point in the history
  • Loading branch information
cptchloroplast committed Dec 6, 2024
1 parent 09127b2 commit b3b85ef
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
37 changes: 37 additions & 0 deletions src/pages/sitemap.xml.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import type { Metadata, Navigation, Post, Tag } from "@schemas"
import { PostService } from "@services"
import type { APIContext } from "astro"

export async function GET(context: APIContext) {
const metadata = context.locals.metadata
const service = PostService(context.locals.runtime.env.DB)
const posts = await service.list()
const tags = await service.listTags()
return new Response(renderSitemap(context.url.origen, metadata, posts), {
headers: {
"Content-Type": "application/xml"
}
})
}

function renderSitemap(origen: string, metadata: Metadata, posts: Post[]) {
const map: Record<string, Date> = {}
const latest = posts[0].published!
const first = posts[posts.length - 1].published!
map[`${origen}`] = latest
map[`${origen}/rss.xml`] = latest
map[`${origen}/contact`] = first
map[`${origen}/posts`] = latest
for (const post of posts) {
map[`${origen}/posts/${post.slug}`] = post.updated ?? post.published!
}
const entries = Object.entries(map)
entries.sort(([loc1], [loc2]) => loc1 < loc2 ? -1 : 1)
return '<?xml version="1.0" encoding="UTF-8"?><urlset xmlns="http://www.sitemaps.org/schemas/sitemap/0.9">'
+ entries.map(([loc, lastmod]) => url(loc, lastmod)).join("")
+ "</urlset>"
}

function url(loc: string, lastmod: Date) {
return `<url><loc>${loc}</loc><lastmod>${lastmod.toISOString().split("T")[0]}</lastmod></url>`
}
4 changes: 2 additions & 2 deletions src/services/PostService.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import type { D1Database } from "@cloudflare/workers-types"
import { type Post, PostsTable, schema, TagsTable } from "@schemas"
import { type Post, PostsTable, schema, type Tag, TagsTable } from "@schemas"
import { conflictUpdateAllExcept } from "@utils"
import { asc, count, desc, eq, inArray, type InferSelectModel, and, isNotNull } from "drizzle-orm"
import { drizzle } from "drizzle-orm/d1"
Expand All @@ -10,7 +10,7 @@ type PostService = {
getLatest(): Promise<Post | undefined>
list(): Promise<Post[]>
listByTag(tag: string): Promise<Post[]>
listTags(): Promise<{ name: string, count: number }[]>
listTags(): Promise<{ name: Tag, count: number }[]>
upsert(value: Post): Promise<Post>
}
type PostRecord = InferSelectModel<typeof PostsTable> & { tags: InferSelectModel<typeof TagsTable>[] }
Expand Down

0 comments on commit b3b85ef

Please sign in to comment.








ApplySandwichStrip

pFad - (p)hone/(F)rame/(a)nonymizer/(d)eclutterfier!      Saves Data!


--- a PPN by Garber Painting Akron. With Image Size Reduction included!

Fetched URL: http://github.com/cptchloroplast/blog/commit/b3b85efa6f0ddded1d77384843dabf27a865ccbb

Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy