Skip to content

Commit

Permalink
improve: use sort by publish date (#66)
Browse files Browse the repository at this point in the history
* improve: use sort by publish date

Signed-off-by: james-a-morris <[email protected]>

* fix: recent articles in homepage

Signed-off-by: james-a-morris <[email protected]>

---------

Signed-off-by: james-a-morris <[email protected]>
  • Loading branch information
james-a-morris committed Oct 1, 2024
1 parent 940ff00 commit c679fb9
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 4 deletions.
1 change: 1 addition & 0 deletions src/app/(routes)/blog/page.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export default async function BlogHomePage({ searchParams }: PageProps) {
const recentArticleSlugs = await retrieveContentfulPublishedSlugs({
limit: 6,
avoidTags: ["get-started"],
sortByRecent: true,
});
const getStartedSlugs = await retrieveContentfulPublishedSlugs({
limit: 3,
Expand Down
9 changes: 5 additions & 4 deletions src/app/_lib/contentful.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,11 +50,13 @@ export async function retrieveContentfulPublishedSlugs({
limit,
avoidTags,
includeTags,
sortByRecent,
}: {
query?: string;
limit?: number;
avoidTags?: string[];
includeTags?: string[];
sortByRecent?: boolean;
} = {}): Promise<string[]> {
const client = getProductionClient();
const options = {
Expand All @@ -66,6 +68,7 @@ export async function retrieveContentfulPublishedSlugs({
...(query ? { query } : {}),
...(avoidTags ? { "fields.tag[nin]": avoidTags.join(",").toLowerCase() } : {}),
...(includeTags ? { "fields.tag[in]": includeTags.join(",").toLowerCase() } : {}),
...(sortByRecent ? { order: "-fields.publishDate" } : {}),
} as const;
const entries =
await client.withoutUnresolvableLinks.getEntries<TypeAcrossBlogPostSkeleton>(options);
Expand Down Expand Up @@ -113,10 +116,8 @@ export async function retrieveRelevantContentfulEntries(
"fields.tag[in]": tags.join(",").toLowerCase(), // get posts with same tags
"fields.slug[nin]": entrySlugId, // don't include current post
"fields.slug[exists]": true, // no empty slugs

// TODO: Update existing posts to have publishDate
// "fields.publishDate[exists]": true, // no empty dates
// order: "-fields.publishDate", // sorted latest first
"fields.publishDate[exists]": true, // no empty dates
order: "-fields.publishDate", // sorted latest first
} as const;
const entries =
await client.withoutUnresolvableLinks.getEntries<TypeAcrossBlogPostSkeleton>(options);
Expand Down

0 comments on commit c679fb9

Please sign in to comment.