#Building a blog website, previous/next blog post button question

1 messages · Page 1 of 1 (latest)

wild birch
#

Hello,

I decided to give Astro JS a try to rebuild my wife's blog website with.

I however can't seem to figure out how to create next / previous buttons (which include the next blog title etc).

I tried the following in [...slug].astro :

---
import { type CollectionEntry, getCollection } from "astro:content";
import BlogPost from "../../layouts/Blogpost.astro";
import { sortByDate } from "../../utils/sortByDate";

export async function getStaticPaths() {
  const posts = await getCollection("blog");
  const numberOfPosts = posts.length;
  
  return posts.sort(sortByDate).map((post, i) => ({
    params: { slug: post.slug },
    props: {
      post,
      prevPost: i + 1 === numberOfPosts ? null : posts[i + 1],
      nextPost: i === 0 ? null : posts[i - 1],
    },
  }));
}
type Props = CollectionEntry<"blog">;

const props = Astro.props;
const { Content } = await props.render();

export const prerender = true;
---

<BlogPost {...props.data}>
  <Content />
</BlogPost>

I then run into a bunch of errors which is to be expected, however I have not found a way to solve these. I tried changing the bottom part to:

const { Content } = await props.post.render();

export const prerender = true;
---

<BlogPost {...props.post.data}>
  <Content />
</BlogPost>

I'm hoping somebody here knows a way to fix this.

Edit: Astro 3.1

cerulean osprey
#

instead of being in a [...slug].astro file, it should be in [...page].astro file, and then instead of returning params and props, you'd have something like this:

return paginate((posts.sort(sortByDate), { pageSize: n)

If you want an example, you can see my blog

GitHub

My personal website/portfolio. Contribute to jhilker98/jhilker98.github.io development by creating an account on GitHub.

wild birch
cerulean osprey
#

yes

wild birch
#

Hmm interesting, i'll check your code a bit and try it out. Thanks for the fast answer !

cerulean osprey
#

yep! granted I'm still playing around with styling for it for each page, but it does work functionally

wild birch
#

Nice, styling wasn't too bad with tailwind but i just kept running into tailwind issues in Astro for some weird reason

cerulean osprey
#

oh? what sort of issues?

wild birch
#

There's some weird stuff going on with styles not applying / something fighting with Tailwind. I eventually wrapped everything around a div with a class name of "prose" to get it kind of working, but I found it extremely weird

#

I then restarted in a clean project (the other was the Astro JS Blog starting template) and ran into some issues again, but this time it seemed a bit better

cerulean osprey
#

oh? are you using the typography plugin? if so that might be the issue, I'd need to see your code though

wild birch
#

Closed repo, ill open it for a bit

cerulean osprey
#

oh alright

#

well the typography plugin uses a class called prose, so i was going to say if so you were that would explain it

wild birch
#

Ah yeah, the old project did use that (I had it saved locally, just checked)

cerulean osprey
#

that would explain it

wild birch
cerulean osprey
#

alright, thanks

wild birch
#

Still a bit bad with Astro though, so you'll likely find some bad code

cerulean osprey
#

honestly i'm not great either

#

how many posts were you thinking on each page?

wild birch
#

Haven't quite planned to add pagination yet, but when I do add it probably like 8-10

cerulean osprey
#

ok

spring adder
wild birch
#

Nice, I build this before I React but this seems to be a bit easier

cerulean osprey
#

yeah - I can merge the [...page].astro i created in my fork into yours - I haven't created the pagination menu but you will be able to play around with it

wild birch
#

Ah - yeah sure, ill then be able to customize it to my needs

cerulean osprey
#

yeah, i just made a placeholder for it

wild birch
#

Nice, thanks man

spring adder
#

Side note: with my previous solution, you don't need to name the file [page].astro.

wild birch
#

Right. Well ill see how far I can get with this 🙂