perf: aggregate homepage data and trim frontend loading

This commit is contained in:
2026-03-31 00:25:58 +08:00
parent 99b308e800
commit a9a05aa105
4 changed files with 137 additions and 18 deletions

View File

@@ -132,6 +132,14 @@ export interface ApiSiteSettings {
paragraph_comments_enabled: boolean;
}
export interface ApiHomePagePayload {
site_settings: ApiSiteSettings;
posts: ApiPost[];
tags: ApiTag[];
friend_links: ApiFriendLink[];
categories: ApiCategory[];
}
export interface AiSource {
slug: string;
href: string;
@@ -398,8 +406,22 @@ class ApiClient {
}
async getPostBySlug(slug: string): Promise<UiPost | null> {
const posts = await this.getPosts();
return posts.find(post => post.slug === slug) || null;
const response = await fetch(`${this.baseUrl}/posts/slug/${encodeURIComponent(slug)}`, {
headers: {
'Content-Type': 'application/json',
},
});
if (response.status === 404) {
return null;
}
if (!response.ok) {
const errorText = await response.text().catch(() => '');
throw new Error(errorText || `API error: ${response.status} ${response.statusText}`);
}
return normalizePost((await response.json()) as ApiPost);
}
async getComments(
@@ -487,6 +509,24 @@ class ApiClient {
return normalizeSiteSettings(settings);
}
async getHomePageData(): Promise<{
siteSettings: SiteSettings;
posts: UiPost[];
tags: UiTag[];
friendLinks: AppFriendLink[];
categories: UiCategory[];
}> {
const payload = await this.fetch<ApiHomePagePayload>('/site_settings/home');
return {
siteSettings: normalizeSiteSettings(payload.site_settings),
posts: payload.posts.map(normalizePost),
tags: payload.tags.map(normalizeTag),
friendLinks: payload.friend_links.map(normalizeFriendLink),
categories: payload.categories.map(normalizeCategory),
};
}
async getCategories(): Promise<UiCategory[]> {
const categories = await this.fetch<ApiCategory[]>('/categories');
return categories.map(normalizeCategory);