From ddbed7d075d35d2daf9697575431258e2d472f1c Mon Sep 17 00:00:00 2001 From: Visual-Dawg Date: Tue, 30 Jan 2024 22:17:28 +0100 Subject: [PATCH] fix RSS not showing in prod (#45) --- src/app.html | 2 +- src/lib/components/Footer.svelte | 2 +- src/lib/posts.js | 18 ++++++++++++++++++ src/routes/api/news/+server.js | 20 +------------------- src/routes/news/[slug]/+page.js | 2 -- src/routes/{rss => rss.xml}/+server.js | 12 +++++++----- 6 files changed, 28 insertions(+), 28 deletions(-) create mode 100644 src/lib/posts.js rename src/routes/{rss => rss.xml}/+server.js (77%) diff --git a/src/app.html b/src/app.html index 9fc88ed..a3a6687 100755 --- a/src/app.html +++ b/src/app.html @@ -5,7 +5,7 @@ - + %sveltekit.head% diff --git a/src/lib/components/Footer.svelte b/src/lib/components/Footer.svelte index c08d8a3..a694cbd 100755 --- a/src/lib/components/Footer.svelte +++ b/src/lib/components/Footer.svelte @@ -78,7 +78,7 @@
  • { + 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 +} diff --git a/src/routes/api/news/+server.js b/src/routes/api/news/+server.js index d92e827..c89e552 100644 --- a/src/routes/api/news/+server.js +++ b/src/routes/api/news/+server.js @@ -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) diff --git a/src/routes/news/[slug]/+page.js b/src/routes/news/[slug]/+page.js index 3e15cab..ebe8d6a 100644 --- a/src/routes/news/[slug]/+page.js +++ b/src/routes/news/[slug]/+page.js @@ -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`) diff --git a/src/routes/rss/+server.js b/src/routes/rss.xml/+server.js similarity index 77% rename from src/routes/rss/+server.js rename to src/routes/rss.xml/+server.js index be9be8e..bd76dd6 100644 --- a/src/routes/rss/+server.js +++ b/src/routes/rss.xml/+server.js @@ -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 ` - + ${siteTitle} News ${siteURL}/news ${siteDescription}