forked from nuotsu/sanitypress
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathroute.ts
50 lines (45 loc) · 940 Bytes
/
route.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
import RSS from 'rss'
import { fetchSanity } from '@/lib/sanity'
import groq from 'groq'
import processUrl from '@/lib/processUrl'
export async function GET() {
const { blog, posts } = await fetchSanity<{
blog: Sanity.Page
posts: Sanity.BlogPost[]
}>(
groq`{
'blog': *[_type == 'page' && metadata.slug.current == 'blog'][0]{
_type,
title,
metadata
},
'posts': *[_type == 'blog.post']{
_type,
title,
publishDate,
metadata
}
}`,
{ tags: ['blog-rss'] },
)
const url = processUrl(blog)
const feed = new RSS({
title: `${blog.title}`,
site_url: url,
feed_url: `${url}/rss.xml`,
language: 'en',
})
posts.map((post) =>
feed.item({
title: post.title,
url: processUrl(post),
date: post.publishDate,
description: post.metadata.description,
}),
)
return new Response(feed.xml({ indent: true }), {
headers: {
'Content-Type': 'application/atom+xml',
},
})
}