Hacker Newsnew | past | comments | ask | show | jobs | submitlogin
Counting website hits using Cloudflare workers (aawadia.dev)
4 points by asadawadia on July 3, 2023 | hide | past | favorite | 2 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




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: