Skip to content

Commit 3718ba2

Browse files
committed
fix: update Posts component to evaluate "New" badge conditionally based on client-side state
1 parent 95c421b commit 3718ba2

2 files changed

Lines changed: 8 additions & 1 deletion

File tree

src/components/app/blog/Posts.tsx

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import classNames from "classnames";
2+
import { useEffect, useState } from "preact/hooks";
23
import { useTranslation } from "~/i18n/context";
34

45
import { type Frontmatter } from "~/types/frontmatter";
@@ -17,6 +18,11 @@ const toNodeId = (index: number) =>
1718

1819
const Posts = ({ posts }: Props) => {
1920
const { t } = useTranslation();
21+
// isNew uses new Date() — must be evaluated client-side only so the static
22+
// build doesn't bake in a stale "today" value that never updates on GitHub Pages.
23+
const [mounted, setMounted] = useState(false);
24+
useEffect(() => setMounted(true), []);
25+
const checkIsNew = (date?: string) => mounted && isNew(date);
2026
const visiblePosts =
2127
posts?.filter(
2228
(post) => !(post.hidden && process.env.NODE_ENV !== "development")
@@ -38,7 +44,7 @@ const Posts = ({ posts }: Props) => {
3844
>
3945
<div className="mb-7 flex items-center justify-between gap-4 font-mono text-xs uppercase tracking-[0.18em] text-outline">
4046
<div className="inline-flex items-center gap-3">
41-
{isNew(featuredPost.date) && (
47+
{checkIsNew(featuredPost.date) && (
4248
<span className="rounded bg-secondary/18 px-2 py-1 font-mono text-xs font-semibold uppercase tracking-[0.16em] text-secondary">
4349
{t("blog.posts.newBadge")}
4450
</span>

src/pages/posts/index.astro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,6 @@ posts.sort((a, b) => Date.parse(b.data.date) - Date.parse(a.data.date));
2929
</section>
3030
<Posts
3131
posts={posts.map((post) => ({ ...post.data, slug: `/posts/${post.id}` } as PostFrontmatter))}
32+
client:visible
3233
/>
3334
</DefaultPageLayout>

0 commit comments

Comments
 (0)