Merge pull request #4 from damjan1996/auto-claude/025-implement-blog-post-caching-and-lazy-loading
auto-claude: 025-implement-blog-post-caching-and-lazy-loading
This commit is contained in:
+15
-1
@@ -1,5 +1,6 @@
|
||||
import fs from 'fs';
|
||||
import path from 'path';
|
||||
import { cache } from '@/utils/cache';
|
||||
|
||||
export interface BlogPost {
|
||||
slug: string;
|
||||
@@ -130,6 +131,14 @@ function parseMarkdownPost(filename: string, content: string): BlogPost | null {
|
||||
}
|
||||
|
||||
export function getAllBlogPosts(): BlogPost[] {
|
||||
const cacheKey = 'blog:all-posts';
|
||||
|
||||
// Check cache first
|
||||
const cachedPosts = cache.get<BlogPost[]>(cacheKey);
|
||||
if (cachedPosts) {
|
||||
return cachedPosts;
|
||||
}
|
||||
|
||||
if (!fs.existsSync(BLOG_POSTS_DIR)) {
|
||||
console.warn('Blog posts directory not found:', BLOG_POSTS_DIR);
|
||||
return [];
|
||||
@@ -157,7 +166,12 @@ export function getAllBlogPosts(): BlogPost[] {
|
||||
}
|
||||
|
||||
// Sort by date descending (newest first)
|
||||
return posts.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
|
||||
const sortedPosts = posts.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
|
||||
|
||||
// Cache the results
|
||||
cache.set(cacheKey, sortedPosts);
|
||||
|
||||
return sortedPosts;
|
||||
}
|
||||
|
||||
export function getBlogPostBySlug(slug: string): BlogPost | null {
|
||||
|
||||
Reference in New Issue
Block a user