trevortylerlee / astro-micro

Blog theme for Astro with search and comments built-in. Zero frameworks.
https://astro-micro.vercel.app
MIT License
203 stars 47 forks source link

Prev / Next post buttons do not advance chronologically, but by slug or folder name. #26

Closed cgranier closed 5 months ago

cgranier commented 5 months ago

Even though the /blog page sorts the posts correctly by date, once you navigate to a post the NextPostand PrevPost buttons do not function as expected.

To fix this, make the following change in src/pages/blog/[...slug].astro:

const posts = await getCollection("blog");

should be:

const posts = (await getCollection("blog"))
  .filter((post) => !post.data.draft)
  .sort((a, b) => b.data.date.valueOf() - a.data.date.valueOf());

This way, you're reading from a chronological list of posts.

cgranier commented 5 months ago

To replicate, create a new post under src/content/blog:

  1. Create a folder named 08-new-post (to follow the current system of folder names) and then create an index.md file inside this folder with the following content:
---
title: "MARCH 18 2024"
description: "."
date: "2024-03-18"
draft: false
---

MARCH 18 2024
  1. Rebuild the site and notice that NextPost/PrevPost will place this new article at the end of the list, even though it will appear listed correctly under the blog page listing.