Skip to content

Commit bb3d258

Browse files
committed
blog fix
1 parent 23501fd commit bb3d258

File tree

4 files changed

+23
-17
lines changed

4 files changed

+23
-17
lines changed

src/routes/_libraries/blog.index.tsx

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,11 @@
11
import { Link, createFileRoute } from '@tanstack/react-router'
22

3-
import { formatAuthors } from '~/utils/blog'
3+
import { formatAuthors, getPublishedPosts } from '~/utils/blog'
44
import { Markdown } from '~/components/Markdown'
55
import { format } from 'date-fns'
66
import { Footer } from '~/components/Footer'
77
import { PostNotFound } from './blog'
88
import { createServerFn } from '@tanstack/react-start'
9-
import { allPosts } from 'content-collections'
109
import { setResponseHeaders } from '@tanstack/react-start/server'
1110

1211
const fetchFrontMatters = createServerFn({ method: 'GET' }).handler(
@@ -17,13 +16,7 @@ const fetchFrontMatters = createServerFn({ method: 'GET' }).handler(
1716
'public, max-age=300, durable, stale-while-revalidate=300',
1817
})
1918

20-
const now = new Date()
21-
22-
return allPosts
23-
.filter((post) => !post.draft && new Date(post.published) <= now)
24-
.sort((a, b) => {
25-
return new Date(b.published).getTime() - new Date(a.published).getTime()
26-
})
19+
return getPublishedPosts()
2720
.map((post) => {
2821
return {
2922
slug: post.slug,

src/routes/_libraries/index.tsx

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ import LandingPageGad from '~/components/LandingPageGad'
1515
import { MaintainerCard } from '~/components/MaintainerCard'
1616
import { coreMaintainers } from '~/libraries/maintainers'
1717
import { useToast } from '~/components/ToastProvider'
18-
import { allPosts } from 'content-collections'
19-
import { formatAuthors } from '~/utils/blog'
18+
import { formatAuthors, getPublishedPosts } from '~/utils/blog'
2019
import { format } from 'date-fns'
2120
import { Markdown } from '~/components/Markdown'
2221
import { createServerFn } from '@tanstack/react-start'
@@ -56,10 +55,7 @@ const fetchRecentPosts = createServerFn({ method: 'GET' }).handler(async () => {
5655
}),
5756
)
5857

59-
return allPosts
60-
.sort((a, b) => {
61-
return new Date(b.published).getTime() - new Date(a.published).getTime()
62-
})
58+
return getPublishedPosts()
6359
.slice(0, 3)
6460
.map((post) => {
6561
return {

src/server/feed/blog.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ export type BlogSyncResult = {
2222
*/
2323
export async function syncBlogPosts(): Promise<BlogSyncResult> {
2424
// Dynamic import to ensure content-collections are available in server environment
25-
const { allPosts } = await import('content-collections')
25+
const { getPublishedPosts } = await import('~/utils/blog')
2626

2727
const results: BlogSyncResult = {
2828
success: true,
@@ -32,7 +32,7 @@ export async function syncBlogPosts(): Promise<BlogSyncResult> {
3232
errors: [],
3333
}
3434

35-
for (const post of allPosts) {
35+
for (const post of getPublishedPosts()) {
3636
try {
3737
const entryId = `blog:${post.slug}`
3838
const publishedAt = new Date(post.published)

src/utils/blog.ts

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import { allPosts, type Post } from 'content-collections'
2+
13
const listJoiner = new Intl.ListFormat('en-US', {
24
style: 'long',
35
type: 'conjunction',
@@ -10,3 +12,18 @@ export function formatAuthors(authors: Array<string>) {
1012

1113
return listJoiner.format(authors)
1214
}
15+
16+
/**
17+
* Returns published blog posts (not drafts, not future-dated),
18+
* sorted by publish date descending (newest first).
19+
*/
20+
export function getPublishedPosts(): Post[] {
21+
const now = new Date()
22+
23+
return allPosts
24+
.filter((post) => !post.draft && new Date(post.published) <= now)
25+
.sort(
26+
(a, b) =>
27+
new Date(b.published).getTime() - new Date(a.published).getTime(),
28+
)
29+
}

0 commit comments

Comments
 (0)