My DEV Social Counter

Written by BenjaminPrice | Published 2020/02/28
Tech Story Tags: web-development | javascript | blog | social | open-core | devto

TLDR The DEV Social Badge is an unofficial social badge that queries the DEV.to API to see if your blog post has been posted on DEV. The first release, v0.1.0, should still be considered unstable and used at your own risk. You must be using Canonical URLs on your blog posts to get the badge to appear. If the script doesn't find a matching post, nothing gets added to your page. The API is cached for 24 hours, so don't expect your badge to show the positive reaction count in real-time.via the TL;DR App

I just open-sourced the codebase for the DEV Social Badge that I've been working on. Check it out on GitHub!
DEV Social Badge is an unofficial social badge that queries the DEV.to API to see if your blog post has been posted on DEV.
This first release, v0.1.0, should still be considered unstable and used at your own risk.
An important requirement for this to work: You must be using Canonical URLs on DEV.
This allows the code to determine which of your DEV posts matches the post hosting the badge.

Using the script

Using the script is fairly straight forward. You have to add a couple of tags to your <head> and a couple in the <body>, where you want the badge to appear.
Adding the script tag to the <head>
Add the following script tag somewhere in your <head> to load the minified script.
<!-- Place script tag before the end of the head tag -->
<script src="https://unpkg.com/dev-social-badge@0.1.0/dist/dev-social-badge.min.js"></script>
Adding the devbadge and script tags to the <body>
The <devbadge /> will be replaced by the script with the social badge, if the script is able to find a matching post on DEV. If it doesn't return any matching posts, nothing gets added to your page.
If you're using a template for your blog posts, I suggest adding this to the template file for your blog posts - not your main site layout. This will help to reduce unnecessary API calls to DEV.to.
<!-- Place devbadge tag wherever you'd like the badge to appear -->
<devbadge />
<!-- Place script tag before the end of the body tag -->
<script>window.onload = function(){typeof findOnDev !== "undefined" && findOnDev()}</script>

API Limits

Currently, there are no published limits on API calls. But, that doesn't mean that there isn't one. It's possible that this could stop functioning.
Additionally, the DEV API documentation states that responses are cached for 24 hours. Don't expect your badge to show the positive reaction count in real-time.

You can help

You don't need to write any code to help the project. Just giving the repository a β˜…Star on GitHub will help towards getting the project hosted on other CDNs, such as cdnjs.
I will be adding Issues to the repository for areas where I'd like some help, as well as to get my improvement ideas out of my head, over the next day or two. If you'd like to contribute to the code, please check out the Contribution Guidelines (so far, they're quite sparse).

Let me hear from you

Please share your thoughts in the comments thread on DEV. I'd love to get your feedback and opinions.

Written by BenjaminPrice | πŸ‘¨πŸ»β€πŸ’» Head of Engineering (CRM) @ Mercari Inc
Published by HackerNoon on 2020/02/28