-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathblog.ts
More file actions
22 lines (19 loc) · 715 Bytes
/
blog.ts
File metadata and controls
22 lines (19 loc) · 715 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
import type { CollectionEntry } from 'astro:content';
import { getCollection } from 'astro:content';
/**
* Cached blog posts collection.
* Since Astro pre-renders pages at build time, this module-level cache
* ensures the collection is fetched only once during the build process,
* avoiding redundant queries across multiple page renders.
*/
let cachedPosts: CollectionEntry<'blog'>[] | null = null;
/**
* Get all blog posts with caching.
* Results are cached at the module level to avoid repeated collection fetches.
*/
export async function getAllBlogPosts(): Promise<CollectionEntry<'blog'>[]> {
if (cachedPosts === null) {
cachedPosts = await getCollection('blog');
}
return cachedPosts;
}