Live data from Hacker News

Adding view count and like button to 11ty

annoyscript.vercel.app

1–10 of 13 posts

Re: Adding view count and like button to 11ty

#2
There doesn't seem to be a limit to how many times a reader can smash that like button, which brings a new degree of expression to smashing that like. You can smash that like 10 times the article was good, 100 times if it was amazing, or 100 million times if you hated it and want the author to be bankrupted by their Vercel bill.

Re: Adding view count and like button to 11ty

#3
post #2

There doesn't seem to be a limit to how many times a reader can smash that like button, which brings a new degree of expression to smashing that like. You can smash that like 10 times the article was good, 100 times if it was amazing, or 100 million times if you hated it and want the author to be bankrupted by their Vercel bill.

That's how medium does it, except they name it claps (or applause or something like that). I always kinda liked that about it

Re: Adding view count and like button to 11ty

#4
The HSET calls are not just unnecessary, they're a bug. It introduces the possibility of a race condition where a view or like happens between the HGET and HSET. HSETing the values to zero doesn't actually make anything faster or more correct, it just introduces the possibility of overwriting a value that came in moments ago.

Re: Adding view count and like button to 11ty

#5
post #2

There doesn't seem to be a limit to how many times a reader can smash that like button, which brings a new degree of expression to smashing that like. You can smash that like 10 times the article was good, 100 times if it was amazing, or 100 million times if you hated it and want the author to be bankrupted by their Vercel bill.

are you the one that is actually DDoSing my Vercel KV lol? I had like 5k requests in the last few hours ^^

As I mentioned in the blog, I have a free tier, so I just got an email from Vercel that KV will stop accepting new request for today.

Re: Adding view count and like button to 11ty

#6

The HSET calls are not just unnecessary, they're a bug. It introduces the possibility of a race condition where a view or like happens between the HGET and HSET. HSETing the values to zero doesn't actually make anything faster or more correct, it just introduces the possibility of overwriting a value that came in moments ago.

so how would you approach this?

Re: Adding view count and like button to 11ty

#7
post #5
post #2

There doesn't seem to be a limit to how many times a reader can smash that like button, which brings a new degree of expression to smashing that like. You can smash that like 10 times the article was good, 100 times if it was amazing, or 100 million times if you hated it and want the author to be bankrupted by their Vercel bill.

are you the one that is actually DDoSing my Vercel KV lol? I had like 5k requests in the last few hours ^^ As I mentioned in the blog, I have a free tier, so I just got an email from Vercel that KV will stop accepting new request for today.

(I work at Vercel) You can set up the Vercel Firewall to block traffic: https://vercel.com/docs/security/vercel-waf/custom-rules

Re: Adding view count and like button to 11ty

#8
post #7
post #5

Earlier quoted context omitted.

are you the one that is actually DDoSing my Vercel KV lol? I had like 5k requests in the last few hours ^^ As I mentioned in the blog, I have a free tier, so I just got an email from Vercel that KV will stop accepting new request for today.

(I work at Vercel) You can set up the Vercel Firewall to block traffic: https://vercel.com/docs/security/vercel-waf/custom-rules

thank you kind soul. I just push new version of the script so it only accepts 3 likes per visit. Not ideal, but a quick fix (:

Re: Adding view count and like button to 11ty

#9
post #6

The HSET calls are not just unnecessary, they're a bug. It introduces the possibility of a race condition where a view or like happens between the HGET and HSET. HSETing the values to zero doesn't actually make anything faster or more correct, it just introduces the possibility of overwriting a value that came in moments ago.

so how would you approach this?

Just do hincrby alone, if it doesn't exist it will automatically become 1

Re: Adding view count and like button to 11ty

#10
post #6

Earlier quoted context omitted.

so how would you approach this?

Just do hincrby alone, if it doesn't exist it will automatically become 1

You are right. It just should be like so:

Get:

   const likes = (await kv.hget(postTitle, "likes")) || 0
Post:

   const likes = await kv.hincrby(postTitle, "likes", 1);
Post reply on HN