I made a free Dev.to Writing Streak Calculator using Next.js, Day.js and the Dev.to API
Tags: productivity, showdev, javascript, react
Published: 2021-04-13 22:26:44

---
description: Do you wonder what your current weekly posting streak is? I made a tool that will tell you! All you need to do is enter your Dev.to username.
---
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](https://dev.to/jameswallis/deploying-a-next-js-project-on-vercel-in-less-than-three-minutes-with-a-custom-domain-568o).

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](https://devto-writing-streak-calculator.wallis.dev)

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

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/5r9gco37i55t394d6geg.png">
  <figcaption>The Writing Streak Calculator</figcaption>
</figure>

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](https://github.com/forem/forem/blob/master/app/services/badges/award_streak.rb#L3-L12)).

To build it I used my usual tools with a couple of others:
* [Next.js](https://nextjs.org)
* [Tailwind CSS](https://tailwindcss.com)
* [Dev.to API](https://docs.forem.com/api)
* [DayJS](https://day.js.org) - Heavily for calculating the streaks
* [SWR](https://swr.vercel.app) - Fetches articles and user data from the Dev.to API
* [Preact](https://preactjs.com) - Reduced first load size from 83kB to 49kB (Used for the first time, seems good!)

[Source code on GitHub.](https://github.com/james-wallis/devto-writing-streak-calculator)

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](https://github.com/james-wallis/devto-writing-streak-calculator/issues).*

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](https://dev.to/jameswallis/i-built-a-dev-to-analytics-dashboard-to-track-historic-post-data-2c7o).

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/d1pa3rq439tco4vlo2jx.png">
  <figcaption>Writing Streak displayed on my analytics dashboard (final box on the right)</figcaption>
</figure>
 

---

## 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](https://github.com/james-wallis/devto-writing-streak-calculator/issues). 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.

<figure>
  <img src="https://dev-to-uploads.s3.amazonaws.com/uploads/articles/muv2yfolf1icalmtmbky.png">
  <figcaption>December 2020 - January 2021. Red circles denote published dates.</figcaption>
</figure>

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!