performance improvements
This commit is contained in:
@@ -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';
|
||||
---
|
||||
|
||||
@@ -8,10 +8,16 @@ if (!response.ok) {
|
||||
return new Response(null, { status: 404 });
|
||||
}
|
||||
|
||||
return new Response(await response.blob(), {
|
||||
headers: {
|
||||
'content-type': response.headers.get('content-type') || 'application/octet-stream',
|
||||
'cache-control': 'public, max-age=3600'
|
||||
}
|
||||
});
|
||||
const headers = new Headers();
|
||||
const contentType = response.headers.get('content-type');
|
||||
if (contentType) headers.set('content-type', contentType);
|
||||
const contentLength = response.headers.get('content-length');
|
||||
if (contentLength) headers.set('content-length', contentLength);
|
||||
const etag = response.headers.get('etag');
|
||||
if (etag) headers.set('etag', etag);
|
||||
const lastModified = response.headers.get('last-modified');
|
||||
if (lastModified) headers.set('last-modified', lastModified);
|
||||
headers.set('cache-control', 'public, max-age=3600');
|
||||
|
||||
return new Response(response.body, { headers });
|
||||
---
|
||||
|
||||
Reference in New Issue
Block a user