DEV Community

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

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

James Wallis
Software Developer at Ostmodern, Computer Science Graduate, University of Portsmouth
Originally published at wallis.dev ・4 min read

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!

Discussion (8)

Collapse
devlorenzo profile image
DevLorenzo

Well done!
Just a question for you, I saw that the redirect link (top right) for @ben lead to his "portfolio", have you manually inserted it?
Because if so, you did the same for my newsletter at my name ("worldindev.ck.page/")!

Collapse
jameswallis profile image
James Wallis Author

Thanks!
That's using the user's website URL (or Dev.to profile URL if they haven't set one). See website_url here: docs.forem.com/api/#operation/getUser. You'll have set your newsletter URL as your website URL in your Dev.to settings.

Collapse
kallmanation profile image
Nathan Kallman

Would have been nice a couple weeks ago when I was trying to count all my weeks 😅 At least I know now I counted correctly!

Image of calculator saying "Your current streak:<br>
52 weeks! You've achieved the longest writing streak possible (16 weeks)."

Collapse
jameswallis profile image
James Wallis Author

Congratulations on 52 weeks of writing! I'm exhausted after just 16 😅

Yeah, that or we both counted incorrectly 😂. . It's actually really helpful that you've been able to calculate yours as well to verify my method. Thanks!

Collapse
dev_emmy profile image
nshimiye_emmy

you did a great job buddy, Thanks.

Collapse
jameswallis profile image
James Wallis Author

Thank you!

Collapse
andrewbaisden profile image
Andrew Baisden

Wow very cool!

Collapse
jameswallis profile image
James Wallis Author

Thanks!