I made a free Dev.to Writing Streak Calculator using Next.js, Day.js and the Dev.to API

jameswallis

James Wallis

Posted on April 13, 2021

I made a free Dev.to Writing Streak Calculator using Next.js, Day.js and the Dev.to API

Do you wonder what your current weekly posting streak is?

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 Dev.to calculate the writing streak? Can I build a tool to help fellow Dev.to 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 Dev.to 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
  • Dev.to API
  • DayJS - Heavily for calculating the streaks
  • SWR - Fetches articles and user data from the Dev.to 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!
https://devto-writing-streak-calculator.wallis.dev

Bonus - display on your own site

There is also an API route that will fetch your latest and longest writing streak. You just pass your username as a query param.

https://devto-writing-streak-calculator.wallis.dev/api/calculate?username=jameswallis

I use it to display my current writing streak on my Advanced Dev.to Dashboard with historical analytics.


Writing Streak displayed on my analytics dashboard (final box on the right)


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 Dev.to 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:
https://devto-writing-streak-calculator.wallis.dev


Summary

I've introduced the writing streak calculator tool made for the Dev.to 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!

💖 💪 🙅 🚩
jameswallis
James Wallis

Posted on April 13, 2021

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related