i renamed a post and all comments disappeared. all of them. no warning. no backup. just bc i changed the filename. (¬_¬)
the article was "im hating the current internet". then i thought "tired of the current internet" sounded better. simple. renamed the html, adjusted links, git push and done.
except i forgot one thing. the comment system uses the filename as key in cloudflare kv. when i changed from hating-current-internet to tired-current-internet, the backend looked for a key that didnt exist yet. and the old comments got stuck in the old key, orphaned, with no one to read them.
how the comment system works
i use cloudflare kv to store comments. its a simple key-value database. each article has a key like comments:artigos/filename.
on the frontend, the js grabs the url path, removes the leading slash and the .html, and uses that as slug. on the backend, the function receives that slug and queries kv.
works perfectly until u rename a file. then it becomes a silent problem. no error. no crash. just empty. as if no one had ever commented.
found out it wasnt just this article
i checked git history to understand what happened. and remembered i had renamed other posts before.
"sms-auth-is-inconvenience-in-disguise" was "hate-phone-code-login". "hate-mobile-design" was "odeio-responsividade-mobile". "mobile-mmorpg-suck-nowadays" was "mmorpgs-mobile-chatice".
probably all of those lost comments on rename and i didnt even notice. bc who goes back to an old post to check if the conversation is still there?
the fix i made
instead of manually messing with kv, i implemented a lazy migration on the backend. when someone accesses an article and the new key is empty, the system checks if theres an old mapped key. if there is, it copies the data to the new key and serves normally.
i added a simple object in the code with old and current slugs. is it a hack? yeah. but it works and solves the problem without me having to run a migration script or pay for backup tools.
if u use kv or any key-value database with slug as key, be careful when renaming content. this type of bug is silent and easy to go unnoticed.
now whenever i rename a post ill remember to add the mapping in the code first. or maybe ill change the architecture in the future to use a fixed id instead of the filename. but for now, the hack saves the day~ (´。• ᵕ •。`)
and if u ever commented on one of my posts and one day it didnt show up anymore, sorry. it was an accident. the rename is to blame.