Skip to content

Counting website hits using Cloudflare workers

blog.aawadia.dev
4 pointsasadawadia2 comments
On HN

Comments

Nice tutorial. One catch:

    // asynchronously make the request to the counter service
    // don't wait for this to finish before responding back to the client
    fetch("https://api/" + counterName, requestOptions)
I would recommend wrapping this in `event.waitUntil()`, like:
    event.waitUntil(fetch("https://api/" + counterName, requestOptions))
Otherwise, the Workers Runtime will cancel this call as soon as the top-level response is returned. That may not be causing any trouble here because the cancellation happens too late to actually have an effect (the cancellation would manifest as the HTTP connection to the counter API being closed, but by the time it notices it has probably already finished incrementing the counter). Still, it's a good idea to be safe add add the waitUntil().

Ah, Thanks! Didn't consider/know that

AboutSource Built by g1lg1l

Hackerly is an independent reader for Hacker News, built on the public HN API. Not affiliated with Y Combinator.