#cloudflare #serverless #cloud #frontend #development

i have a whole cloud just for my sites comments

Miuna

have u ever stopped to think that some people use disqus on their blog? or that facebook comment thing? ugh, gross ( ˘•ω•˘ )

not me. i built my own comment system from scratch and im using an entire cloud infrastructure just for that. like, an article page has its few kb of html and css, and in the comments theres an edge function running, kv storage, turnstile, rate limit, everything serverless. sounds overkill? thats bc it is. but its so much fun~

why i didnt use disqus

simple. i hate trackers. i hate ads. i hate when some third party site keeps injecting scripts into my site and loading stuff from another domain. also disqus is ugly. doesnt match my sites look u.u

so i wondered, could i make something simple that would be just mine? no dependencies on anyone? and it worked. i used cloudflare pages functions with kv storage. for those who dont know, kv is like a key-value database from cloudflare, but its not a traditional database, its more like a globally distributed cache. perfect for comments~ (≧◡≦)

how the flow works

someone opens the article, the js script does a fetch to /api/comments?slug=article-name, the edge function receives it, looks up the key comments:article-name in kv and returns a json array. simple as that.

to send one, theres a basic form with optional name, textarea for text, and a hidden author password field. if i fill in the right password, the comment comes out with an isAuthor flag set to true. then it shows its me~ (´。• ᵕ •。`)

before saving, the backend checks a honeypot. its an invisible field called website. if a bot fills it, instant ban. then comes cloudflare turnstile to make sure its not a bot. if it passes, it checks rate limit by ip. u can only comment every 10 seconds. if u try faster, u get 429.

the comments live in a json in kv, each slug has its own key. theres a 500 comment limit per article. if it goes over that, the oldest one disappears. like fifo. pretty rude, but it works.

cloudflare kv panel showing stored comment keys
real cloudflare kv panel with actual keys. the comments are literally a json stored in the cloud~

reply threads

oh, and u can reply to comments. its not rocket science. it just has a parentId field in the json. if its null, its a root comment. if it has a uuid, its a reply. the frontend just filters and groups. no sql needed, no joins, none of that. pure json and hand made (¬‿¬)

and the text supports basic markdown. *italic*, **bold**, `code`. i wrote the parser in js myself, super simple, regex replacement. doesnt support tables or anything complicated, just the basics so its not boring.

the python script that injects everything

heres where it gets different. i have a python script called inject-comments.py that runs through the artigos/ and estudos/ folders and injects the <script src="../js/comments.js"></script> tag before the </body> of all htmls.

if the script is already there, it skips. if not, it adds it. simple, practical, and i dont have to edit file by file. just run the py and done. every new article automatically gets comments~ ( ´ ▽ ` )ノ

i know u can do this with build tools, bundlers, whatever. but i like simple static sites. i dont want webpack, i dont want vite, i dont want any of that. pure python and pure html. its a more old school vibe and i like it.

serverless isnt just a buzzword

when i say its serverless, its bc theres literally no server running 24/7. the edge function only executes when someone makes a request. kv is distributed storage. no virtual machine, no container, no provisioned database. everything on-demand.

for a small blog like mine, this is perfect. i dont pay for idle time, i dont need to backup databases, i dont need to configure postgres. i just grab my htmls, throw them on cloudflare pages, and the function + kv take care of the rest. digital minimalism even in the infra~ (。♥‿♥。)

of course if my site went viral and had millions of comments, kv probably wouldnt be the best choice. but im realistic. my blog isnt gonna go viral. and if it does, ill think about migrating then. for now, its just a pretty little json sitting in the cloud.

for moderation theres a separate password. only whoever has the moderator password can delete comments via delete method. im the only one with the password. end of story.

sometimes i look at all this and think, dude, theres an entire cloud infrastructure running just so a few people can leave "great article miuna" in the comments. is it overkill? definitely. but its my overkill, and i love it.

if u have a static blog and are thinking about adding comments, consider building something of ur own. doesnt need to be complex. a json, an edge function, and a bit of js on the frontend. its more satisfying than pasting a third party widget and calling it a day. and it looks like u~ (ノ◕ヮ◕)ノ*:・゚✧

ler em português →