From 3ff71d031b16c93aa9fc2e34788cd12270d4085f Mon Sep 17 00:00:00 2001 From: Swizzer Date: Sun, 13 Jul 2025 13:47:00 +0800 Subject: [PATCH] =?UTF-8?q?perf(content):=20=E2=9A=A1=EF=B8=8F=20reduce=20?= =?UTF-8?q?archive=20page=20size=20by=20stripping=20post=20body=20(#541)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * 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 --- src/pages/archive.astro | 6 +++--- src/utils/content-utils.ts | 24 ++++++++++++++++++++++-- 2 files changed, 25 insertions(+), 5 deletions(-) diff --git a/src/pages/archive.astro b/src/pages/archive.astro index 52ced049..90ede242 100644 --- a/src/pages/archive.astro +++ b/src/pages/archive.astro @@ -3,12 +3,12 @@ import ArchivePanel from "@components/ArchivePanel.svelte"; import I18nKey from "@i18n/i18nKey"; import { i18n } from "@i18n/translation"; 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(); --- - + diff --git a/src/utils/content-utils.ts b/src/utils/content-utils.ts index a39a02f6..f30b8903 100644 --- a/src/utils/content-utils.ts +++ b/src/utils/content-utils.ts @@ -1,9 +1,10 @@ -import { getCollection } from "astro:content"; +import { type CollectionEntry, getCollection } from "astro:content"; import I18nKey from "@i18n/i18nKey"; import { i18n } from "@i18n/translation"; 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 }) => { return import.meta.env.PROD ? data.draft !== true : true; }); @@ -13,6 +14,11 @@ export async function getSortedPosts() { const dateB = new Date(b.data.published); return dateA > dateB ? -1 : 1; }); + return sorted; +} + +export async function getSortedPosts() { + const sorted = await getRawSortedPosts(); for (let i = 1; i < sorted.length; i++) { sorted[i].data.nextSlug = sorted[i - 1].slug; @@ -25,7 +31,21 @@ export async function getSortedPosts() { return sorted; } +export type PostForList = { + slug: string; + data: CollectionEntry<"posts">["data"]; +}; +export async function getSortedPostsList(): Promise { + const sortedFullPosts = await getRawSortedPosts(); + // delete post.body + const sortedPostsList = sortedFullPosts.map((post) => ({ + slug: post.slug, + data: post.data, + })); + + return sortedPostsList; +} export type Tag = { name: string; count: number;