CodeNewbie Community

James Wallis
James Wallis

Posted on • Originally published at on

I made a free Posting Streak Calculator using Next.js, Day.js and the API

Do you wonder what your current weekly posting streak is?

Originally written for AFAIK CodeNewbie doesn't have writing streak badges. If there is interest it should be pretty easy to extend it to work with the CodeNewbie API as well as Comment below if you want this!

On Monday (12th April 2021), I was awarded the 16-week streak badge. It was a surprise, albeit a welcome one, as I figured that I was two weeks away from writing for sixteen continuous weeks with my first article being published on January 10th, 2021.

This made me think - how does calculate the writing streak? Can I build a tool to help fellow community members understand if they're on track for their next milestone.

Below you'll find the answers - tl;dr made a tool that's available for anyone to use.

The Writing Streak Calculator

I'll demo the app first and further down explain how I calculate the user's current writing streak.

The Writing Streak Calculator

The app is simple by design.

It consists of a single input box for your username and a text box next to it. Once you've entered your username it will calculate your current writing streak and report it back to you (with some text that is borrowed from the Forem codebase).

To build it I used my usual tools with a couple of others:

  • Next.js
  • Tailwind CSS
  • API
  • DayJS - Heavily for calculating the streaks
  • SWR - Fetches articles and user data from the API
  • Preact - Reduced first load size from 83kB to 49kB (Used for the first time, seems good!)

Source code on GitHub.

That is pretty much it. At this point, there are no overly complicated features. Some things I'd like to add, however, are:

  • Date since last posted
  • Longest writing streak
  • A warning that the user needs to post if they're close to the end-of-week deadline
  • Support other Forem sites (CodeNewbie etc)

Something more you want to see? Add it in the comments or open an issue.

In case you missed it above, here's the link for the tool. Go check out your writing streak!

How the writing streak is calculated

Prewarning : There is the possibility that I'm calculating it incorrectly. Hopefully, a Forem contributor will call me out and I can correct it! Otherwise, if you think your streak isn't right let me know in the comments or raise an issue on GitHub. That being said, it's correct for me for two separate streaks so I'm confident.

I started by noting when I was awarded the 16 weeks and 4-week badges (I deleted the email for 8 weeks):

  • 4 weeks: Monday 17th August 2020
  • 16 weeks: Monday 12th April 2021

So badges are awarded on Mondays.

Next, I wanted to understand why my streak wasn't broken between the 30th December 2020 (2nd post in the streak) and the 10th January 2021 (3rd post in the streak) - that's an 11 day difference. After searching for missing articles and trying to find reason in the Ruby on Rails code, I eventually just checked a calendar.

December 2020 - January 2021. Red circles denote published dates.

It turned out that the 10th January 2021 was the week after the 30th December 2020 when the week starts on a Monday.

So I came to the conclusion that as long as a post is published between Monday and Sunday then the streak would continue!

Here's the link again:


I've introduced the writing streak calculator tool made for the community.

If you've enjoyed this article or the tool, react!

Let me know your latest streak or any comments down below.

Thanks for reading!

Discussion (2)

ellativity profile image
Ella (she/her/elle)

This is such a great project, @jameswallis ! Thanks for sharing your process and result with us!

jameswallis profile image
James Wallis Author

Thank you! No worries, hopefully you found it somewhat helpful!