Web3 With Solidity And Svelte

Published: Sun May 16 2021

This blog currently runs on Sapper. It’s a great system, but I’ll be moving to SvelteKit in the not too distant future. I read a post by @flaviocopes about his stack and how he schedules posts every day. I was inspired. Not only that, but I’ll be working to get a backlog and up my consistency posting here.

To get started, I debated switching to HUGO, go is fun and all, but I’m really enjoying Svelte and decided to stick with this little blog. I like how simple it is and knowing that I built it all.

Let’s build for the future!

The first feature I am implementing is preventing future posts from showing up here. I guess that’s, preventing the future from building?

In several places, blog/index.svelte for example, I fetch the blog posts. I initially was going to filter them here from the posts returned. This got fairly repetitive.

<script context="module">
  export function preload() {
    return this.fetch(`blog.json`)
      .then((r) => r.json())
      .then((posts) => {
        return { posts };
      });
  }
</script>

How could I DRY this out?

These fetch from blog.json 🤔

I’ll fix it at the source!

I have a function sortPosts() which puts them in order by date. They already are looking at the date I up_dated_ the function to sortAndFilterPosts()

function sortAndFilterPosts(posts) {
  const q = new Date();
  const m = q.getMonth() + 1;
  const d = q.getDay();
  const y = q.getFullYear();
  const today = new Date(y, m, d);

  posts = posts.filter((post) => {
    return post.date <= today;
  });
  return posts.sort((post1, post2) => {
    const date1 = new Date(post1.date);
    const date2 = new Date(post2.date);
    return date2 - date1;
  });
}

The messy bit with today is to make sure the date formats match when I compare them. It works well enough.

Next time I’ll work on getting the site to build once a day to ensure that the posts from the future show up! 👍