Skip to content

Commit 66f2538

Browse files
committed
Stop caching 404s for newly published posts.
Look up missing slugs live so a first visit after publish can render the article.
1 parent 7f5ce2a commit 66f2538

2 files changed

Lines changed: 23 additions & 11 deletions

File tree

app/posts/[title]/page.tsx

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,8 @@ type RouteParams = {
2424
title: string;
2525
};
2626

27-
export const revalidate = 300;
28-
29-
export async function generateStaticParams() {
30-
const articles = await getPublishedArticles();
31-
return articles.map((article) => ({
32-
title: article.slug,
33-
}));
34-
}
27+
export const dynamic = "force-dynamic";
28+
export const dynamicParams = true;
3529

3630
function buildKeywords(
3731
articleTitle: string,

lib/content.ts

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,17 @@ async function getPublicAuthorDocuments() {
357357
}
358358
}
359359

360-
async function getPublishedArticleDocuments(): Promise<ContentDocument[]> {
360+
function normalizeRouteSlug(slug: string) {
361+
try {
362+
return decodeURIComponent(slug).trim();
363+
} catch {
364+
return slug.trim();
365+
}
366+
}
367+
368+
async function getPublishedArticleDocuments(
369+
revalidateSeconds: number | false = 300,
370+
): Promise<ContentDocument[]> {
361371
const { apiKey, projectId } = getRequiredFirebaseConfig();
362372
const params = new URLSearchParams({ key: apiKey });
363373
const response = await fetch(
@@ -379,7 +389,9 @@ async function getPublishedArticleDocuments(): Promise<ContentDocument[]> {
379389
},
380390
},
381391
}),
382-
next: { revalidate: 300 },
392+
...(revalidateSeconds === false
393+
? { cache: "no-store" as const }
394+
: { next: { revalidate: revalidateSeconds } }),
383395
},
384396
);
385397

@@ -532,10 +544,16 @@ export async function getPublishedArticles(limitCount?: number) {
532544
}
533545

534546
export async function getPublishedArticleBySlug(slug: string) {
547+
const normalizedSlug = normalizeRouteSlug(slug);
535548
const authors = await getAllAuthors();
536549
const authorLookup = createAuthorLookup(authors);
537550
const documents = await getPublishedArticleDocuments();
538-
const document = documents.find((entry) => entry.data.slug === slug);
551+
const cached = documents.find((entry) => entry.data.slug === normalizedSlug);
552+
const document =
553+
cached ||
554+
(await getPublishedArticleDocuments(false)).find(
555+
(entry) => entry.data.slug === normalizedSlug,
556+
);
539557

540558
if (!document) return null;
541559

0 commit comments

Comments
 (0)