fix: show 404 when post doesn't exist

This commit is contained in:
Lio 2025-11-19 10:38:04 +01:00
parent 34ddae165b
commit aaeb92da9f
6 changed files with 9 additions and 12 deletions

View file

@ -8,8 +8,7 @@ const lang = getLangFromUrl(Astro.url)
const t = useTranslations(lang) const t = useTranslations(lang)
const allPosts = await getPostsByLocale(lang) const allPosts = await getPostsByLocale(lang)
const posts = allPosts const posts = allPosts.slice(0, common.latestPosts)
.slice(0, common.latestPosts)
--- ---
<div class="my-8 text-xl font-medium md:my-8"> <div class="my-8 text-xl font-medium md:my-8">

View file

@ -4,6 +4,7 @@ pubDate: 2025-11-18
description: "This is the first post of my new Astro blog." description: "This is the first post of my new Astro blog."
author: "@lio@pounced-on.me" author: "@lio@pounced-on.me"
tags: ["furries", "making-friends"] tags: ["furries", "making-friends"]
# published: true
--- ---
meow meow

View file

@ -16,16 +16,16 @@ const urls = {
general: `/images/general/ogImage.png`, general: `/images/general/ogImage.png`,
} }
const isPost = Astro.url.pathname.includes("posts") const isPost = post
const ogImageURL = new URL(isPost ? urls.postURL : urls.general, Astro.site) const ogImageURL = new URL(post ? urls.postURL : urls.general, Astro.site)
.href .href
const permalink = new URL(Astro.url.pathname, Astro.site).href const permalink = new URL(Astro.url.pathname, Astro.site).href
const config = lang === "de" ? de : en const config = lang === "de" ? de : en
const title = isPost const title = post
? `${post.title} - ${config.meta.title}` ? `${post.title} - ${config.meta.title}`
: `${config.meta.description} - ${config.meta.title}` : `${config.meta.description} - ${config.meta.title}`
const description = isPost ? post.description : config.meta.description const description = post ? post.description : config.meta.description
--- ---
<!doctype html> <!doctype html>

View file

@ -18,8 +18,7 @@ export async function GET(request: { url: URL }) {
return rss({ return rss({
title: config.meta.title, title: config.meta.title,
description: config.meta.description, description: config.meta.description,
site: site: config.meta.url,
config.meta.url,
items: posts.map((post: any) => ({ items: posts.map((post: any) => ({
title: post.data.title, title: post.data.title,
description: post.data.description, description: post.data.description,

View file

@ -9,8 +9,7 @@ export async function GET() {
return rss({ return rss({
title: config.meta.title, title: config.meta.title,
description: config.meta.description, description: config.meta.description,
site: site: config.meta.url,
config.meta.url,
items: posts.map((post: any) => ({ items: posts.map((post: any) => ({
title: post.data.title, title: post.data.title,
description: post.data.description, description: post.data.description,

View file

@ -35,11 +35,10 @@ export const getPostsByLocale = async (locale: string) => {
: await getCollection("dePosts") : await getCollection("dePosts")
// Add the locale to the data of each post // Add the locale to the data of each post
posts.forEach((post: any) => { posts.forEach((post: any) => {
post.data.lang = locale post.data.lang = locale
}) })
const filtered = posts.filter(p => p.data.published) const filtered = posts.filter((p) => p.data.published)
return filtered.sort( return filtered.sort(
(a: any, b: any) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf(), (a: any, b: any) => b.data.pubDate.valueOf() - a.data.pubDate.valueOf(),
) )