fix RSS not showing in prod (#45)

This commit is contained in:
Visual-Dawg 2024-01-30 22:17:28 +01:00 committed by GitHub
parent 821d96b466
commit ddbed7d075
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
6 changed files with 28 additions and 28 deletions

View file

@ -5,7 +5,7 @@
<link rel="icon" href="%sveltekit.assets%/favicon.ico" />
<meta name="viewport" content="width=device-width" />
<meta property="og:image" content="/imgs/og-img.png" />
<link rel="alternate" type="application/rss+xml" title="Hyprland News" href="/rss" />
<link rel="alternate" type="application/rss+xml" title="Hyprland News" href="/rss.xml" />
<style></style>
%sveltekit.head%
</head>

View file

@ -78,7 +78,7 @@
</li>
<li class="">
<a
href="/rss"
href="/rss.xml"
class="text-slate-400 hover:text-slate-200"
target="_blank"
aria-label="Rss Feed"><RssIcon class="h-12 w-12 " /></a

18
src/lib/posts.js Normal file
View file

@ -0,0 +1,18 @@
export async function getNews() {
const posts = Object.entries(import.meta.glob('/src/content/news/*.md', { eager: true }))
.flatMap(([path, { metadata }]) => {
const slug = path.split('/').at(-1)?.replace('.md', '')
const rawDate = metadata.date
const date = new Date(typeof rawDate === 'number' ? rawDate * 1000 : rawDate).getTime()
if (!slug || !path || Number.isNaN(date)) {
console.error(`Invalid file ${path} ${JSON.stringify({ ...metadata, date, slug })}`)
return []
}
return { slug, ...metadata, date }
})
.sort(({ date: a }, { date: b }) => new Date(b).getTime() - new Date(a).getTime())
return posts
}

View file

@ -1,24 +1,6 @@
import { getNews } from '$lib/posts'
import { json } from '@sveltejs/kit'
async function getNews() {
const posts = Object.entries(import.meta.glob('/src/content/news/*.md', { eager: true }))
.flatMap(([path, { metadata }]) => {
const slug = path.split('/').at(-1)?.replace('.md', '')
const rawDate = metadata.date
const date = new Date(typeof rawDate === 'number' ? rawDate * 1000 : rawDate).getTime()
if (!slug || !path || Number.isNaN(date)) {
console.error(`Invalid file ${path} ${JSON.stringify({ ...metadata, date, slug })}`)
return []
}
return { slug, ...metadata, date }
})
.sort(({ date: a }, { date: b }) => new Date(b).getTime() - new Date(a).getTime())
return posts
}
export async function GET() {
const news = await getNews()
return json(news)

View file

@ -1,5 +1,3 @@
import { error } from '@sveltejs/kit'
export async function load({ params, fetch }) {
try {
const post = await import(`../../../content/news/${params.slug}.md`)

View file

@ -1,13 +1,15 @@
import { getNews } from '$lib/posts'
const siteURL = 'https://hyprland.org'
const siteTitle = 'Hyprland'
const siteDescription = 'Tiling window manager with the looks'
export const prerender = true
export const GET = async ({ fetch }) => {
const allNews = await fetch('api/news')
.then((response) => response.json())
.then((news) => news.sort((a, b) => new Date(b.date) - new Date(a.date)))
export const GET = async () => {
const allNews = await getNews().then((news) =>
news.sort((a, b) => new Date(b.date) - new Date(a.date))
)
const body = renderXml(allNews)
const options = {
@ -24,7 +26,7 @@ function renderXml(posts) {
return `<?xml version="1.0" encoding="UTF-8" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
<channel>
<atom:link href="${siteURL}/rss" rel="self" type="application/rss+xml" />
<atom:link href="${siteURL}/rss.xml" rel="self" type="application/rss+xml" />
<title>${siteTitle} News</title>
<link>${siteURL}/news</link>
<description>${siteDescription}</description>