Skip to content

Commit d53c43b

Browse files
committed
Add caching strategies blog
Signed-off-by: Amrish Kushwaha <askmaurya48@gmail.com>
1 parent 719e1f9 commit d53c43b

File tree

3 files changed

+50
-30
lines changed

3 files changed

+50
-30
lines changed

app/blog/[slug]/page.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,9 @@ export async function generateMetadata({
124124

125125
export function generateStaticParams() {
126126
const blogs = getAllBlogsMetadata();
127-
return blogs.map(blog => ({ slug: blog.slug }));
127+
return blogs
128+
?.filter((blog) => !blog?.draft)
129+
.map((blog) => ({ slug: blog.slug }));
128130
}
129131

130132
export const dynamicParams = false;
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
---
2+
title: "Caching Strategies"
3+
date: "2025-07-14"
4+
category: "cache"
5+
tags: [ "Caching", "Production Ready"]
6+
draft: true
7+
---
8+
9+
# This blog explains in detail about the caching strategies

lib/blogs.ts

Lines changed: 38 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -8,68 +8,74 @@ export interface BlogMetadata {
88
category: string;
99
tags: string[];
1010
slug: string;
11+
draft?: boolean; // Optional field to indicate if the blog is a draft
1112
filename: string; // Add filename to track the actual file
1213
}
1314

1415
export function getBlogMetadata(slug: string): BlogMetadata {
1516
// First try to find by custom slug in frontmatter
16-
const blogsDirectory = path.join(process.cwd(), 'content/blog');
17-
const filenames = fs.readdirSync(blogsDirectory).filter(name => name.endsWith('.mdx'));
18-
17+
const blogsDirectory = path.join(process.cwd(), "content/blog");
18+
const filenames = fs
19+
.readdirSync(blogsDirectory)
20+
.filter((name) => name.endsWith(".mdx"));
21+
1922
let targetFile = null;
2023
let data: any = {};
21-
24+
2225
// Look for file with matching custom slug in frontmatter
2326
for (const filename of filenames) {
2427
const filePath = path.join(blogsDirectory, filename);
25-
const contents = fs.readFileSync(filePath, 'utf8');
28+
const contents = fs.readFileSync(filePath, "utf8");
2629
const { data: frontmatter } = matter(contents);
27-
28-
if (frontmatter.slug === slug || filename.replace(/\.mdx$/, '') === slug) {
30+
31+
if (frontmatter.slug === slug || filename.replace(/\.mdx$/, "") === slug) {
2932
targetFile = filename;
3033
data = frontmatter;
3134
break;
3235
}
3336
}
34-
37+
3538
if (!targetFile) {
3639
throw new Error(`Blog with slug "${slug}" not found`);
3740
}
38-
41+
3942
return {
4043
title: data.title,
4144
date: data.date,
4245
category: data.category,
4346
tags: data.tags || [],
4447
slug: data.slug || slug,
45-
filename: targetFile.replace(/\.mdx$/, ''),
48+
filename: targetFile.replace(/\.mdx$/, ""),
49+
draft: data?.draft || false, // Use optional chaining to handle missing draft field
4650
};
4751
}
4852

4953
export function getAllBlogsMetadata(): BlogMetadata[] {
50-
const blogsDirectory = path.join(process.cwd(), 'content/blog');
54+
const blogsDirectory = path.join(process.cwd(), "content/blog");
5155
const filenames = fs.readdirSync(blogsDirectory);
52-
56+
5357
return filenames
54-
.filter(name => name.endsWith('.mdx'))
55-
.map(name => {
56-
const defaultSlug = name.replace(/\.mdx$/, '');
58+
.filter((name) => name.endsWith(".mdx"))
59+
.map((name) => {
60+
const defaultSlug = name.replace(/\.mdx$/, "");
5761
const filePath = path.join(blogsDirectory, name);
58-
const fileContents = fs.readFileSync(filePath, 'utf8');
62+
const fileContents = fs.readFileSync(filePath, "utf8");
5963
const { data } = matter(fileContents);
60-
64+
6165
// Use custom slug from frontmatter if available, otherwise use filename
6266
const slug = data.slug || defaultSlug;
63-
67+
6468
return {
6569
title: data.title,
6670
date: data.date,
6771
category: data.category,
6872
tags: data.tags || [],
6973
slug,
7074
filename: defaultSlug,
75+
draft: data.draft || false,
7176
};
7277
})
78+
?.filter((blog) => blog?.draft !== true) // Exclude drafts
7379
.sort((a, b) => new Date(b.date).getTime() - new Date(a.date).getTime());
7480
}
7581

@@ -79,38 +85,41 @@ export interface BlogContent extends BlogMetadata {
7985

8086
export function getContent(slug: string): BlogContent {
8187
// First try to find by custom slug in frontmatter
82-
const blogsDirectory = path.join(process.cwd(), 'content/blog');
83-
const filenames = fs.readdirSync(blogsDirectory).filter(name => name.endsWith('.mdx'));
84-
88+
const blogsDirectory = path.join(process.cwd(), "content/blog");
89+
const filenames = fs
90+
.readdirSync(blogsDirectory)
91+
.filter((name) => name.endsWith(".mdx"));
92+
8593
let targetFile = null;
8694
let data: any = {};
87-
let content = '';
88-
95+
let content = "";
96+
8997
// Look for file with matching custom slug in frontmatter
9098
for (const filename of filenames) {
9199
const filePath = path.join(blogsDirectory, filename);
92-
const contents = fs.readFileSync(filePath, 'utf8');
100+
const contents = fs.readFileSync(filePath, "utf8");
93101
const { data: frontmatter, content: mdxContent } = matter(contents);
94-
95-
if (frontmatter.slug === slug || filename.replace(/\.mdx$/, '') === slug) {
102+
103+
if (frontmatter.slug === slug || filename.replace(/\.mdx$/, "") === slug) {
96104
targetFile = filename;
97105
data = frontmatter;
98106
content = mdxContent;
99107
break;
100108
}
101109
}
102-
110+
103111
if (!targetFile) {
104112
throw new Error(`Blog with slug "${slug}" not found`);
105113
}
106-
114+
107115
return {
108116
title: data.title,
109117
date: data.date,
110118
category: data.category,
111119
tags: data.tags || [],
112120
slug: data.slug || slug,
113-
filename: targetFile.replace(/\.mdx$/, ''),
121+
filename: targetFile.replace(/\.mdx$/, ""),
122+
draft: data?.draft || false,
114123
content,
115124
};
116125
}

0 commit comments

Comments
 (0)