performance improvements

This commit is contained in:
2026-05-14 17:52:13 +02:00
parent 6bc51d6d14
commit 046f60dcb6
9 changed files with 299 additions and 134 deletions
+12 -19
View File
@@ -7,6 +7,10 @@ const { slug } = Astro.params;
const API_URL = (typeof process !== 'undefined' ? process.env.PUBLIC_API_URL : import.meta.env.PUBLIC_API_URL) || 'http://localhost:3000';
interface CoverImage { url: string; alt: string }
interface PostNeighbor {
slug: string;
title?: string;
}
interface PostDetail {
slug: string;
date: string;
@@ -18,10 +22,8 @@ interface PostDetail {
reading_time: number;
cover_image?: CoverImage;
image_count: number;
}
interface PostInfo {
slug: string;
title?: string;
prev?: PostNeighbor;
next?: PostNeighbor;
}
function formatDate(d: string) {
@@ -36,35 +38,26 @@ function formatSlug(s: string) {
let post: PostDetail | null = null;
let html = '';
let error = '';
let neighbors: { prev?: PostInfo; next?: PostInfo } = {};
try {
const [postRes, listRes] = await Promise.all([
fetch(`${API_URL}/api/posts/${encodeURIComponent(slug ?? '')}`),
fetch(`${API_URL}/api/posts`),
]);
const postRes = await fetch(`${API_URL}/api/posts/${encodeURIComponent(slug ?? '')}`);
if (postRes.ok) {
post = await postRes.json();
html = renderMarkdown(post!.content);
} else {
error = 'Work not found in the catalogue';
}
if (listRes.ok) {
const list: PostInfo[] = await listRes.json();
const i = list.findIndex(p => p.slug === slug);
if (i >= 0) {
neighbors = {
prev: i > 0 ? list[i - 1] : undefined,
next: i < list.length - 1 ? list[i + 1] : undefined,
};
}
}
} catch (e) {
const cause = (e as any)?.cause;
error = `Could not connect to backend at ${API_URL}: ${e instanceof Error ? e.message : String(e)}${cause ? ' (Cause: ' + (cause.message || cause.code || JSON.stringify(cause)) + ')' : ''}`;
console.error(error);
}
const neighbors = {
prev: post?.prev,
next: post?.next,
};
const isAdmin = Astro.cookies.get('admin_session')?.value === '1';
const displayTitle = post ? (post.title || formatSlug(post.slug)) : 'Work';
---