perf(content): ️ reduce archive page size by stripping post body (#541)
Some checks failed
Code quality / quality (push) Failing after 9s
Build and Check / Astro Check for Node.js 22 (push) Failing after 6s
Build and Check / Astro Check for Node.js 23 (push) Failing after 5s
Build and Check / Astro Build for Node.js 22 (push) Failing after 5s
Build and Check / Astro Build for Node.js 23 (push) Failing after 13s

* perf(content): ️ reduce archive page size by stripping post body

* apply biome fix

* refactor: Separate function responsibilities (removed unnecessary linkage of preceding and following slugs)

---------

Co-authored-by: L4Ph <me@l4ph.moe>
This commit is contained in:
Swizzer
2025-07-13 13:47:00 +08:00
committed by GitHub
parent 297db1384c
commit 3ff71d031b
2 changed files with 25 additions and 5 deletions

View File

@@ -3,12 +3,12 @@ import ArchivePanel from "@components/ArchivePanel.svelte";
import I18nKey from "@i18n/i18nKey"; import I18nKey from "@i18n/i18nKey";
import { i18n } from "@i18n/translation"; import { i18n } from "@i18n/translation";
import MainGridLayout from "@layouts/MainGridLayout.astro"; import MainGridLayout from "@layouts/MainGridLayout.astro";
import { getSortedPosts } from "../utils/content-utils"; import { getSortedPostsList } from "../utils/content-utils";
const sortedPosts = await getSortedPosts(); const sortedPostsList = await getSortedPostsList();
--- ---
<MainGridLayout title={i18n(I18nKey.archive)}> <MainGridLayout title={i18n(I18nKey.archive)}>
<ArchivePanel sortedPosts={sortedPosts} client:only="svelte"></ArchivePanel> <ArchivePanel sortedPosts={sortedPostsList} client:only="svelte"></ArchivePanel>
</MainGridLayout> </MainGridLayout>

View File

@@ -1,9 +1,10 @@
import { getCollection } from "astro:content"; import { type CollectionEntry, getCollection } from "astro:content";
import I18nKey from "@i18n/i18nKey"; import I18nKey from "@i18n/i18nKey";
import { i18n } from "@i18n/translation"; import { i18n } from "@i18n/translation";
import { getCategoryUrl } from "@utils/url-utils.ts"; import { getCategoryUrl } from "@utils/url-utils.ts";
export async function getSortedPosts() { // // Retrieve posts and sort them by publication date
async function getRawSortedPosts() {
const allBlogPosts = await getCollection("posts", ({ data }) => { const allBlogPosts = await getCollection("posts", ({ data }) => {
return import.meta.env.PROD ? data.draft !== true : true; return import.meta.env.PROD ? data.draft !== true : true;
}); });
@@ -13,6 +14,11 @@ export async function getSortedPosts() {
const dateB = new Date(b.data.published); const dateB = new Date(b.data.published);
return dateA > dateB ? -1 : 1; return dateA > dateB ? -1 : 1;
}); });
return sorted;
}
export async function getSortedPosts() {
const sorted = await getRawSortedPosts();
for (let i = 1; i < sorted.length; i++) { for (let i = 1; i < sorted.length; i++) {
sorted[i].data.nextSlug = sorted[i - 1].slug; sorted[i].data.nextSlug = sorted[i - 1].slug;
@@ -25,7 +31,21 @@ export async function getSortedPosts() {
return sorted; return sorted;
} }
export type PostForList = {
slug: string;
data: CollectionEntry<"posts">["data"];
};
export async function getSortedPostsList(): Promise<PostForList[]> {
const sortedFullPosts = await getRawSortedPosts();
// delete post.body
const sortedPostsList = sortedFullPosts.map((post) => ({
slug: post.slug,
data: post.data,
}));
return sortedPostsList;
}
export type Tag = { export type Tag = {
name: string; name: string;
count: number; count: number;